Bläddra i källkod

UserAddress full data

zinab_elgendy 7 månader sedan
förälder
incheckning
3bf8b39f63

+ 1 - 1
MTWorkHR.API/Controllers/CompanyController.cs

@@ -11,7 +11,7 @@ namespace MTWorkHR.API.Controllers
 {
     [Route("api/[controller]")]
     [ApiController]
-    [AppAuthorize]
+    //[AppAuthorize]
     public class CompanyController : ControllerBase
     {
         private readonly ICompanyService _companyService;

+ 32 - 0
MTWorkHR.API/Controllers/LogController.cs

@@ -0,0 +1,32 @@
+using Microsoft.AspNetCore.Authorization;
+
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using MTWorkHR.Application.Filters;
+using MTWorkHR.Application.Identity;
+using MTWorkHR.Application.Models;
+using MTWorkHR.Application.Services;
+using MTWorkHR.Application.Services.Interfaces;
+using MTWorkHR.Core.Entities;
+
+namespace MTWorkHR.API.Controllers
+{
+    [Route("api/[controller]")]
+    [ApiController]
+    //[AppAuthorize]
+    public class LogController : ControllerBase
+    {
+        private readonly ILogService<UserLog> _logService;
+        public LogController(ILogService<UserLog> CompanyService) 
+        {
+            this._logService = CompanyService;
+        }
+        [HttpGet("GetAll")]
+        public async Task<ActionResult<List<UserLog>>> GetAll([FromQuery] PagingInputDto pagingInput)
+        {
+            return Ok( await _logService.GetAll(pagingInput));
+        }
+       
+       
+    }
+}

+ 2 - 0
MTWorkHR.Application/ApplicationServiceRegistration.cs

@@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection;
 using MTWorkHR.Application.Identity;
 using MTWorkHR.Application.Services;
 using MTWorkHR.Application.Services.Interfaces;
+using MTWorkHR.Core.Entities;
 using MTWorkHR.Core.Global;
 using MTWorkHR.Identity.Services;
 using System.Reflection;
@@ -34,6 +35,7 @@ namespace MTWorkHR.Application
             services.AddScoped<ILookupService, LookupService>();
             services.AddScoped<ICompanyService, CompanyService>();
             services.AddScoped<IOTPService, OTPService>();
+            services.AddScoped<ILogService<UserLog>, LogService<UserLog>>();
 
             return services;
         }

+ 3 - 1
MTWorkHR.Application/Dtos/Identity/UserAddressDto.cs

@@ -6,7 +6,9 @@ namespace MTWorkHR.Application.Models
     public class UserAddressDto : EntityDto
     {
         public int? CountryId { get; set; }
-        public string? City { get; set; }
+        public int? CityId { get; set; }
+        public string? CityName { get; set; }
+        public string? CountryName { get; set; }
         public string? PostalCode { get; set; }
         public string? AddressDesc { 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<string>? TeamUsers { get; set; }
+        public List<string>? TeamUserIds { get; set; }
         public string? CreateUser { get; set; }
 
     }

+ 4 - 1
MTWorkHR.Application/Mapper/MappingProfile.cs

@@ -49,7 +49,10 @@ namespace MTWorkHR.Application.Mapper
             CreateMap<ApplicationUser, UserUpdateDto>();
 
             CreateMap<AttachmentDto, UserAttachment>().ReverseMap();
-            CreateMap<UserAddress, UserAddressDto>().ReverseMap();
+            CreateMap<UserAddress, UserAddressDto>()
+              .ForMember(s => s.CountryName, o => o.MapFrom(s => s.Country == null ? "" : GlobalInfo.lang == "ar" ? s.Country.NameAr : s.Country.NameEn))
+              .ForMember(s => s.CityName, o => o.MapFrom(s => s.City == null ? "" : GlobalInfo.lang == "ar" ? s.City.NameAr : s.City.NameEn)
+              ).ReverseMap();
 
             //identity userRoles
             CreateMap<IdentityUserRole<string>, UserRoleDto>().ReverseMap();

+ 18 - 3
MTWorkHR.Application/Services/Base/LogService.cs

@@ -7,20 +7,22 @@ using System.Reflection;
 using MTWorkHR.Application.Services.Interfaces;
 using MTWorkHR.Core.UnitOfWork;
 using MTWorkHR.Core.IRepositories.Base;
+using MTWorkHR.Application.Models;
+using MTWorkHR.Core.Entities;
 
 namespace MTWorkHR.Application.Services
 {
-    public class LogService<TEntity>: ILogService<TEntity> where TEntity : class
+    public class LogService<TEntity> : ILogService<TEntity> where TEntity : class 
     {
         private readonly IUnitOfWorkLog unitOfWork;
 
-        //private readonly IRepository<TEntity> repository;
+        private readonly IRepositoryLog<TEntity> repository;
 
         public LogService(IUnitOfWorkLog _unitOfWork)
         {
             unitOfWork = _unitOfWork;
             //this.repository = repository;
-            //repository = (IRepository<TEntity>)unitOfWork.GetRepositoryByName(typeof(TEntity).Name);
+            repository = (IRepositoryLog<TEntity>)unitOfWork.GetRepositoryByName(typeof(TEntity).Name);
         }
 
 
@@ -33,6 +35,19 @@ namespace MTWorkHR.Application.Services
 
             await unitOfWork.CompleteAsync();
         }
+         public virtual async Task<PagingResultDto<TEntity>> GetAll(PagingInputDto pagingInputDto)
+        {
+            var result = await repository.GetAllAsync(pagingInputDto);
+
+            var list = Mapper.MapperObject.Mapper.Map<IList<TEntity>>(result.Item1);
 
+            var response = new PagingResultDto<TEntity>
+            {
+                Result = list,
+                Total = result.Item2
+            };
+
+            return response;
+        }
     }
 }

+ 6 - 2
MTWorkHR.Application/Services/Interfaces/ILogService.cs

@@ -1,4 +1,6 @@
-using System;
+using MTWorkHR.Application.Models;
+using MTWorkHR.Core.Entities;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -6,8 +8,10 @@ using System.Threading.Tasks;
 
 namespace MTWorkHR.Application.Services.Interfaces
 {
-    public interface ILogService<TEntity>
+    public interface ILogService<TEntity> 
+        where TEntity : class
     {
         Task Create(TEntity input);
+        Task<PagingResultDto<TEntity>> GetAll(PagingInputDto pagingInputDto);
     }
 }

+ 12 - 0
MTWorkHR.Application/Services/Interfaces/IUserLogService.cs

@@ -0,0 +1,12 @@
+
+using MTWorkHR.Application.Dtos.Log;
+using MTWorkHR.Application.Models;
+using MTWorkHR.Core.Entities;
+using System.Threading.Tasks;
+
+namespace MTWorkHR.Application.Services.Interfaces
+{
+    public interface IUserLogService : IService<UserLog, UserLogDto, UserLogDto>
+    {
+    }
+}

+ 34 - 2
MTWorkHR.Application/Services/User/TeamService.cs

@@ -13,6 +13,7 @@ using MTWorkHR.Application.Services.Interfaces;
 using MTWorkHR.Core.Email;
 using MTWorkHR.Core.Entities;
 using MTWorkHR.Infrastructure.UnitOfWorks;
+using System.Linq;
 
 namespace MTWorkHR.Application.Services
 {
@@ -35,17 +36,48 @@ namespace MTWorkHR.Application.Services
         public override async Task<TeamDto> Create(TeamDto input)
         {
             var entity = MapperObject.Mapper.Map<Team>(input);
+            
             if (entity is null)
             {
                 throw new AppException(ExceptionEnum.MapperIssue);
             }
+            entity.TeamUsers = input.TeamUserIds?.Select(s => new TeamUser { AssignedUserId = s, IsAdmin = false }).ToList();
+            
+            var team = await _unitOfWork.Team.AddAsync(entity);
+            await _unitOfWork.CompleteAsync();
+
+            var response = MapperObject.Mapper.Map<TeamDto>(team);
+            return response;
+        }
+
+        public override async Task<TeamDto> Update(TeamDto input)
+        {
+            var entity = await _unitOfWork.Team.GetByIdAsync(input.Id);
+            if (entity is null)
+                throw new AppException(ExceptionEnum.RecordNotExist);
+            entity.NameAr = input.NameAr;
+            entity.NameEn = input.NameEn;
+            var oldUsers = entity.TeamUsers?.Select(s => s.AssignedUserId);
+
+            var NewItems = input.TeamUserIds?.Where(u => !oldUsers.Contains(u));
+            var newTeamUsers = NewItems?.Select(s => new TeamUser { AssignedUserId = s, IsAdmin = false }).ToList();
+            var DeletedItems = oldUsers?.Where(u => !input.TeamUserIds.Contains(u));
+            foreach (var delUser in DeletedItems)
+            {
+                var removeItem = entity.TeamUsers?.FirstOrDefault(u => u.AssignedUserId == delUser);
+                if(removeItem != null) entity.TeamUsers.Remove(removeItem);
+            }
+            foreach (var newUsr in newTeamUsers)
+            {
+                entity.TeamUsers.Add(newUsr);
+            }
 
-            var task = await _unitOfWork.Team.AddAsync(entity);
             await _unitOfWork.CompleteAsync();
 
-            var response = MapperObject.Mapper.Map<TeamDto>(task);
+            var response = Mapper.MapperObject.Mapper.Map<TeamDto>(entity);
             return response;
         }
+
         public async Task<bool> AssignAdminManager(TeamUserDto teamUser)
         {
             var result = false;

+ 2 - 1
MTWorkHR.Application/Services/User/UserService.cs

@@ -56,7 +56,8 @@ namespace MTWorkHR.Application.Services
         {
             var entity = await _userManager.Users
                 .Include(x => x.UserRoles)
-                .Include(x => x.UserAddress)
+                .Include(x => x.UserAddress).ThenInclude(x=> x.City)
+                .Include(x => x.UserAddress).ThenInclude(x=> x.Country)
                 .Include(x => x.UserAttachments)
                 .Include(x => x.JobTitle)
                 .Include(x => x.Industry)

+ 4 - 1
MTWorkHR.Core/IRepositories/Base/IRepositoryLog.cs

@@ -1,4 +1,5 @@
-using System;
+using MTWorkHR.Core.IDto;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -13,5 +14,7 @@ namespace MTWorkHR.Core.IRepositories.Base
         Task<T> AddAsync(T entity);
         Task DeleteAsync(T entity);
         IQueryable<T> AsQueryable();
+        Task<Tuple<ICollection<T>, int>> GetAllAsync(IPagingInputDto pagingInputDto);
+
     }
 }

+ 10 - 0
MTWorkHR.Infrastructure/Configurations/RoleConfiguration.cs

@@ -34,6 +34,16 @@ namespace MTWorkHR.Infrastructure.Configurations
                     IsDeleted = false,
 
                 }
+                ,
+                new ApplicationRole
+                {
+                    Id = "CO5B3B92-2311-48F8-9DEC-F9FAEF1F211R",
+                    Name = "Contractor",
+                    NormalizedName = "CONTRACTOR",
+                    IsAdmin = false,
+                    IsDeleted = false,
+
+                }
 
                 );
         }

