Explorar el Código

Chat inprogress

zinab_elgendy hace 1 mes
padre
commit
72219c854e

+ 1 - 1
MTWorkHR.API/Chat/ChatHub.cs

@@ -2,7 +2,7 @@
 
 namespace MTWorkHR.API.Chat
 {
-    public class ChatHub : Hub
+    public class ChatHubxx : Hub
     {
         public async Task SendMessage(string user, string message)
         {

+ 1 - 1
MTWorkHR.API/Program.cs

@@ -33,7 +33,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);
 });
 

+ 34 - 0
MTWorkHR.Application/Chat/Chat.cs

@@ -0,0 +1,34 @@
+using Microsoft.AspNetCore.SignalR;
+using MTWorkHR.Application.Identity;
+using MTWorkHR.Core.Global;
+using MTWorkHR.Core.UnitOfWork;
+
+namespace MTWorkHR.API.Chat
+{
+    public class Chat : Hub
+    {
+        private readonly IUnitOfWork _unitOfWork;
+
+        public Chat(IUnitOfWork unitOfWork) 
+        {
+            _unitOfWork = unitOfWork;
+        }
+        public async Task getOnlineUsers()
+        {
+            //var allConnections = await _unitOfWork.Connection.GetAllAsync();
+            //var currUserId = allConnections.Item1.Where(c => c.SignalrId == Context.ConnectionId).Select(c => c.UserId).SingleOrDefault();
+            //List<User> onlineUsers = allConnections
+            //    .Where(c => c.PersonId != currUserId)
+            //    .Select(c =>
+            //        new UserDto(c.PersonId, ctx.Person.Where(p => p.Id == c.PersonId).Select(p => p.Name).SingleOrDefault(), c.SignalrId)
+            //    ).ToList();
+            //await Clients.Caller.SendAsync("getOnlineUsersResponse", onlineUsers);
+        }
+
+
+        public async Task sendMsg(string connId, string msg)
+        {
+            await Clients.Client(connId).SendAsync("sendMsgResponse", Context.ConnectionId, msg);
+        }
+    }
+}

+ 21 - 0
MTWorkHR.Application/Chat/ChatHub.cs

@@ -0,0 +1,21 @@
+using Microsoft.AspNetCore.SignalR;
+using MTWorkHR.Application.Identity;
+using MTWorkHR.Core.Global;
+using MTWorkHR.Core.UnitOfWork;
+
+namespace MTWorkHR.API.Chat
+{
+    public class ChatHub : Hub
+    {
+        private readonly IUnitOfWork _unitOfWork;
+
+        public ChatHub(IUnitOfWork unitOfWork) 
+        {
+            _unitOfWork = unitOfWork;
+        }
+        public async Task SendMessage(string user, string message)
+        {
+            await Clients.All.SendAsync("ReceiveMessage", user, message);
+        }
+    }
+}

+ 7 - 0
MTWorkHR.Application/Chat/IChatClient.cs

@@ -0,0 +1,7 @@
+namespace MTWorkHR.API.Chat
+{
+    public interface IChatClient
+    {
+        Task ReceiveMessage(string message);
+    }
+}

+ 47 - 66
MTWorkHR.Application/Services/User/UserService.cs

