Browse Source

MeetingUsers ids

zinab_elgendy 7 months ago
parent
commit
8b362d1481

+ 1 - 1
MTWorkHR.API/Program.cs

@@ -20,7 +20,7 @@ var config = new AppSettingsConfiguration();
 // Add services to the container.
 builder.Services.AddDbContext<HRDataContext>(options =>
 {   
-    options.UseSqlServer(config.ConnectionStrings.MTWorkHRConnectionString);
+    options.UseSqlServer(config.ConnectionStrings.LocalConnectionString);
   //  options.UseSqlServer(builder.Configuration.GetSection("ConnectionStrings:MTWorkHRConnectionString").Value);
 });
 

+ 2 - 0
MTWorkHR.Application/Dtos/User/MeetingDto.cs

@@ -22,6 +22,8 @@ namespace MTWorkHR.Application.Models
         public string? Location { get; set; }
         [MaxLength(250)]
         public string? MeetingLink { get; set; }
+        public List<string>? MeetingUserIds { get; set; }
+
         public List<MeetingUserDto>? MeetingUsers { get; set; }
         public string? CreateUser { get; set; }
 

+ 1 - 0
MTWorkHR.Application/Services/Interfaces/IUserService.cs

@@ -15,6 +15,7 @@ namespace MTWorkHR.Application.Identity
         Task<List<UserDto>> GetAllEmployees();
 
         Task<UserDto> GetUserById(string id);
+        Task<string> GetUserFullName(string userId);
         Task Delete(string id);
         Task<UserDto> Create(UserDto input);
         Task<UserUpdateDto> Update(UserUpdateDto input);

+ 85 - 15
MTWorkHR.Application/Services/User/MeetingService.cs

@@ -1,43 +1,113 @@
-using Microsoft.AspNetCore.Identity;
-using Microsoft.AspNetCore.WebUtilities;
-using Microsoft.EntityFrameworkCore;
-using Microsoft.Extensions.Configuration;
+using Microsoft.EntityFrameworkCore;
 using MTWorkHR.Application.Identity;
 using MTWorkHR.Application.Mapper;
 using MTWorkHR.Application.Models;
 using MTWorkHR.Core.Global;
-using MTWorkHR.Core.IRepositories;
 using MTWorkHR.Core.UnitOfWork;
-using MTWorkHR.Infrastructure.Entities;
 using MTWorkHR.Application.Services.Interfaces;
-using MTWorkHR.Core.Email;
 using MTWorkHR.Core.Entities;
