From 4843890f9de61e7e054e38cc43bbc4233aacd412 Mon Sep 17 00:00:00 2001 From: mmrbnjd Date: Sat, 2 Aug 2025 19:00:53 +0330 Subject: [PATCH] ... --- Hushian.Application/Services/ChatService.cs | 92 +++++++++++++++---- .../Controllers/v1/ChatController.cs | 11 +++ Presentation/HushianWebApp/Pages/Chat.razor | 21 ++++- .../Pages/FromUserSide/UserCP.razor | 61 ++++++++---- .../HushianWebApp/Service/ChatService.cs | 8 ++ 5 files changed, 158 insertions(+), 35 deletions(-) diff --git a/Hushian.Application/Services/ChatService.cs b/Hushian.Application/Services/ChatService.cs index 9c90b4d..bed5466 100644 --- a/Hushian.Application/Services/ChatService.cs +++ b/Hushian.Application/Services/ChatService.cs @@ -8,6 +8,7 @@ using Microsoft.AspNetCore.SignalR; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; +using System.ComponentModel.Design; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -166,22 +167,39 @@ namespace Hushian.Application.Services ConversationResponses = new List() { new() { Text = dto.Question, Type = type } } }; var mi = await _ConversationRepository.ADD(conversation); - Response.Value = new ChatItemDto() - { - ID = mi.ID, - ExperID = mi.ConversationResponses.OrderBy(o => o.ID).Last().ExperID, - ExperFullName = /*mi.ConversationResponses.OrderBy(o => o.ID).Last().Exper.FullName*/ "", - GroupID = mi.GroupID, - GroupName = /*mi.Group.Name*/ "", - LastText = mi.ConversationResponses.OrderBy(o => o.ID).Last().Text, - LastMsgdate = mi.Cdatetime.GetDatePersian(), - LastMsgtime = mi.Cdatetime.GetTime(), - LastMsgType = mi.ConversationResponses.OrderBy(o => o.ID).Last().Type, - status = mi.Status, - UserID = mi.UserID, - UserFullName = /*string.IsNullOrEmpty(mi.User.FullName) ? mi.User.Mobile : mi.User.FullName*/ "" + if(mi.ID > 0) + Response.Value = await _ConversationRepository.Get() + .Include(inc => inc.Group).Include(inc => inc.ConversationResponses).ThenInclude(tinc => tinc.Exper) + .Where(w => w.ID==mi.ID) + .Select(s => new ChatItemDto() + { + ID = s.ID, + ExperID = s.ConversationResponses.OrderBy(o => o.ID).Last().ExperID, + ExperFullName = s.ConversationResponses.OrderBy(o => o.ID).Last().Exper.FullName, + GroupID = s.GroupID, + GroupName = s.Group.Name, + LastText = s.ConversationResponses.OrderBy(o => o.ID).Last().Text, + LastMsgdate = s.Cdatetime.GetDatePersian(), + LastMsgtime = s.Cdatetime.GetTime(), + LastMsgType = s.ConversationResponses.OrderBy(o => o.ID).Last().Type, + status = s.Status, + UserID = s.UserID, + UserFullName = string.IsNullOrEmpty(s.User.FullName) ? s.User.Mobile : s.User.FullName, + Responses = s.ConversationResponses.Select(ss => new ChatItemResponseDto() + { + ChatItemID = ss.ConversationID, + ExperID = ss.ExperID, + ExperName = ss.Exper.FullName, + FileContent = ss.FileContent, + FileName = ss.FileName, + FileType = ss.FileType, + ID = ss.ID, + IsRead = ss.IsRead, + text = ss.Text, + Type = ss.Type + }).ToList() - }; + }).FirstOrDefaultAsync(); Response.Success = mi.ID > 0; } else Response.Errors.Add("شناسه گروه صحیح نمی باشد"); @@ -305,7 +323,49 @@ namespace Hushian.Application.Services Response.Value= await _ConversationRepository.Get() .Include(inc => inc.Group) .Include(inc => inc.ConversationResponses).ThenInclude(tinc => tinc.Exper) - .Where(w => w.UserID== UserID && w.Status != ConversationStatus.Finished) + .Where(w => w.UserID== UserID &&w.CompanyID==CompanyID && w.Status != ConversationStatus.Finished) + .OrderByDescending(o=>o.ID) + .Select(s => new ChatItemDto() + { + ID = s.ID, + ExperID = s.ConversationResponses.OrderBy(o => o.ID).Last().ExperID, + ExperFullName = s.ConversationResponses.OrderBy(o => o.ID).Last().Exper.FullName, + GroupID = s.GroupID, + GroupName = s.Group.Name, + LastText = s.ConversationResponses.OrderBy(o => o.ID).Last().Text, + LastMsgdate = s.Cdatetime.GetDatePersian(), + LastMsgtime = s.Cdatetime.GetTime(), + LastMsgType = s.ConversationResponses.OrderBy(o => o.ID).Last().Type, + status = s.Status, + UserID = s.UserID, + UserFullName = string.IsNullOrEmpty(s.User.FullName) ? s.User.Mobile : s.User.FullName, + Responses = s.ConversationResponses.Select(ss => new ChatItemResponseDto() + { + ChatItemID = ss.ConversationID, + ExperID = ss.ExperID, + ExperName = ss.Exper.FullName, + FileContent = ss.FileContent, + FileName = ss.FileName, + FileType = ss.FileType, + ID = ss.ID, + IsRead = ss.IsRead, + text = ss.Text, + Type = ss.Type + }).ToList() + + }).FirstOrDefaultAsync(); + + if (Response.Value != null) Response.Success = true; + return Response; + } + public async Task> GetChat(int ChatID, int UserID) + { + ResponseBase Response = new(); + Response.Value = await _ConversationRepository.Get() + .Include(inc => inc.Group) + .Include(inc => inc.ConversationResponses).ThenInclude(tinc => tinc.Exper) + .Where(w => w.UserID == UserID && w.ID == ChatID ) + .OrderByDescending(o => o.ID) .Select(s => new ChatItemDto() { ID = s.ID, diff --git a/Presentation/Hushian.WebApi/Controllers/v1/ChatController.cs b/Presentation/Hushian.WebApi/Controllers/v1/ChatController.cs index df41909..a6f23ed 100644 --- a/Presentation/Hushian.WebApi/Controllers/v1/ChatController.cs +++ b/Presentation/Hushian.WebApi/Controllers/v1/ChatController.cs @@ -52,6 +52,17 @@ namespace Hushian.WebApi.Controllers.v1 return response.Success ? Ok(response.Value) : Accepted() ; + } + [HttpGet("User/Chat/{ChatID}")] + [Authorize(Roles = "User")] + public async Task Getchat(int ChatID) + { + string strUserID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First(); + int UserID = Convert.ToInt32(strUserID); + var response = await _chatService.GetChat(ChatID, UserID); + + return response.Success ? Ok(response.Value) : Accepted(); + } [HttpGet("ChatsAwaitingOurResponse")] [Authorize(Roles = "Company,Exper")] diff --git a/Presentation/HushianWebApp/Pages/Chat.razor b/Presentation/HushianWebApp/Pages/Chat.razor index 05c7da0..eb38b5c 100644 --- a/Presentation/HushianWebApp/Pages/Chat.razor +++ b/Presentation/HushianWebApp/Pages/Chat.razor @@ -58,10 +58,17 @@
- @item.UserFullName + @item.UserFullName + @if(!string.IsNullOrEmpty(item.GroupName)){ +
+ @item.GroupName +
+ } +
@item.LastMsgdate @item.LastMsgtime +
@item.LastText
@@ -82,6 +89,12 @@
@item.UserFullName + @if (!string.IsNullOrEmpty(item.GroupName)) + { +
+ @item.GroupName +
+ }
@item.LastMsgdate @item.LastMsgtime @@ -108,6 +121,12 @@
@item.UserFullName + @if (!string.IsNullOrEmpty(item.GroupName)) + { +
+ @item.GroupName +
+ }
@item.LastMsgdate @item.LastMsgtime diff --git a/Presentation/HushianWebApp/Pages/FromUserSide/UserCP.razor b/Presentation/HushianWebApp/Pages/FromUserSide/UserCP.razor index 37aeaf3..54da232 100644 --- a/Presentation/HushianWebApp/Pages/FromUserSide/UserCP.razor +++ b/Presentation/HushianWebApp/Pages/FromUserSide/UserCP.razor @@ -1,4 +1,5 @@ @page "/UserCP/{CompanyID:int}" +@page "/UserCP/{CompanyID:int}/{ChatID:int?}" @using Common.Dtos.Company @using Common.Dtos.Conversation @using Common.Dtos.Group @@ -89,7 +90,7 @@

هوشیان

- + @if (CompanyGroups != null && CompanyGroups.Any()) {
@@ -97,10 +98,18 @@
@foreach (var group in CompanyGroups) { -
- + @if (group.img == null || group.img.Length == 0) + { + + } + else + { + Uploaded Image + + } @group.Name
@@ -154,6 +163,8 @@
@code { [Parameter] public int CompanyID { get; set; } + [Parameter] public int? ChatID { get; set; } + private bool _shouldObserveVisibility = false; int? GroupID = null; ReadANDUpdate_CompanyDto? CompanyInfo = new(); @@ -170,7 +181,12 @@ { if (CompanyInfo == null) return string.Empty; string value = $"{CompanyInfo.FullName}"; - if (LastOpenChat != null) + if (GroupID.HasValue) + { + value += "/" + CompanyGroups.FirstOrDefault(f => f.ID == GroupID.GetValueOrDefault()).Name; + + } + if (LastOpenChat != null && !string.IsNullOrEmpty(LastOpenChat.ExperFullName)) { value += "/" + LastOpenChat.ExperFullName; } @@ -201,9 +217,13 @@ Question = MsgInput, UserID = 0 }); - if (model != null) LastOpenChat = model; - else toastService.Notify(new ToastMessage(ToastType.Danger, "خطا در گفتگو جدید")); - + if (model != null) + { + LastOpenChat = model; + } + else toastService.Notify(new ToastMessage(ToastType.Danger, "خطا در گفتگو جدید")); + + } await Task.Yield(); // Scroll to bottom for user's own messages @@ -269,8 +289,9 @@ { if (CompanyInfo != null) { - // LastOpenChat = await ChatService.GetLastOpenChatInCompany(CompanyID); - LastOpenChat = null; + if (ChatID.HasValue) LastOpenChat = await ChatService.Getchat(ChatID.GetValueOrDefault()); + else LastOpenChat = LastOpenChat = await ChatService.GetLastOpenChatInCompany(CompanyID); + if (LastOpenChat != null) { @@ -318,12 +339,16 @@ IsLogin = false; StateHasChanged(); } - + async Task SelectGroup(int groupId) { GroupID = groupId; StateHasChanged(); } + private string GetImageSource(byte[] bytes) + => $"data:image/jpeg;base64,{Convert.ToBase64String(bytes)}"; + + }