1 | using Dal.ApplicationStorage.DataAccess.Abstract;
|
---|
2 | using Microsoft.EntityFrameworkCore;
|
---|
3 | using Microsoft.Extensions.Logging;
|
---|
4 | using Models.DataTransferObjects.Administrator;
|
---|
5 | using Models.DataTransferObjects.Company;
|
---|
6 | using System;
|
---|
7 | using System.Collections.Generic;
|
---|
8 | using System.Linq;
|
---|
9 | using System.Text;
|
---|
10 | using System.Threading.Tasks;
|
---|
11 |
|
---|
12 | namespace Dal.ApplicationStorage.DataAccess.Concrete
|
---|
13 | {
|
---|
14 | public class AdministratorDa : IAdministrator
|
---|
15 | {
|
---|
16 | private readonly ApiContext _db;
|
---|
17 | private static ILogger<AdministratorDa> _logger;
|
---|
18 |
|
---|
19 | public AdministratorDa(ApiContext db,ILogger<AdministratorDa> logger)
|
---|
20 | {
|
---|
21 | _db = db;
|
---|
22 | _logger = logger;
|
---|
23 | }
|
---|
24 |
|
---|
25 | public async Task<List<AdministratorCompaniesDTO>> GetAllCompanies()
|
---|
26 | {
|
---|
27 | try
|
---|
28 | {
|
---|
29 | var companies = new List<AdministratorCompaniesDTO>();
|
---|
30 | var companiesFromDb = await _db.Companies.Include(x => x.BusinessUser.User).ToListAsync();
|
---|
31 | foreach (var company in companiesFromDb)
|
---|
32 | {
|
---|
33 |
|
---|
34 | companies.Add(new AdministratorCompaniesDTO()
|
---|
35 | {
|
---|
36 | CompanyEmail = company.CompanyEmail,
|
---|
37 | CompanyName = company.CompanyName,
|
---|
38 | CompanyId = company.CompanyId,
|
---|
39 | IsApproved = company.AdministratorId != null ? true : false,
|
---|
40 | BusinessUserId = company.BusinessUserId,
|
---|
41 | BusinessUserName = company.BusinessUser.User.Username,
|
---|
42 | BusinessUserEmail = company.BusinessUser.User.Email,
|
---|
43 |
|
---|
44 | });
|
---|
45 | }
|
---|
46 | return companies;
|
---|
47 | }
|
---|
48 | catch (Exception e)
|
---|
49 | {
|
---|
50 | _logger.LogError(e.Message);
|
---|
51 | throw;
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public async Task<bool> ApproveCompany(int companyId, int administratorId)
|
---|
56 | {
|
---|
57 | try
|
---|
58 | {
|
---|
59 | var companyFromDb = await _db.Companies.Where(x => x.CompanyId == companyId).FirstOrDefaultAsync();
|
---|
60 | if (companyFromDb != null)
|
---|
61 | {
|
---|
62 | companyFromDb.AdministratorId = administratorId;
|
---|
63 | _db.Update(companyFromDb);
|
---|
64 | await _db.SaveChangesAsync();
|
---|
65 | }
|
---|
66 | return true;
|
---|
67 | }
|
---|
68 | catch (Exception e)
|
---|
69 | {
|
---|
70 | _logger.LogError(e.Message);
|
---|
71 | throw;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | }
|
---|
76 | }
|
---|