-using MTWorkHR.Infrastructure.UnitOfWorks;
+using System.Linq.Dynamic.Core;
 
 namespace MTWorkHR.Application.Services
 {
     public class MeetingService : BaseService<Meeting, MeetingDto, MeetingDto>, IMeetingService
     {
         private readonly IUnitOfWork _unitOfWork;
+        private readonly IUserService _userService;
 
-        public MeetingService(IUnitOfWork unitOfWork):base(unitOfWork)
+        public MeetingService(IUnitOfWork unitOfWork, IUserService userService) : base(unitOfWork)
         {
             _unitOfWork = unitOfWork;
+            _userService = userService;
         }
 
         public override async Task<MeetingDto> GetById(long id)
         {
             var entity = await _unitOfWork.Meeting.GetByIdWithAllChildren(id);
             var response = MapperObject.Mapper.Map<MeetingDto>(entity);
+            if (response != null)
+                response.MeetingUserIds = response.MeetingUsers != null ? response.MeetingUsers?.Select(u => u.AssignedUserId).ToList() : new List<string>();
             return response;
         }
+       
+        public override async Task<MeetingDto> Create(MeetingDto input)
+        {
+            var entity = MapperObject.Mapper.Map<Meeting>(input);
 
-        //public override async Task<List<MeetingDto>> GetAll()
-        //{
-        //    var Meetings = await _unitOfWork.Meeting.GetAllAsync();
-        //    var response = MapperObject.Mapper.Map<List<MeetingDto>>(Meetings);
-        //    return response;
-        //}
+            if (entity is null)
+            {
+                throw new AppException(ExceptionEnum.MapperIssue);
+            }
+            entity.MeetingUsers = input.MeetingUserIds?.Select(s => new MeetingUser { AssignedUserId = s, AssignedUserName = _userService.GetUserFullName(s).Result }).ToList();
+
+            var team = await _unitOfWork.Meeting.AddAsync(entity);
+            await _unitOfWork.CompleteAsync();
+
+            var response = MapperObject.Mapper.Map<MeetingDto>(team);
+            response.MeetingUserIds = input.MeetingUserIds;
+            return response;
+        }
+
+        public override async Task<MeetingDto> Update(MeetingDto input)
+        {
+            var entity = await _unitOfWork.Meeting.GetByIdWithAllChildren(input.Id);
+            if (entity is null)
+                throw new AppException(ExceptionEnum.RecordNotExist);
+            entity.Description = input.Description;
+            entity.Location = input.Location;
+            entity.Title = input.Title;
+            entity.MeetingLink = input.MeetingLink;
+            entity.StartDate = input.StartDate;
+            entity.EndDate = input.EndDate;
+            var oldUsers = entity.MeetingUsers?.Select(s => s.AssignedUserId);
+
+            var NewItems = input.MeetingUserIds?.Where(u => !oldUsers.Contains(u));
+            var newMeetingUsers = NewItems?.Select(s => new MeetingUser { MeetingId = input.Id, AssignedUserId = s, AssignedUserName = _userService.GetUserFullName(s).Result }).ToList();
+            var DeletedItems = oldUsers?.Where(u => !input.MeetingUserIds.Contains(u));
+            _unitOfWork.BeginTran();
+            foreach (var delUser in DeletedItems)
+            {
+                var removeItem = entity.MeetingUsers?.FirstOrDefault(u => u.AssignedUserId == delUser);
+                if (removeItem != null) await _unitOfWork.MeetingUser.DeleteAsync(removeItem);
+            }
+            foreach (var newUsr in newMeetingUsers)
+            {
+                await _unitOfWork.MeetingUser.AddAsync(newUsr);
+            }
+
+            await _unitOfWork.CompleteAsync();
+            _unitOfWork.CommitTran();
+
+            var response = Mapper.MapperObject.Mapper.Map<MeetingDto>(entity);
+            return response;
+        }
+        public override async Task<PagingResultDto<MeetingDto>> GetAll(PagingInputDto PagingInputDto)
+        {
+            var res = await _unitOfWork.Meeting.GetAllWithChildrenAsync();
+            var query = res.Item1;
+            if (PagingInputDto.Filter != null)
+            {
+                var filter = PagingInputDto.Filter;
+                query = query.Where(u => u.Title.Contains(filter) || u.Description.Contains(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();
+
+            var list = MapperObject.Mapper
+                .Map<IList<MeetingDto>>(await page.ToListAsync());
+
+            var response = new PagingResultDto<MeetingDto>
+            {
+                Result = list,
+                Total = total
+            };
+
+            return response;
+        }
 
         //public override async Task Delete(long id)
         //{

+ 10 - 10
MTWorkHR.Application/Services/User/TeamService.cs

@@ -72,7 +72,7 @@ namespace MTWorkHR.Application.Services
             {
                 throw new AppException(ExceptionEnum.MapperIssue);
             }
-            entity.TeamUsers = input.TeamUserIds?.Select(s => new TeamUser { AssignedUserId = s,AssignedUserName = GetUserName(s).Result, IsAdmin = false }).ToList();
+            entity.TeamUsers = input.TeamUserIds?.Select(s => new TeamUser { AssignedUserId = s,AssignedUserName = _userService.GetUserFullName(s).Result, IsAdmin = false }).ToList();
             
             var team = await _unitOfWork.Team.AddAsync(entity);
             await _unitOfWork.CompleteAsync();
@@ -81,12 +81,12 @@ namespace MTWorkHR.Application.Services
             response.TeamUserIds = input.TeamUserIds;
             return response;
         }
-        private async Task<string> GetUserName(string userId)
-        {
-            var entity = await _userService.GetUserById(userId);
-            var name = entity == null ? "" : entity.FirstName + " " + entity.LastName;
-            return name;
-        }
+        //private async Task<string> GetUserName(string userId)
+        //{
+        //    var entity = await _userService.GetUserById(userId);
+        //    var name = entity == null ? "" : entity.FirstName + " " + entity.LastName;
+        //    return name;
+        //}
         public override async Task<TeamDto> Update(TeamDto input)
         {
             var entity = await _unitOfWork.Team.GetByIdWithAllChildren(input.Id);
@@ -97,17 +97,17 @@ namespace MTWorkHR.Application.Services
             var oldUsers = entity.TeamUsers?.Select(s => s.AssignedUserId);
 
             var NewItems = input.TeamUserIds?.Where(u => !oldUsers.Contains(u));
-            var newTeamUsers = NewItems?.Select(s => new TeamUser {TeamId = input.Id, AssignedUserId = s, AssignedUserName = GetUserName(s).Result, IsAdmin = false }).ToList();
+            var newTeamUsers = NewItems?.Select(s => new TeamUser {TeamId = input.Id, AssignedUserId = s, AssignedUserName = _userService.GetUserFullName(s).Result, IsAdmin = false }).ToList();
             var DeletedItems = oldUsers?.Where(u => !input.TeamUserIds.Contains(u));
             _unitOfWork.BeginTran();
             foreach (var delUser in DeletedItems)
             {
                 var removeItem = entity.TeamUsers?.FirstOrDefault(u => u.AssignedUserId == delUser);
-                if(removeItem != null) _unitOfWork.TeamUser.DeleteAsync(removeItem);
+                if(removeItem != null) await _unitOfWork.TeamUser.DeleteAsync(removeItem);
             }
             foreach (var newUsr in newTeamUsers)
             {
-                _unitOfWork.TeamUser.AddAsync(newUsr);
+                await _unitOfWork.TeamUser.AddAsync(newUsr);
             }
 
             await _unitOfWork.CompleteAsync();

+ 6 - 0
MTWorkHR.Application/Services/User/UserService.cs

@@ -77,6 +77,12 @@ namespace MTWorkHR.Application.Services
 
             return response;
         }
+        public async Task<string> GetUserFullName(string userId)
+        {
+            var entity = await GetUserById(userId);
+            var name = entity == null ? "" : entity.FirstName + " " + entity.LastName;
+            return name;
+        }
 
         //public async Task<List<UserDto>> GetAll(PagingInputDto pagingInput)
         //{

+ 1 - 0
MTWorkHR.Core/Entities/User/MeetingUser.cs

@@ -17,5 +17,6 @@ namespace MTWorkHR.Core.Entities
         public Meeting? Meeting { get; set; }
         [Required]
         public string AssignedUserId { get; set; }
+        public string AssignedUserName { get; set; }
     }
 }

+ 1 - 0
MTWorkHR.Core/IRepositories/User/IMeetingRepository.cs

@@ -11,5 +11,6 @@ namespace MTWorkHR.Core.IRepositories
     public interface IMeetingRepository : IRepository<Meeting>
     {
         Task<Meeting> GetByIdWithAllChildren(long id);
+        Task<Tuple<IQueryable<Meeting>, int>> GetAllWithChildrenAsync();
     }
 }

+ 21 - 0
MTWorkHR.Core/IRepositories/User/IMeetingUserRepository.cs

@@ -0,0 +1,21 @@
+using MTWorkHR.Core.Entities;
+using MTWorkHR.Core.IDto;
+using MTWorkHR.Core.IRepositories.Base;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MTWorkHR.Core.IRepositories
+{
+    public interface IMeetingUserRepository : IRepository<MeetingUser>
+    {
+        Task<Tuple<ICollection<MeetingUser>, int>> GetAllAsync(IPagingInputDto pagingInputDto);
+        Task<MeetingUser> GetByIdAsync(long id);
+        Task<MeetingUser> AddAsync(MeetingUser entity);
+        Task<IList<MeetingUser>> AddRangeAsync(IList<MeetingUser> entity);
+        Task DeleteAsync(MeetingUser entity);
+        Task DeleteAsync(IEnumerable<MeetingUser> entities);
+    }
+}

+ 2 - 0
MTWorkHR.Core/IUnitOfWork/IUnitOfWork.cs

@@ -18,6 +18,8 @@ namespace MTWorkHR.Core.UnitOfWork
         ITeamUserRepository TeamUser{ get; }
         ITeamRepository Team { get; }
         IMeetingRepository Meeting{ get; }
+        IMeetingUserRepository MeetingUser { get; }
+
         IAttendanceRepository Attendance { get; }
         IOrderTypeRepository OrderType { get; }
         IOrderRequestRepository OrderRequest { get; }

+ 3 - 2
MTWorkHR.Infrastructure/InfrastructureServiceRegistration.cs

@@ -28,10 +28,10 @@ namespace MTWorkHR.Infrastructure
         {
             services.AddSingleton(config);
 
-            var vv = config.ConnectionStrings.MTWorkHRConnectionString;
+            
             services.AddDbContext<HRDataContext>(options =>
                 options.UseSqlServer(
-                    config.ConnectionStrings.MTWorkHRConnectionString  //configuration.GetSection("ConnectionString:MTWorkHRConnectionString").Value
+                    config.ConnectionStrings.LocalConnectionString  //configuration.GetSection("ConnectionString:MTWorkHRConnectionString").Value
                     ));
            
             services.AddIdentity<ApplicationUser, ApplicationRole>().AddEntityFrameworkStores<HRDataContext>().AddDefaultTokenProviders();
@@ -68,6 +68,7 @@ namespace MTWorkHR.Infrastructure
             services.AddScoped(typeof(ITeamRepository), typeof(TeamRepository));
             services.AddScoped(typeof(ITeamUserRepository), typeof(TeamUserRepository));
             services.AddScoped(typeof(IMeetingRepository), typeof(MeetingRepository));
+            services.AddScoped(typeof(IMeetingUserRepository), typeof(MeetingUserRepository));
             services.AddScoped(typeof(IAttendanceRepository), typeof(AttendanceRepository));
             services.AddScoped(typeof(IOrderAllocationRepository), typeof(OrderAllocationRepository));
             services.AddScoped(typeof(IOrderRequestRepository), typeof(OrderRequestRepository));

+ 6 - 2
MTWorkHR.Infrastructure/Migrations/20240625073053_addAttachmentType.Designer.cs

@@ -12,8 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
 namespace MTWorkHR.Infrastructure.Migrations
 {
     [DbContext(typeof(HRDataContext))]
-    [Migration("20240625073053_addAttachmentType")]
-    partial class addAttachmentType
+    [Migration("20240625091145_addAttachtype")]
+    partial class addAttachtype
     {
         /// <inheritdoc />
         protected override void BuildTargetModel(ModelBuilder modelBuilder)
@@ -1226,6 +1226,10 @@ namespace MTWorkHR.Infrastructure.Migrations
                         .IsRequired()
                         .HasColumnType("nvarchar(max)");
 
+                    b.Property<string>("AssignedUserName")
+                        .IsRequired()
+                        .HasColumnType("nvarchar(max)");
+
                     b.Property<DateTime>("CreateDate")
                         .HasColumnType("datetime2")
                         .HasColumnOrder(3);

+ 12 - 1
MTWorkHR.Infrastructure/Migrations/20240625073053_addAttachmentType.cs

@@ -5,11 +5,18 @@
 namespace MTWorkHR.Infrastructure.Migrations
 {
     /// <inheritdoc />
-    public partial class addAttachmentType : Migration
+    public partial class addAttachtype : Migration
     {
         /// <inheritdoc />
         protected override void Up(MigrationBuilder migrationBuilder)
         {
+            migrationBuilder.AddColumn<string>(
+                name: "AssignedUserName",
+                table: "MeetingUser",
+                type: "nvarchar(max)",
+                nullable: false,
+                defaultValue: "");
+
             migrationBuilder.InsertData(
                 table: "AttachmentTypes",
                 columns: new[] { "Id", "IsRequired", "NameAr", "NameEn" },
@@ -23,6 +30,10 @@ namespace MTWorkHR.Infrastructure.Migrations
                 table: "AttachmentTypes",
                 keyColumn: "Id",
                 keyValue: 9L);
+
+            migrationBuilder.DropColumn(
+                name: "AssignedUserName",
+                table: "MeetingUser");
         }
     }
 }

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

@@ -1223,6 +1223,10 @@ namespace MTWorkHR.Infrastructure.Migrations
                         .IsRequired()
                         .HasColumnType("nvarchar(max)");
 
+                    b.Property<string>("AssignedUserName")
+                        .IsRequired()
+                        .HasColumnType("nvarchar(max)");
+
                     b.Property<DateTime>("CreateDate")
                         .HasColumnType("datetime2")
                         .HasColumnOrder(3);

+ 9 - 0
MTWorkHR.Infrastructure/Repositories/User/MeetingRepository.cs

@@ -21,5 +21,14 @@ namespace MTWorkHR.Infrastructure.Repositories
                 .Include(x => x.MeetingUsers)
                 .FirstOrDefaultAsync(x => x.Id == id);
         }
+
+
+        public async Task<Tuple<IQueryable<Meeting>, int>> GetAllWithChildrenAsync()
+        {
+            var query = dbSet.Include(x => x.MeetingUsers).AsQueryable();
+            var total = await query.CountAsync();
+
+            return new Tuple<IQueryable<Meeting>, int>(query, total);
+        }
     }
 }

+ 41 - 0
MTWorkHR.Infrastructure/Repositories/User/MeetingUserRepository.cs

@@ -0,0 +1,41 @@
+using Microsoft.EntityFrameworkCore;
+using MTWorkHR.Core.Entities;
+using MTWorkHR.Core.IDto;
+using MTWorkHR.Core.IRepositories;
+using MTWorkHR.Infrastructure.Entities;
+using MTWorkHR.Infrastructure.DBContext;
+
+namespace MTWorkHR.Infrastructure.Repositories
+{
+    public class MeetingUserRepository : Repository<MeetingUser>, IMeetingUserRepository
+    {
+        private readonly DbSet<MeetingUser> dbSet;
+
+        public MeetingUserRepository(HRDataContext context) : base(context)
+        {
+            dbSet = context.Set<MeetingUser>();
+
+        }
+        public virtual async Task<MeetingUser> AddAsync(MeetingUser entity)
+        {
+            await dbSet.AddAsync(entity);
+            return entity;
+        }
+
+        public virtual async Task<IList<MeetingUser>> AddRangeAsync(IList<MeetingUser> entity)
+        {
+            await dbSet.AddRangeAsync(entity);
+            return entity;
+        }
+
+        public virtual async Task DeleteAsync(MeetingUser entity)
+        {
+            dbSet.Remove(entity);
+        }
+
+        public virtual async Task DeleteAsync(IEnumerable<MeetingUser> entities)
+        {
+            dbSet.RemoveRange(entities);
+        }
+    }
+}

+ 4 - 0
MTWorkHR.Infrastructure/UnitOfWork/UnitOfWork.cs

@@ -24,6 +24,7 @@ namespace MTWorkHR.Infrastructure.UnitOfWorks
         public ITeamRepository Team{ get; }
         public ITeamUserRepository TeamUser { get; }
         public IMeetingRepository Meeting{ get; }
+        public IMeetingUserRepository MeetingUser { get; }
         public IAttendanceRepository Attendance { get; }
         public IOrderTypeRepository OrderType { get; }
         public ILeaveTypeRepository LeaveType { get; }
@@ -60,6 +61,8 @@ namespace MTWorkHR.Infrastructure.UnitOfWorks
             , ILoginOTPRepository loginOTP
             , ICityRepository city
             , ITeamUserRepository teamUser
+            , IMeetingUserRepository meetingUser
+
             )
         {
             context = _context;
@@ -85,6 +88,7 @@ namespace MTWorkHR.Infrastructure.UnitOfWorks
             LoginOTP = loginOTP;
             City = city;
             TeamUser = teamUser;
+            MeetingUser = meetingUser;
         }
 
         public async Task<int> CompleteAsync()