| | 1 | | using LGDXRobotCloud.API.Exceptions; |
| | 2 | | using LGDXRobotCloud.Data.Entities; |
| | 3 | | using LGDXRobotCloud.Data.Models.Business.Administration; |
| | 4 | | using LGDXRobotCloud.Data.Models.Business.Identity; |
| | 5 | | using Microsoft.AspNetCore.Identity; |
| | 6 | |
|
| | 7 | | namespace LGDXRobotCloud.API.Services.Identity; |
| | 8 | |
|
| | 9 | | public interface ICurrentUserService |
| | 10 | | { |
| | 11 | | Task<LgdxUserBusinessModel> GetUserAsync(string userId); |
| | 12 | | Task<bool> UpdateUserAsync(string userId, LgdxUserUpdateBusinessModel lgdxUserBusinessModel); |
| | 13 | | } |
| | 14 | |
|
| 0 | 15 | | public class CurrentUserService( |
| 0 | 16 | | UserManager<LgdxUser> userManager |
| 0 | 17 | | ) : ICurrentUserService |
| | 18 | | { |
| 0 | 19 | | private readonly UserManager<LgdxUser> _userManager = userManager ?? throw new ArgumentNullException(nameof(userManage |
| | 20 | |
|
| | 21 | | public async Task<LgdxUserBusinessModel> GetUserAsync(string userId) |
| 0 | 22 | | { |
| 0 | 23 | | var user = await _userManager.FindByIdAsync(userId) |
| 0 | 24 | | ?? throw new LgdxNotFound404Exception(); |
| | 25 | |
|
| 0 | 26 | | return new LgdxUserBusinessModel { |
| 0 | 27 | | Id = Guid.Parse(user.Id!), |
| 0 | 28 | | Name = user.Name ?? string.Empty, |
| 0 | 29 | | UserName = user.UserName ?? string.Empty, |
| 0 | 30 | | Email = user.Email ?? string.Empty, |
| 0 | 31 | | Roles = await _userManager.GetRolesAsync(user), |
| 0 | 32 | | TwoFactorEnabled = user.TwoFactorEnabled, |
| 0 | 33 | | AccessFailedCount = user.AccessFailedCount |
| 0 | 34 | | }; |
| 0 | 35 | | } |
| | 36 | |
|
| | 37 | | public async Task<bool> UpdateUserAsync(string userId, LgdxUserUpdateBusinessModel lgdxUserBusinessModel) |
| 0 | 38 | | { |
| 0 | 39 | | var user = await _userManager.FindByIdAsync(userId) |
| 0 | 40 | | ?? throw new LgdxNotFound404Exception(); |
| | 41 | |
|
| 0 | 42 | | user.Name = lgdxUserBusinessModel.Name; |
| 0 | 43 | | user.Email = lgdxUserBusinessModel.Email; |
| 0 | 44 | | var result = await _userManager.UpdateAsync(user); |
| 0 | 45 | | if (!result.Succeeded) |
| 0 | 46 | | { |
| 0 | 47 | | throw new LgdxIdentity400Expection(result.Errors); |
| | 48 | | } |
| | 49 | |
|
| 0 | 50 | | return true; |
| 0 | 51 | | } |
| | 52 | | } |