Browse Source

Login : call GetUser

zinab_elgendy 7 months ago
parent
commit
7f6b9ead18

+ 5 - 4
MTWorkHR.Application/Dtos/Identity/AuthResponse.cs

@@ -9,10 +9,11 @@ namespace MTWorkHR.Application.Models
 {
     public class AuthResponse
     {
-        public string Id { get; set; }
-        public string UserName { get; set; }
-        public string Email { get; set; }
-        public int UserTypeId { get; set; }
+  //      public string Id { get; set; }
+      //  public string UserName { get; set; }
+       // public string Email { get; set; }
+   //     public int UserTypeId { get; set; }
+        public UserDto User { get; set; }
         public string Token{ get; set; }
         public DateTime Expiration { get; set; }
     }

+ 1 - 1
MTWorkHR.Application/Dtos/User/TeamDto.cs

@@ -16,7 +16,7 @@ namespace MTWorkHR.Application.Models
         [MaxLength(250)]
         [Filter]
         public string NameEn { get; set; }
-        public List<TeamUserDto>? TeamUsers { get; set; }
+        public List<string>? TeamUsers { get; set; }
         public string? CreateUser { get; set; }
 
     }

+ 17 - 7
MTWorkHR.Application/Services/Auth/AuthService.cs

@@ -1,10 +1,13 @@
 using Microsoft.AspNetCore.Identity;
+using Microsoft.EntityFrameworkCore;
 using Microsoft.Extensions.Options;
 using Microsoft.IdentityModel.Tokens;
 using MTWorkHR.Application.Exceptions;
 using MTWorkHR.Application.Filters;
 using MTWorkHR.Application.Identity;
+using MTWorkHR.Application.Mapper;
 using MTWorkHR.Application.Models;
+using MTWorkHR.Application.Services.Interfaces;
 using MTWorkHR.Core.Global;
 using MTWorkHR.Infrastructure.Entities;
 using System.IdentityModel.Tokens.Jwt;
@@ -18,16 +21,19 @@ namespace MTWorkHR.Identity.Services
         private readonly UserManager<ApplicationUser> _userManager;
         private readonly SignInManager<ApplicationUser> _signInManager;
         private readonly AppSettingsConfiguration _configuration;
-        public AuthService(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager, AppSettingsConfiguration configuration) 
+        private readonly IUserService _userService;
+
+        public AuthService(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager, AppSettingsConfiguration configuration, IUserService userService) 
         {
             _userManager = userManager;
             _signInManager = signInManager;
             _configuration = configuration;
+            _userService = userService;
         }
         public async Task<AuthResponse> Login(AuthRequest request)
         {
-            var user = await _userManager.FindByEmailAsync(request.Email);
-            if(user == null)
+            var user = await _userManager.FindByEmailAsync(request.Email);           
+            if (user == null)
             {
                 throw new AppException(ExceptionEnum.RecordNotExist);
             }
@@ -36,13 +42,17 @@ namespace MTWorkHR.Identity.Services
             {
                 throw new AppException($"Credentials for '{request.Email} are not valid'.");
             }
+            var userResponse = await _userService.GetById(user.Id);
+
             JwtSecurityToken jwtToken = await GenerateToken(user);
+
             var response = new AuthResponse
             {
-                Id = user.Id,
-                Email = user.Email,
-                UserName = user.UserName,
-                UserTypeId = user.UserType,
+                User = userResponse,
+                //Id = user.Id,
+                //Email = user.Email,
+                //UserName = user.UserName,
+                //UserTypeId = user.UserType,
                 Token = new JwtSecurityTokenHandler().WriteToken( jwtToken),
                 Expiration = jwtToken.ValidTo
             };

+ 14 - 1
MTWorkHR.Application/Services/User/TeamService.cs

@@ -32,7 +32,20 @@ namespace MTWorkHR.Application.Services
             var response = MapperObject.Mapper.Map<TeamDto>(entity);
             return response;
         }
-       
+        public override async Task<TeamDto> Create(TeamDto input)
+        {
+            var entity = MapperObject.Mapper.Map<Team>(input);
+            if (entity is null)
+            {
+                throw new AppException(ExceptionEnum.MapperIssue);
+            }
+
+            var task = await _unitOfWork.Team.AddAsync(entity);
+            await _unitOfWork.CompleteAsync();
+
+            var response = MapperObject.Mapper.Map<TeamDto>(task);
+            return response;
+        }
         public async Task<bool> AssignAdminManager(TeamUserDto teamUser)
         {
             var result = false;