+ 9 - 2
MTWorkHR.Infrastructure/Entities/UserAddress.cs

@@ -1,6 +1,7 @@
 using MTWorkHR.Core.Entities.Base;
 using System.ComponentModel.DataAnnotations.Schema;
 using System.ComponentModel.DataAnnotations;
+using MTWorkHR.Core.Entities;
 
 namespace MTWorkHR.Infrastructure.Entities
 {
@@ -10,9 +11,15 @@ namespace MTWorkHR.Infrastructure.Entities
 
         [ForeignKey("UserId")]
         public ApplicationUser User { get; set; }
+      
+        public long? CountryId { get; set; }
+        public long? CityId{ get; set; }
 
-        public int? CountryId { get; set; }
-        public string? City{ get; set; }
+        [ForeignKey("CountryId")]
+        public CountryLookup? Country { get; set; }
+
+        [ForeignKey("CityId")]
+        public City? City { get; set; }
         public string? PostalCode { get; set; }
         public string? AddressDesc{ get; set; }
     }

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 3315 - 0
MTWorkHR.Infrastructure/Migrations/20240604083451_altrAddress.Designer.cs


+ 96 - 0
MTWorkHR.Infrastructure/Migrations/20240604083451_altrAddress.cs

@@ -0,0 +1,96 @@
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+namespace MTWorkHR.Infrastructure.Migrations
+{
+    /// <inheritdoc />
+    public partial class altrAddress : Migration
+    {
+        /// <inheritdoc />
+        protected override void Up(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.DropColumn(
+                name: "City",
+                table: "UserAddress");
+
+            migrationBuilder.AlterColumn<long>(
+                name: "CountryId",
+                table: "UserAddress",
+                type: "bigint",
+                nullable: true,
+                oldClrType: typeof(int),
+                oldType: "int",
+                oldNullable: true);
+
+            migrationBuilder.AddColumn<long>(
+                name: "CityId",
+                table: "UserAddress",
+                type: "bigint",
+                nullable: true);
+
+            migrationBuilder.CreateIndex(
+                name: "IX_UserAddress_CityId",
+                table: "UserAddress",
+                column: "CityId");
+
+            migrationBuilder.CreateIndex(
+                name: "IX_UserAddress_CountryId",
+                table: "UserAddress",
+                column: "CountryId");
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_UserAddress_Cities_CityId",
+                table: "UserAddress",
+                column: "CityId",
+                principalTable: "Cities",
+                principalColumn: "Id");
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_UserAddress_CountryLookups_CountryId",
+                table: "UserAddress",
+                column: "CountryId",
+                principalTable: "CountryLookups",
+                principalColumn: "Id");
+        }
+
+        /// <inheritdoc />
+        protected override void Down(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.DropForeignKey(
+                name: "FK_UserAddress_Cities_CityId",
+                table: "UserAddress");
+
+            migrationBuilder.DropForeignKey(
+                name: "FK_UserAddress_CountryLookups_CountryId",
+                table: "UserAddress");
+
+            migrationBuilder.DropIndex(
+                name: "IX_UserAddress_CityId",
+                table: "UserAddress");
+
+            migrationBuilder.DropIndex(
+                name: "IX_UserAddress_CountryId",
+                table: "UserAddress");
+
+            migrationBuilder.DropColumn(
+                name: "CityId",
+                table: "UserAddress");
+
+            migrationBuilder.AlterColumn<int>(
+                name: "CountryId",
+                table: "UserAddress",
+                type: "int",
+                nullable: true,
+                oldClrType: typeof(long),
+                oldType: "bigint",
+                oldNullable: true);
+
+            migrationBuilder.AddColumn<string>(
+                name: "City",
+                table: "UserAddress",
+                type: "nvarchar(max)",
+                nullable: true);
+        }
+    }
+}

