public async Task BanDialog(string userToBanId, string whoAccessedId) { if (userToBanId == whoAccessedId) { throw new InvalidDataException("Can't ban yourself."); } var bannedBy = await usersRepository.GetByIdAsync(whoAccessedId); var banned = await usersRepository.GetByIdAsync(userToBanId); if (banned == null || bannedBy == null) { throw new KeyNotFoundException("Wrong id of person to ban."); } try { await UsersBansRepository.AddAsync(UsersBansDatamodel.Create(banned, bannedBy)); } catch { throw new InvalidDataException("User is already banned."); } UsersConversationDataModel dialog; if ((dialog = await usersConversationsRepository.GetDialog(userToBanId, whoAccessedId)) != null) { await ConversationsBansRepository.AddAsync(ConversationsBansDataModel.Create(banned, dialog.Conversation)); } await unitOfWork.Commit(); }
public async Task BanUserFromConversation(int conversationId, string userToBanId, string whoAccessedId) { if (userToBanId == whoAccessedId) { throw new InvalidDataException("Can't ban yourself."); } var conversation = await ConversationRepository.GetByIdAsync(conversationId); var banned = await usersRepository.GetByIdAsync(userToBanId); var userRole = await rolesRepository.GetByIdAsync(conversationId, whoAccessedId); if (userRole.RoleId != ChatRole.Moderator && userRole.RoleId != ChatRole.Creator) { throw new InvalidDataException("Only creator / moderator can ban users."); } if (banned == null) { throw new InvalidDataException("Wrong user to ban id was provided."); } if (conversation == null) { throw new InvalidDataException("Wrong conversation id was provided."); } try { await ConversationsBansRepository.AddAsync(ConversationsBansDataModel.Create(banned, conversation)); await unitOfWork.Commit(); } catch { throw new InvalidDataException("Wrong conversation id was provided."); } }