public async Task <ChatVm> EditChatAsync(EditChatVm editChat, long userId) { using (MessengerDbContext context = contextFactory.Create()) { IQueryable <Chat> query = from chat in context.Chats join chatUser in context.ChatUsers on chat.Id equals chatUser.ChatId join chatUsers in context.ChatUsers on chat.Id equals chatUsers.ChatId where chat.Id == editChat.Id && chatUser.UserId == userId && chatUser.UserRole >= UserRole.Admin && chat.Deleted == false && chatUser.Banned == false && chatUser.Deleted == false && chatUsers.Banned == false && chatUsers.Deleted == false select chat; Chat targetChat = await query .FirstOrDefaultAsync() .ConfigureAwait(false); if (targetChat != null) { targetChat = ChatConverter.GetChat(targetChat, editChat); await context.SaveChangesAsync().ConfigureAwait(false); return(ChatConverter.GetChatVm(targetChat)); } throw new PermissionDeniedException(); } }
public static Chat GetChat(Chat editableChat, EditChatVm editChat) { if (editChat != null) { editableChat.About = editChat.About ?? editableChat.About; editableChat.Name = editChat.Name ?? editableChat.Name; editableChat.Photo = editChat.Photo ?? editableChat.Photo; editableChat.Public = editChat.Public ?? editableChat.Public; editableChat.Security = editChat.Security ?? editableChat.Security; editableChat.Visible = editChat.Visible ?? editableChat.Visible; } return(editableChat); }
public async Task EditChat() { var chat = fillTestDbHelper.Chats.FirstOrDefault(); var expectedChat = new EditChatVm { Id = chat.Id, About = "Edited about", Name = "Edited name", Photo = "NewPhotoRef" }; var actualChat = await updateChatsService.EditChatAsync(expectedChat, chat.ChatUsers.FirstOrDefault(opt => opt.UserRole >= UserRole.Admin).UserId); Assert.True( expectedChat.Id == actualChat.Id && expectedChat.Name == actualChat.Name && expectedChat.About == actualChat.About && expectedChat.Photo == actualChat.Photo); }