public async Task SendMessage(SendMessageViewModel message) { GetChannelByIdQueryResult channel = await _channelRepository.GetById(message.ChannelId); if (channel is null) { return; } GetUserByIdQueryResult user = await _userRepository.GetById(message.SenderId); if (user is null) { return; } var finalMessage = new Message { Content = message.Content, Date = message.Date, ChannelId = message.ChannelId, Sender = new User { Id = user.Id, Username = user.Username } }; await Clients.Group(message.ChannelId.ToString()).SendAsync("ReceiveMessage", finalMessage); }
public async Task <ICommandResult> HandleAsync(JoinChannelCommand command) { command.Validate(); if (command.Invalid) { AddNotifications(command); return(new CommandResult(false, "User could not join this channel", Errors)); } GetUserByIdQueryResult user = await _userRepository.GetById(command.UserId); if (user is null) { AddNotification(nameof(command.UserId), "User not found"); return(new CommandResult(false, "User not found", Errors)); } GetChannelByIdQueryResult channel = await _channelRepository.GetById(command.ChannelId); if (channel is null) { AddNotification(nameof(command.ChannelId), "Channel not found"); return(new CommandResult(false, "Channel not found", Errors)); } bool userAlreadyIsMemeber = await _channelRepository.UserIsMemberOfChannel(user.Id, channel.Id); if (userAlreadyIsMemeber) { AddNotification(nameof(command.ChannelId), "User already is member of this channel"); return(new CommandResult(false, "User could not join this channel", Errors)); } await _channelRepository.AddUserToChannel(user.Id, channel.Id, false); return(new CommandResult(true, "User successfully joined the channel", data: null)); }
public async Task <ICommandResult> HandleAsync(CreateChannelCommand command) { command.Validate(); if (command.Invalid) { AddNotifications(command); return(new CommandResult(false, "Could not create channel", Errors)); } GetUserByIdQueryResult user = await _userRepository.GetById(command.AdministratorId); if (user is null) { AddNotification(nameof(command.AdministratorId), "User not found"); return(new CommandResult(false, "Could not create channel", Errors)); } var administrator = new User(user.Id, user.Username); var channel = new Channel(command.Name, command.Description, administrator); AddNotifications(channel); if (Invalid) { return(new CommandResult(false, "Could not create channel", Errors)); } await _channelRepository.CreateChannel(channel); await _channelRepository.AddUserToChannel(administrator.Id, channel.Id, true); return(new CommandResult(true, "Channel succesfully created", new ChannelOutput { Id = channel.Id, Name = channel.Name, Description = channel.Description, Administrator = new UserOutput { Id = administrator.Id, Username = administrator.Username } })); }