zinab_elgendy пре 1 месец
родитељ
комит
8e9a556270

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

@@ -1,35 +0,0 @@
-using Microsoft.AspNetCore.SignalR;
-using MTWorkHR.Application.Identity;
-using MTWorkHR.Application.Models;
-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.HubConnection.GetAllAsync();
-            var currUserId = allConnections.Item1.Where(c => c.SignalrId == Context.ConnectionId).Select(c => c.UserId).SingleOrDefault();
-            List<ChatUserDto> onlineUsers = allConnections.Item1
-                .Where(c => c.UserId != currUserId)
-                .Select(c =>
-                    new ChatUserDto (c.UserId, c.UserName , 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);
-        }
-    }
-}

+ 91 - 43
MTWorkHR.Application/Chat/ChatHub.cs

@@ -2,7 +2,9 @@
 using Microsoft.AspNetCore.SignalR;
 using MTWorkHR.Application.Filters;
 using MTWorkHR.Application.Identity;
+using MTWorkHR.Application.Mapper;
 using MTWorkHR.Application.Models;
+using MTWorkHR.Application.Services;
 using MTWorkHR.Core.Entities;
 using MTWorkHR.Core.Entities.Base;
 using MTWorkHR.Core.Global;
@@ -18,37 +20,22 @@ namespace MTWorkHR.API.Chat
     {
         private readonly IUnitOfWork _unitOfWork;
         private readonly GlobalInfo _globalInfo;
-
-        public ChatHub(IUnitOfWork unitOfWork, GlobalInfo globalInfo) 
+        private readonly UserService _userService; 
+        public ChatHub(IUnitOfWork unitOfWork, GlobalInfo globalInfo, UserService userService) 
         {
             _unitOfWork = unitOfWork;
             _globalInfo = globalInfo;
-        }
-        public async Task SendMessage(string user, string message)
-        {
-            await Clients.All.SendAsync("ReceiveMessage", user, message);
+            _userService = userService;
         }
 
 
-        public override Task OnDisconnectedAsync(Exception exception)
-        {
-            var connections = _unitOfWork.HubConnection.GetAllAsync(Context.ConnectionId).Result;
-           var currUserId = connections.Item1.Select(c => c.UserId).SingleOrDefault();
-            _unitOfWork.HubConnection.DeleteRangeAsync(connections.Item1.ToList());
-            _unitOfWork.CompleteAsync();
-            Clients.Others.SendAsync("userOff", currUserId);
-            return base.OnDisconnectedAsync(exception);
-        }
-
-
-        //2Tutorial
-        public async Task authMe(string userId)
+        public async Task authMe()
         {
             string currSignalrID = Context.ConnectionId;
             //Person tempPerson = ctx.Person.Where(p => p.Username == personInfo.userName && p.Password == personInfo.password)
             //    .SingleOrDefault();
 
-            if (userId  == _globalInfo.UserId) //if credentials are correct
+            if (_globalInfo.UserId != null) //if credentials are correct
             {
                 Console.WriteLine("\n" + _globalInfo.UserName + " logged in" + "\nSignalrID: " + currSignalrID);
 
@@ -73,37 +60,51 @@ namespace MTWorkHR.API.Chat
             }
         }
 
-
-        //3Tutorial
-        public async Task reauthMe(string userId)
+      
+      
+        public async Task getCompanyUsersList()
         {
-            string currSignalrID = Context.ConnectionId;
-            //ApplicationUser tempPerson = ctx.Person.Where(p => p.Id == personId)
-            //    .SingleOrDefault();
+            var companyUsers = await _userService.GetAllCompanyEmployees();
+            var response = MapperObject.Mapper.Map<List<UserAllDto>>(companyUsers);
 
-            if (userId == _globalInfo.UserId) //if credentials are correct
-            {
-                Console.WriteLine("\n" + _globalInfo.UserName + " logged in" + "\nSignalrID: " + currSignalrID);
+            await Clients.Caller.SendAsync("getCompanyUsersList", response);
+        }
 
-                HubConnection currUser = new HubConnection
-                {
-                    UserId = _globalInfo.UserId,
-                    SignalrId = currSignalrID,
-                    TimeStamp = DateTime.Now
-                };
-                var newConnection = await _unitOfWork.HubConnection.AddAsync(currUser);
+      
+        //Simple Test Method
+        public async Task SendMessageAll(string user, string message)
+        {
+            await Clients.All.SendAsync("ReceiveMessage", user, message);
+        }
 
-                await _unitOfWork.CompleteAsync();
+        public async Task sendMsg(string userId, string msg)
+        {
+            var allConnections = await _unitOfWork.HubConnection.GetAllAsync();
+            var receiverUser = allConnections.Item1.FirstOrDefault(c => c.UserId == userId);
+            var connId = receiverUser != null ? receiverUser.SignalrId : "0";
+            ChatMessage messag = new ChatMessage { Content = msg, ReceiverId = connId, SenderId = _globalInfo.UserId, SenderName = _globalInfo.UserName, IsSeen = false };
+            await Clients.Client(connId).SendAsync("sendMsgResponse", Context.ConnectionId, msg);
+        }
 
-                ChatUserDto newUser = new ChatUserDto(_globalInfo.UserId, _globalInfo.UserName, currSignalrID);
 
-                await Clients.Caller.SendAsync("reauthMeResponse", newUser);//4Tutorial
-                await Clients.Others.SendAsync("userOn", newUser);//4Tutorial
-            }
-        } //end of reauthMe
+        public async Task getAllMessagesByUser(string senderId)
+        {
+            var allmessages = await _unitOfWork.ChatMessage.GetAllWithChildrenAsync(senderId, _globalInfo.UserId);
+            await Clients.Caller.SendAsync("allMessagesResponse", allmessages.Item1);
+        }
+
+        public async override Task<Task> OnDisconnectedAsync(Exception exception)
+        {
+            var connections = await _unitOfWork.HubConnection.GetAllAsync(Context.ConnectionId);
+           var currUserId = connections.Item1.Select(c => c.UserId).SingleOrDefault();
+            _unitOfWork.HubConnection.DeleteRangeAsync(connections.Item1.ToList());
+            _unitOfWork.CompleteAsync();
+            await Clients.Others.SendAsync("userOff", currUserId);
+            return base.OnDisconnectedAsync(exception);
+        }
 
 
-        //4Tutorial
+      
         public void logOut(Guid personId)
         {
             var connections = _unitOfWork.HubConnection.GetAllAsync(Context.ConnectionId).Result;
@@ -114,5 +115,52 @@ namespace MTWorkHR.API.Chat
             Clients.Caller.SendAsync("logoutResponse");
             Clients.Others.SendAsync("userOff", personId);
         }
