public async Task <ActionResult <ConversationModel> > PatchConversation(Guid id, ConversationPatchModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var conversation = await _context.Conversations.Include(x => x.ConversationUsers).SingleOrDefaultAsync(x => x.Id == id); //TODO: handle authorization (user must be admin in the conversation to be able to do changes) if (conversation == null) { return(NotFound()); } if (model.IsPublic.HasValue) { conversation.IsPublic = model.IsPublic.Value; } if (model.Title != null) { conversation.Title = model.Title; } await _context.SaveChangesAsync(); var updatedConversation = new ConversationModelFactory().Create(conversation); var userIds = conversation.ConversationUsers.Select(x => x.UserId.ToString().ToLowerInvariant()).ToList(); await _conversationHubContext.Clients.Users(userIds).ConversationUpdated(updatedConversation); return(updatedConversation); }
public async Task <ActionResult <ConversationModel> > PutConversation(Guid id, Conversation conversation) { if (id != conversation.Id) { return(BadRequest()); } _context.Entry(conversation).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ConversationExists(id)) { return(NotFound()); } else { throw; } } // notify conversation users var userIds = await _context.ConversationUsers .Where(x => x.ConversationId == conversation.Id) .Select(x => x.UserId.ToString().ToLowerInvariant()) .ToListAsync(); var updatedConversationModel = new ConversationModelFactory().Create(conversation); await _conversationHubContext.Clients.Users(userIds).ConversationUpdated(updatedConversationModel); return(updatedConversationModel); }