1 | using Dal.ApplicationStorage.DataAccess.Abstract;
|
---|
2 | using Microsoft.EntityFrameworkCore;
|
---|
3 | using Microsoft.Extensions.Logging;
|
---|
4 | using Models.DatabaseModels;
|
---|
5 | using Models.DataTransferObjects.Administrator;
|
---|
6 | using Models.DataTransferObjects.Company;
|
---|
7 | using ocrent;
|
---|
8 | using System;
|
---|
9 | using System.Collections.Generic;
|
---|
10 | using System.ComponentModel.DataAnnotations;
|
---|
11 | using System.ComponentModel.Design;
|
---|
12 | using System.Linq;
|
---|
13 | using System.Text;
|
---|
14 | using System.Threading.Tasks;
|
---|
15 |
|
---|
16 | namespace Dal.ApplicationStorage.DataAccess.Concrete
|
---|
17 | {
|
---|
18 | public class CompanyDa : ICompanyDa
|
---|
19 | {
|
---|
20 | private readonly ApiContext _db;
|
---|
21 | private static ILogger<CompanyDa> _logger;
|
---|
22 |
|
---|
23 | public CompanyDa(ApiContext db, ILogger<CompanyDa> logger)
|
---|
24 | {
|
---|
25 | _db = db;
|
---|
26 | _logger = logger;
|
---|
27 | }
|
---|
28 |
|
---|
29 | public async Task<bool> AddCompany(RegisterCompanyDTO companyDTO)
|
---|
30 | {
|
---|
31 | try
|
---|
32 | {
|
---|
33 | Company company = new Company();
|
---|
34 |
|
---|
35 | company.CompanyName = companyDTO.CompanyName;
|
---|
36 | company.CompanyEmail = companyDTO.CompanyEmail;
|
---|
37 | company.CreatedOn = DateOnly.FromDateTime(DateTime.Now);
|
---|
38 | company.CreatedBy = companyDTO.CreatedBy;
|
---|
39 | company.BusinessUserId = companyDTO.CreatedBy;
|
---|
40 |
|
---|
41 | _db.Companies.Add(company);
|
---|
42 | await _db.SaveChangesAsync();
|
---|
43 |
|
---|
44 | return true;
|
---|
45 | }
|
---|
46 | catch (Exception e)
|
---|
47 | {
|
---|
48 | _logger.LogError(e.Message);
|
---|
49 | throw;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public async Task<List<UserCompaniesDTO>> GetUserCompanies(int userId)
|
---|
54 | {
|
---|
55 | try
|
---|
56 | {
|
---|
57 | List<UserCompaniesDTO> userCompanies = new List<UserCompaniesDTO>();
|
---|
58 | var userCompaniesFromDb = await _db.Companies.Where(x => x.BusinessUserId == userId).ToListAsync();
|
---|
59 |
|
---|
60 | foreach(var company in userCompaniesFromDb)
|
---|
61 | {
|
---|
62 |
|
---|
63 | userCompanies.Add(new UserCompaniesDTO()
|
---|
64 | {
|
---|
65 | CompanyEmail = company.CompanyEmail,
|
---|
66 | CompanyName = company.CompanyName,
|
---|
67 | CompanyId = company.CompanyId,
|
---|
68 | IsApproved = company.AdministratorId != null ? true : false
|
---|
69 | });
|
---|
70 | }
|
---|
71 | return userCompanies;
|
---|
72 |
|
---|
73 | }
|
---|
74 | catch (Exception e)
|
---|
75 | {
|
---|
76 | _logger.LogError(e.Message);
|
---|
77 | throw;
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public async Task<AdministratorCompaniesDTO> GetCompany(int companyId)
|
---|
82 | {
|
---|
83 | try
|
---|
84 | {
|
---|
85 | var companyFromDb = await _db.Companies.Include(x => x.BusinessUser.User).Where(x => x.CompanyId == companyId).FirstOrDefaultAsync();
|
---|
86 | AdministratorCompaniesDTO company = new AdministratorCompaniesDTO();
|
---|
87 | company.CompanyEmail = companyFromDb.CompanyEmail;
|
---|
88 | company.IsApproved = companyFromDb.AdministratorId != null ? true : false;
|
---|
89 | company.BusinessUserName = companyFromDb.BusinessUser.User.Username;
|
---|
90 | company.BusinessUserId = companyFromDb.BusinessUserId;
|
---|
91 | company.BusinessUserEmail = companyFromDb.CompanyEmail;
|
---|
92 | company.CompanyName = companyFromDb.CompanyName;
|
---|
93 | company.CompanyId = companyFromDb.CompanyId;
|
---|
94 | return company;
|
---|
95 | }
|
---|
96 | catch (Exception e)
|
---|
97 | {
|
---|
98 | _logger.LogError(e.Message);
|
---|
99 | throw;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|