source: Application/Dal/ApplicationStorage/DataAccess/Concrete/CompanyDa.cs

Last change on this file was f5f7c24, checked in by 192011 <mk.snicker@…>, 17 months ago

Initial commit

  • Property mode set to 100644
File size: 3.5 KB
Line 
1using Dal.ApplicationStorage.DataAccess.Abstract;
2using Microsoft.EntityFrameworkCore;
3using Microsoft.Extensions.Logging;
4using Models.DatabaseModels;
5using Models.DataTransferObjects.Administrator;
6using Models.DataTransferObjects.Company;
7using ocrent;
8using System;
9using System.Collections.Generic;
10using System.ComponentModel.DataAnnotations;
11using System.ComponentModel.Design;
12using System.Linq;
13using System.Text;
14using System.Threading.Tasks;
15
16namespace 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}
Note: See TracBrowser for help on using the repository browser.