+
+      
+
+
+
+        public async Task getOnlineUsers()
+        {
+            var allConnections = await _unitOfWork.HubConnection.GetAllAsync();
+            var currUserId = allConnections.Item1.Where(c => c.SignalrId == Context.ConnectionId).Select(c => c.UserId).SingleOrDefault();
+            List<ChatUserDto> onlineUsers = allConnections.Item1
+                .Where(c => c.UserId != currUserId)
+                .Select(c =>
+                    new ChatUserDto(c.UserId, c.UserName, c.SignalrId)
+                ).ToList();
+            await Clients.Caller.SendAsync("getOnlineUsersResponse", onlineUsers);
+        }
+
+
+
+        //public async Task reauthMe(string userId)
+        //{
+        //    string currSignalrID = Context.ConnectionId;
+        //    //ApplicationUser tempPerson = ctx.Person.Where(p => p.Id == personId)
+        //    //    .SingleOrDefault();
+
+        //    if (userId == _globalInfo.UserId) //if credentials are correct
+        //    {
+        //        Console.WriteLine("\n" + _globalInfo.UserName + " logged in" + "\nSignalrID: " + currSignalrID);
+
+        //        HubConnection currUser = new HubConnection
+        //        {
+        //            UserId = _globalInfo.UserId,
+        //            SignalrId = currSignalrID,
+        //            TimeStamp = DateTime.Now
+        //        };
+        //        var newConnection = await _unitOfWork.HubConnection.AddAsync(currUser);
+
+        //        await _unitOfWork.CompleteAsync();
+
+        //        ChatUserDto newUser = new ChatUserDto(_globalInfo.UserId, _globalInfo.UserName, currSignalrID);
+
+        //        await Clients.Caller.SendAsync("reauthMeResponse", newUser);//4Tutorial
+        //        await Clients.Others.SendAsync("userOn", newUser);//4Tutorial
+        //    }
+        //} //end of reauthMe
+
+
     }
 }

