source: Application/Dal/ApplicationStorage/DataAccess/Concrete/IdentityDa.cs@ f5f7c24

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

Initial commit

  • Property mode set to 100644
File size: 3.6 KB
Line 
1using Dal.ApplicationStorage.DataAccess.Abstract;
2using Microsoft.Build.Framework;
3using Microsoft.EntityFrameworkCore;
4using Microsoft.Extensions.Logging;
5using Models.DatabaseModels;
6using Models.DataTransferObjects;
7using ocrent;
8using System;
9using System.Collections.Generic;
10using System.Configuration;
11using System.Linq;
12using System.Security.Principal;
13using System.Text;
14using System.Threading.Tasks;
15
16namespace Dal.ApplicationStorage.DataAccess.Concrete
17{
18 public class IdentityDa : IIdentityCustomDa
19 {
20 private readonly ApiContext _db;
21 private static ILogger<IdentityDa> _logger;
22
23 public IdentityDa(ApiContext db,ILogger<IdentityDa> logger)
24 {
25 _db = db;
26 _logger = logger;
27
28 }
29
30 public async Task<bool> Register(RegisterDTO registerInfo)
31 {
32 try
33 {
34
35 User user = new User();
36
37 user.Email = registerInfo.Email;
38 user.FirstName = registerInfo.FirstName;
39 user.LastName = registerInfo.LastName;
40 user.Username = registerInfo.Username;
41 user.Pass = registerInfo.Pass;
42 user.CreatedOn = DateOnly.FromDateTime(DateTime.Now);
43
44 _db.Users.Add(user);
45 await _db.SaveChangesAsync();
46
47 var userIdFromDb = await _db.Users.Where(x => x.Email.Equals(registerInfo.Email)).Select(x => x.UserId).FirstOrDefaultAsync();
48 if (registerInfo.IsBusinessUser)
49 {
50 _db.BusinessUsers.Add(new BusinessUser() { UserId = userIdFromDb });
51 }
52 else
53 {
54 _db.Clients.Add(new Client()
55 {
56 UserId = userIdFromDb
57 });
58 }
59
60 await _db.SaveChangesAsync();
61
62 return true;
63 }
64 catch (Exception e)
65 {
66 _logger.LogError(e.Message);
67 throw;
68 }
69 }
70 public async Task<LoginDTO> CheckLoginInformation(LoginDTO loginInfo)
71 {
72 try
73 {
74 var userFromDb = await _db.Users.Include(x => x.Administrator).Include(x => x.Client).Include(x => x.BusinessUser).Where(x => x.Email.Equals(loginInfo.Email)).FirstOrDefaultAsync();
75
76 if(userFromDb != null)
77 {
78 loginInfo.UserId = userFromDb.UserId;
79 loginInfo.ValidEmail = true;
80 loginInfo.FirstName = userFromDb.FirstName;
81 if (userFromDb.Pass.Equals(loginInfo.Password))
82 {
83 loginInfo.ValidPassword = true;
84 }
85 else
86 {
87 loginInfo.ValidPassword = false;
88 }
89 if(userFromDb.Administrator != null)
90 {
91 loginInfo.Claim = "Administrator";
92 }
93 else if (userFromDb.BusinessUser != null)
94 {
95 loginInfo.Claim = "BusinessUser";
96 }
97 else
98 {
99 loginInfo.Claim = "Client";
100 }
101
102 }
103 else
104 {
105 loginInfo.ValidEmail = false;
106 }
107
108 return loginInfo;
109
110
111 }
112 catch (Exception e)
113 {
114 _logger.LogError(e.Message);
115 throw;
116 }
117 }
118 }
119}
Note: See TracBrowser for help on using the repository browser.