public async Task <ResultDto <Guid> > ExecuteAsync(long MyUserId, string TargetUsername) { return(await Task.Run(async() => { try { var targetUser = await _context.Users .FirstOrDefaultAsync(u => u.Username.ToLower() == TargetUsername.ToLower()); var myUser = await _context.Users.FindAsync(MyUserId); if (targetUser == null || myUser == null) { return new ResultDto <Guid>() { Status = Common.Enums.ServiceStatus.NotFound, }; } Chatroom oldChatroom = await _context.Chatrooms .Include(c => c.UserInChatrooms) .Where(c => c.ChatroomType == Common.Enums.ChatroomType.PV) .FirstOrDefaultAsync(c => c.UserInChatrooms.Any(u => u.UserId == targetUser.Id) && c.UserInChatrooms.Any(u => u.UserId == MyUserId)); Guid Data = new Guid(); if (oldChatroom != null) { Data = oldChatroom.Guid; } else { Chatroom newChatroom = new Chatroom() { ChatroomType = Common.Enums.ChatroomType.PV, Guid = Guid.NewGuid(), InsertTime = DateTime.Now, }; await _context.Chatrooms.AddAsync(newChatroom); //await _context.SaveChangesAsync(); UserInChatroom myUserInChatroom = new UserInChatroom() { Chatroom = newChatroom, ChatroomId = newChatroom.Id, User = myUser, UserId = myUser.Id }; UserInChatroom targetUserInChatroom = new UserInChatroom() { Chatroom = newChatroom, ChatroomId = newChatroom.Id, User = targetUser, UserId = targetUser.Id }; await _context.UserInChatrooms.AddAsync(myUserInChatroom); await _context.UserInChatrooms.AddAsync(targetUserInChatroom); await _context.SaveChangesAsync(); Data = newChatroom.Guid; } return new ResultDto <Guid>() { Data = Data, Status = Common.Enums.ServiceStatus.Success, }; } catch (Exception) { return new ResultDto <Guid>() { Status = Common.Enums.ServiceStatus.SystemError, }; } })); }
public async Task <ResultDto> ExecuteAsync(RequestCreateNewGroupService request) { return(await Task.Run(async() => { try { #region Find User var Creator = await _context.Users.FindAsync(request.UserId); #endregion #region Validate if (Creator == null) { return new ResultDto() { Status = Common.Enums.ServiceStatus.NotFound }; } if (String.IsNullOrWhiteSpace(request.GroupName)) { return new ResultDto() { Status = Common.Enums.ServiceStatus.Error, Message = "نام وارد شده اشتباه اسنت" }; } #endregion #region --Create Chatroom-- Chatroom newChatroom = new Chatroom { Name = request.GroupName, ChatroomType = Common.Enums.ChatroomType.Group, CreatorId = request.UserId, Creator = Creator, Guid = Guid.NewGuid(), InsertTime = DateTime.Now, }; await _context.Chatrooms.AddAsync(newChatroom); Message info = new Message() { Chatroom = newChatroom, ChatroomID = newChatroom.Id, MessageType = MessageType.Info, SendDate = DateTime.Now, Text = $"گروه {newChatroom.Name} ایجاد شد" }; await _context.Messages.AddAsync(info); #endregion #region --add image to group-- if (request.ImageFile != null) { var upRes = await request.ImageFile.UploadFileAsync("Images/ChatoomImage/", _environment); if (!upRes.Status) { return new ResultDto() { Status = Common.Enums.ServiceStatus.SaveFileError, }; } ChatroomImage chatroomImage = new ChatroomImage() { Chatroom = newChatroom, ChatroomId = newChatroom.Id, ImageName = upRes.FileNameAddress, InsertTime = DateTime.Now, }; await _context.ChatroomImages.AddAsync(chatroomImage); } #endregion #region --Add Creator To Group-- UserInChatroom userInChatroom = new UserInChatroom() { Chatroom = newChatroom, ChatroomId = newChatroom.Id, User = Creator, UserId = request.UserId, }; await _context.UserInChatrooms.AddAsync(userInChatroom); #endregion await _context.SaveChangesAsync(); return new ResultDto() { Status = Common.Enums.ServiceStatus.Success, }; } catch (Exception) { return new ResultDto() { Status = Common.Enums.ServiceStatus.SystemError, }; } })); }
public async Task <ResultDto <JoinedChatDetailDto> > ExecuteAsync(long MyUserId, Guid ChatroomJoinGuid) { return(await Task.Run(async() => { try { var chatroom = await _context.Chatrooms .FirstOrDefaultAsync(c => c.JoinLinkGuid == ChatroomJoinGuid); var user = await _context.Users.FindAsync(MyUserId); #region validation if (chatroom == null || chatroom.ChatroomType == Common.Enums.ChatroomType.PV || user == null) { return new ResultDto <JoinedChatDetailDto>() { Status = Common.Enums.ServiceStatus.NotFound, }; } if (_context.UserInChatrooms.Any(u => u.UserId == MyUserId && u.ChatroomId == chatroom.Id)) { return new ResultDto <JoinedChatDetailDto>() { Status = Common.Enums.ServiceStatus.AccessDenied, }; } #endregion UserInChatroom newUserInChatroom = new UserInChatroom() { Chatroom = chatroom, ChatroomId = chatroom.Id, User = user, UserId = MyUserId }; await _context.UserInChatrooms.AddAsync(newUserInChatroom); Message info = new Message() { Chatroom = chatroom, ChatroomID = chatroom.Id, MessageType = MessageType.Info, SendDate = DateTime.Now, Text = $" {user.Name} به گروه پیوست", }; await _context.Messages.AddAsync(info); await _context.SaveChangesAsync(); JoinedChatDetailDto Data = new JoinedChatDetailDto() { ChatroomName = chatroom.Name, Guid = chatroom.Guid, }; #region find image var chatroomImage = await _context.ChatroomImages .FirstOrDefaultAsync(c => c.ChatroomId == chatroom.Id); string chatroomImageName = "Images/ChatroomImages/Defaut.png"; if (chatroomImage != null) { chatroomImageName = chatroomImage.ImageName; } #endregion Data.ImageName = chatroomImageName; #region find last message var lastmessage = await _context.Messages .Where(m => m.ChatroomID == chatroom.Id) .OrderBy(m => m.SendDate) .LastOrDefaultAsync(); #endregion Data.LastMessage = lastmessage?.Text; Data.LastMessageTime = lastmessage?.SendDate; return new ResultDto <JoinedChatDetailDto>() { Status = Common.Enums.ServiceStatus.Success, Data = Data, }; } catch (Exception) { return new ResultDto <JoinedChatDetailDto>() { Status = Common.Enums.ServiceStatus.SystemError, }; } })); }