+ 20 - 4
MTWorkHR.Infrastructure/Migrations/HRDataContextModelSnapshot.cs

@@ -2779,11 +2779,11 @@ namespace MTWorkHR.Infrastructure.Migrations
                     b.Property<string>("AddressDesc")
                         .HasColumnType("nvarchar(max)");
 
-                    b.Property<string>("City")
-                        .HasColumnType("nvarchar(max)");
+                    b.Property<long?>("CityId")
+                        .HasColumnType("bigint");
 
-                    b.Property<int?>("CountryId")
-                        .HasColumnType("int");
+                    b.Property<long?>("CountryId")
+                        .HasColumnType("bigint");
 
                     b.Property<DateTime>("CreateDate")
                         .HasColumnType("datetime2")
@@ -2812,6 +2812,10 @@ namespace MTWorkHR.Infrastructure.Migrations
 
                     b.HasKey("Id");
 
+                    b.HasIndex("CityId");
+
+                    b.HasIndex("CountryId");
+
                     b.HasIndex("UserId")
                         .IsUnique();
 
@@ -3182,12 +3186,24 @@ namespace MTWorkHR.Infrastructure.Migrations
 
             modelBuilder.Entity("MTWorkHR.Infrastructure.Entities.UserAddress", b =>
                 {
+                    b.HasOne("MTWorkHR.Core.Entities.City", "City")
+                        .WithMany()
+                        .HasForeignKey("CityId");
+
+                    b.HasOne("MTWorkHR.Core.Entities.CountryLookup", "Country")
+                        .WithMany()
+                        .HasForeignKey("CountryId");
+
                     b.HasOne("MTWorkHR.Infrastructure.Entities.ApplicationUser", "User")
                         .WithOne("UserAddress")
                         .HasForeignKey("MTWorkHR.Infrastructure.Entities.UserAddress", "UserId")
                         .OnDelete(DeleteBehavior.Cascade)
                         .IsRequired();
 
+                    b.Navigation("City");
+
+                    b.Navigation("Country");
+
                     b.Navigation("User");
                 });
 

