Changeset 13f1472 for resTools_backend/backend/Services/UserService.cs
- Timestamp:
- 09/18/22 18:09:53 (2 years ago)
- Branches:
- master
- Parents:
- 49b0bbd
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
resTools_backend/backend/Services/UserService.cs
r49b0bbd r13f1472 20 20 Task<AuthenticateResponse> Register(CreateUserRequest req, bool isFirst); 21 21 Task<User> GetById(int id); 22 Task<User> GetByEmail(string email); 22 23 Task SendEmailConfirmation(string email); 23 24 Task SendPasswordReset(string email); 24 25 Task ConfirmEmail(User user, string checkValid); 25 26 Task ResetPassword(string checkValid, string password); 27 Task<List<UserResponse>> GetUsers(); 28 Task UpdateVipStatus(int id, bool isVip); 29 26 30 } 27 31 … … 39 43 } 40 44 45 public async Task<User> GetByEmail(string email) 46 { 47 return await _context.Users.FirstOrDefaultAsync(x => x.Email == email); 48 } 49 41 50 public async Task<AuthenticateResponse> Authenticate(AuthenticateRequest model) 42 51 { … … 49 58 var token = generateJwtToken(user); 50 59 51 return new AuthenticateResponse { Email = user.Email, Id = user.Id, Token = token, IsAdmin = user.IsAdmin, IsConfirmed = user.IsConfirmed };60 return new AuthenticateResponse { Email = user.Email, Id = user.Id, Token = token, IsAdmin = user.IsAdmin, IsConfirmed = user.IsConfirmed, isVip = user.IsVip}; 52 61 } 53 62 … … 75 84 public async Task<AuthenticateResponse> Register(CreateUserRequest req, bool isFirst) 76 85 { 77 User user = new User() { Email = req.Email, Password = req.Password, IsAdmin = isFirst, IsConfirmed = false }; 86 var exists = await _context.Users.FirstOrDefaultAsync(x => x.Email == req.Email); 87 if(exists != null && req.IsConfirmed) 88 { 89 return new AuthenticateResponse { Email = exists.Email, Id = exists.Id, IsAdmin = exists.IsAdmin, IsConfirmed = true }; 90 } 91 User user = new User() { Email = req.Email, Password = req.Password, IsAdmin = isFirst, IsConfirmed = req.IsConfirmed, IsVip = false }; 78 92 await _context.Users.AddAsync(user); 79 93 await _context.SaveChangesAsync(); 80 94 var token = generateJwtToken(user); 81 return new AuthenticateResponse { Email = user.Email, Id = user.Id, Token = token, IsAdmin = user.IsAdmin, IsConfirmed = false};95 return new AuthenticateResponse { Email = user.Email, Id = user.Id, Token = token, IsAdmin = user.IsAdmin, IsConfirmed = req.IsConfirmed, isVip = user.IsVip }; 82 96 } 83 97 … … 125 139 } 126 140 141 public async Task<List<UserResponse>> GetUsers() 142 { 143 return await _context.Users.Select(x => new UserResponse() 144 { 145 Email = x.Email, 146 Id = x.Id, 147 IsVip = x.IsVip 148 }).OrderBy(x => x.Id).ToListAsync(); 149 } 150 151 public async Task UpdateVipStatus(int id, bool isVip) 152 { 153 var user = await _context.Users.FindAsync(id); 154 user.IsVip = isVip; 155 _context.Users.Update(user); 156 await _context.SaveChangesAsync(); 157 } 158 127 159 private string generateJwtToken(User user) 128 160 {
Note:
See TracChangeset
for help on using the changeset viewer.