@@ -165,88 +165,69 @@ namespace MTWorkHR.Application.Services
         public virtual async Task<PagingResultDto<UserAllDto>> GetAll(UserPagingInputDto PagingInputDto)
         {
             var query = _userManager.Users
-                .Include(u => u.Qualification).Include(u => u.JobTitle).Include(u => u.University).Include(u => u.Industry).Include(u => u.Country)
-                .Where(e => _globalInfo.CompanyId == null || e.CompanyId != _globalInfo.CompanyId)
-                .AsQueryable();
-            var filter = PagingInputDto.Filter;
-            query = query.Where(u =>
-         u.UserName.Contains(filter) ||
-         u.Email.Contains(filter) ||
-         u.FavoriteName.Contains(filter) ||
-         u.Position.Contains(filter) ||
-         u.PhoneNumber.Contains(filter));
-
-            // Check if the filter contains a space (indicating a full name)
-            if (filter.Contains(" "))
-            {
-                var fullName = filter.Split(" ");
-                var firstNameFilter = fullName[0];
-                var lastNameFilter = fullName.Length > 1 ? fullName[1] : "";
+                .Include(u => u.Qualification)
+                .Include(u => u.JobTitle)
+                .Include(u => u.University)
+                .Include(u => u.Industry)
+                .Include(u => u.Country)
+                .Where(u => _globalInfo.CompanyId == null || u.CompanyId != _globalInfo.CompanyId);
 
-                query = query.Where(u =>
-                    (u.FirstName.Contains(firstNameFilter) &&
-                     (string.IsNullOrEmpty(lastNameFilter) || u.LastName.Contains(lastNameFilter))) ||
-                    (u.LastName.Contains(lastNameFilter) &&
-                     (string.IsNullOrEmpty(firstNameFilter) || u.FirstName.Contains(firstNameFilter))));
-            }
-            else
+            var filter = PagingInputDto.Filter?.Trim();
+
+            // Filtering by text fields and handling full name search
+            if (!string.IsNullOrEmpty(filter))
             {
-                // If there's no space, apply the filter for individual names
+                var filters = filter.Split(" ");
+                var firstNameFilter = filters[0];
+                var lastNameFilter = filters.Length > 1 ? filters[1] : "";
+
                 query = query.Where(u =>
-                    u.FirstName.Contains(filter) ||
-                    u.LastName.Contains(filter));
-            }
-            if (PagingInputDto.IndustryId != null && PagingInputDto.IndustryId.Count > 0)
-            {
-                query = query.Where(u => u.IndustryId.HasValue && PagingInputDto.IndustryId.Contains( u.IndustryId.Value ));
+                    u.UserName.Contains(filter) || u.Email.Contains(filter) || u.FavoriteName.Contains(filter) ||
+                    u.Position.Contains(filter) || u.PhoneNumber.Contains(filter) ||
+                    u.FirstName.Contains(firstNameFilter) && (string.IsNullOrEmpty(lastNameFilter) || u.LastName.Contains(lastNameFilter)) ||
+                    u.LastName.Contains(lastNameFilter) && (string.IsNullOrEmpty(firstNameFilter) || u.FirstName.Contains(firstNameFilter))
+                );
             }
-            if (PagingInputDto.QualificationId != null)
-            {
+
+            // Applying additional filters
+            if (PagingInputDto.IndustryId?.Count > 0)
+                query = query.Where(u => u.IndustryId.HasValue && PagingInputDto.IndustryId.Contains(u.IndustryId.Value));
+
+            if (PagingInputDto.QualificationId.HasValue)
                 query = query.Where(u => u.QualificationId == PagingInputDto.QualificationId);
-            }
-            if (PagingInputDto.JobTitleId != null)
-            {
+
+            if (PagingInputDto.JobTitleId.HasValue)
                 query = query.Where(u => u.JobTitleId == PagingInputDto.JobTitleId);
-            }
-            if (PagingInputDto.UniversityId != null)
-            {
+
+            if (PagingInputDto.UniversityId.HasValue)
                 query = query.Where(u => u.UniversityId == PagingInputDto.UniversityId);
-            }
-            if (PagingInputDto.CountryId != null && PagingInputDto.CountryId.Count > 0)
-            {
-                //List<long> CountryList = PagingInputDto.CountryId.Split(",").Select(long.Parse).ToList();
+
+            if (PagingInputDto.CountryId?.Count > 0)
                 query = query.Where(u => u.CountryId.HasValue && PagingInputDto.CountryId.Contains(u.CountryId.Value));
-            }
-            if (PagingInputDto.UserTypeId != null && PagingInputDto.UserTypeId.Count > 0)
-            {
+
+            if (PagingInputDto.UserTypeId?.Count > 0)
                 query = query.Where(u => PagingInputDto.UserTypeId.Contains(u.UserType));
-            }
-            if (PagingInputDto.Employed != null)
-            {
-                if(PagingInputDto.Employed == true)
-                    query = query.Where(u => u.CompanyId != null);
-                else 
-                    query = query.Where(u => u.CompanyId == null);
-            }
-           
 
-            var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
+            if (PagingInputDto.Employed.HasValue)
+                query = query.Where(u => PagingInputDto.Employed.Value ? u.CompanyId != null : u.CompanyId == null);
 
-            var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
+            // Ordering by specified field and direction
+            var orderByField = !string.IsNullOrEmpty(PagingInputDto.OrderByField) ? PagingInputDto.OrderByField : "UserName";
+            var orderByDirection = PagingInputDto.OrderType?.ToLower() == "desc" ? "desc" : "asc";
+            query = query.OrderBy($"{orderByField} {orderByDirection}");
 
+            // Pagination
             var total = await query.CountAsync();