+ 35 - 1
MTWorkHR.Infrastructure/Repositories/RepositoryLog.cs

@@ -1,4 +1,6 @@
 using Microsoft.EntityFrameworkCore;
+using MTWorkHR.Core.Entities.Base;
+using MTWorkHR.Core.IDto;
 using MTWorkHR.Core.IRepositories.Base;
 using MTWorkHR.Infrastructure.DBContext;
 using System;
@@ -8,7 +10,7 @@ using System.Reflection;
 using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
-
+using System.Linq.Dynamic.Core;
 namespace MTWorkHR.Infrastructure.Repositories
 {
     public class RepositoryLog<T>: IRepositoryLog<T> where T : class
@@ -47,5 +49,37 @@ namespace MTWorkHR.Infrastructure.Repositories
         {
             return dbSet.AsQueryable<T>();
         }
+
+
+        public virtual async Task<Tuple<ICollection<T>, int>> GetAllAsync(IPagingInputDto pagingInputDto)
+        {
+            var query = dbSet.AsQueryable();
+
+            if (pagingInputDto.HiddenFilter != null)
+            {
+                query = query.Where(pagingInputDto.HiddenFilter);
+            }
+
+            if (pagingInputDto.Filter != null)
+            {
+                var props = typeof(T).GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(FilterAttribute)));
+                var condition = "";
+                foreach (var p in props)
+                {
+                    condition = (condition == "" ? condition : condition + " || ") + p.Name + ".Contains(@0)";
+                }
+
+                query = query.Where(condition, pagingInputDto.Filter);
+            }
+
+            var order = query.OrderBy(pagingInputDto.OrderByField + " " + pagingInputDto.OrderType);
+
+            var page = order.Skip((pagingInputDto.PageNumber * pagingInputDto.PageSize) - pagingInputDto.PageSize).Take(pagingInputDto.PageSize);
+
+            var total = await query.CountAsync();
+
+            return new Tuple<ICollection<T>, int>(await page.ToListAsync(), total);
+            throw new NotImplementedException();
+        }
     }
 }