+ 1 - 1
MTWorkHR.Core/IRepositories/Chat/IChatMessageRepository.cs

@@ -11,6 +11,6 @@ namespace MTWorkHR.Core.IRepositories
     public interface IChatMessageRepository : IRepository<ChatMessage>
     {
         Task<ChatMessage> GetByIdWithAllChildren(long id);
-        Task<Tuple<IQueryable<ChatMessage>, int>> GetAllWithChildrenAsync();
+        Task<Tuple<IQueryable<ChatMessage>, int>> GetAllWithChildrenAsync(string senderId, string receiverId);
     }
 }

+ 1 - 0
MTWorkHR.Infrastructure/DBContext/HRDataContext.cs

@@ -42,6 +42,7 @@ namespace MTWorkHR.Infrastructure.DBContext
         public DbSet<OrderAllocation> OrderAllocations { get; set; }
         public DbSet<OrderRequest> OrderRequests { get; set; }
         public DbSet<ChatMessage> ChatMessages { get; set; }
+        public DbSet<HubConnection> HubConnections { get; set; }
         //-------------------Lookups---------------------------
         public DbSet<CountryLookup> CountryLookups { get; set; }
         public DbSet<Qualification> Qualifications { get; set; }

+ 1 - 1
MTWorkHR.Infrastructure/InfrastructureServiceRegistration.cs

@@ -83,7 +83,7 @@ namespace MTWorkHR.Infrastructure
             services.AddScoped(typeof(ICityRepository), typeof(CityRepository));
             services.AddScoped(typeof(IProjectTeamRepository), typeof(ProjectTeamRepository));
             services.AddScoped(typeof(IChatMessageRepository), typeof(ChatMessageRepository));
-            services.AddScoped(typeof(IHubConnectionRepository), typeof(ConnectionRepository));
+            services.AddScoped(typeof(IHubConnectionRepository), typeof(HubConnectionRepository));
             
 
 

+ 2 - 2
MTWorkHR.Infrastructure/Repositories/Chat/ChatMessageRepository.cs

@@ -22,9 +22,9 @@ namespace MTWorkHR.Infrastructure.Repositories
                 .Include(x => x.ChatAttachments)
                 .FirstOrDefaultAsync(x => x.Id == id);
         }
-        public async Task<Tuple<IQueryable<ChatMessage>, int>> GetAllWithChildrenAsync()
+        public async Task<Tuple<IQueryable<ChatMessage>, int>> GetAllWithChildrenAsync(string senderId, string receiverId)
         {
-            var query = dbSet.Include(x => x.ChatAttachments).AsQueryable();
+            var query = dbSet.Include(x => x.ChatAttachments).Where(m=> m.ReceiverId == receiverId && m.SenderId == senderId).AsQueryable();
             var total = await query.CountAsync();
 
             return new Tuple<IQueryable<ChatMessage>, int>(query, total);

+ 2 - 2
MTWorkHR.Infrastructure/Repositories/Chat/ConnectionRepository.cs

@@ -7,11 +7,11 @@ using MTWorkHR.Core.IRepositories;
 
 namespace MTWorkHR.Infrastructure.Repositories
 {
-    public class ConnectionRepository : Repository<HubConnection>, IHubConnectionRepository
+    public class HubConnectionRepository : Repository<HubConnection>, IHubConnectionRepository
     {
         private readonly DbSet<HubConnection> dbSet;
 
-        public ConnectionRepository(HRDataContext context) : base(context)
+        public HubConnectionRepository(HRDataContext context) : base(context)
         {
             dbSet = context.Set<HubConnection>();
         }