+            var result = await query
+                .Skip((PagingInputDto.PageNumber - 1) * PagingInputDto.PageSize)
+                .Take(PagingInputDto.PageSize)
+                .ToListAsync();
 
-            var list = MapperObject.Mapper
-                .Map<IList<UserAllDto>>(await page.ToListAsync());
+            var list = MapperObject.Mapper.Map<IList<UserAllDto>>(result);
 
-            var response = new PagingResultDto<UserAllDto>
-            {
-                Result = list,
-                Total = total
-            };
-
-            return response;
+            return new PagingResultDto<UserAllDto> { Result = list, Total = total };
         }
+
         public async Task<List<UserDto>> GetAllEmployees()
         {
             var employees = await _userManager.GetUsersInRoleAsync("Employee");

+ 15 - 0
MTWorkHR.Core/Entities/Chat/Connection.cs

@@ -0,0 +1,15 @@
+using MTWorkHR.Core.Entities.Base;
+using System;
+using System.Collections.Generic;
+
+#nullable disable
+
+namespace MTWorkHR.Core.Entities
+{
+    public partial class Connection : Entity
+    {
+        public Guid UserId { get; set; }
+        public string SignalrId { get; set; }
+        public DateTime TimeStamp { get; set; }
+    }
+}

+ 15 - 0
MTWorkHR.Core/IRepositories/Chat/IConnectionRepository.cs

@@ -0,0 +1,15 @@
+using MTWorkHR.Core.Entities;
+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 IConnectionRepository : IRepository<Connection>
+    {
+     
+    }
+}

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

@@ -36,6 +36,7 @@ namespace MTWorkHR.Core.UnitOfWork
         ILoginOTPRepository LoginOTP { get; }
 
         IChatMessageRepository ChatMessage { get; }
+        IConnectionRepository Connection { get; }
 
         Task<int> CompleteAsync();
 

+ 3 - 2
MTWorkHR.Infrastructure/InfrastructureServiceRegistration.cs

@@ -31,7 +31,7 @@ namespace MTWorkHR.Infrastructure
             
             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();
@@ -83,7 +83,8 @@ namespace MTWorkHR.Infrastructure
             services.AddScoped(typeof(ICityRepository), typeof(CityRepository));
             services.AddScoped(typeof(IProjectTeamRepository), typeof(ProjectTeamRepository));
             services.AddScoped(typeof(IChatMessageRepository), typeof(ChatMessageRepository));
-
+            services.AddScoped(typeof(IConnectionRepository), typeof(ConnectionRepository));
+            
 
 
             services.AddScoped(typeof(IPermissionRepository), typeof(PermissionRepository));

+ 21 - 0
MTWorkHR.Infrastructure/Repositories/Chat/ConnectionRepository.cs

@@ -0,0 +1,21 @@
+using Microsoft.EntityFrameworkCore;
+using MTWorkHR.Core.Entities;
+using MTWorkHR.Core.IDto;
+using MTWorkHR.Infrastructure.Entities;
+using MTWorkHR.Infrastructure.DBContext;
+using MTWorkHR.Core.IRepositories;
+
+namespace MTWorkHR.Infrastructure.Repositories
+{
+    public class ConnectionRepository : Repository<Connection>, IConnectionRepository
+    {
+        private readonly DbSet<Connection> dbSet;
+
+        public ConnectionRepository(HRDataContext context) : base(context)
+        {
+            dbSet = context.Set<Connection>();
+        }
+        
+       
+    }
+}

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

@@ -39,6 +39,7 @@ namespace MTWorkHR.Infrastructure.UnitOfWorks
         public ILoginOTPRepository LoginOTP { get; }
         public ICityRepository City { get; }
         public IChatMessageRepository ChatMessage { get; }
+        public IConnectionRepository Connection { get; }
 
         public UnitOfWork(HRDataContext _context
             , IPermissionRepository permission
@@ -66,6 +67,8 @@ namespace MTWorkHR.Infrastructure.UnitOfWorks
             , IMeetingUserRepository meetingUser
             , IProjectTeamRepository projectTeam
             , IChatMessageRepository chatMessage
+            , IConnectionRepository connection
+
             )
         {
             context = _context;
@@ -94,6 +97,7 @@ namespace MTWorkHR.Infrastructure.UnitOfWorks
             MeetingUser = meetingUser;
             ProjectTeam = projectTeam;
             ChatMessage = chatMessage;
+            Connection = connection;
         }
 
         public async Task<int> CompleteAsync()