public async Task <ServiceResponse <MessageGetDTO> > CreateMessageAsync(MessageAddDTO messageDTO) { ServiceResponse <MessageGetDTO> serviceResponse = new ServiceResponse <MessageGetDTO>(); try { UserDialog userDialog = await _messageRepository.GetUserDialogByFKIdAsync(messageDTO.UserId, messageDTO.DialogId); if (userDialog == null) { serviceResponse.Success = false; serviceResponse.Message = "Нет доступа"; return(serviceResponse); } Message message = new Message() { Text = messageDTO.Text, UserDialog = userDialog }; _messageRepository.Add(message); await _context.SaveChangesAsync(); _chatEventRepository.Add(new ChatEvent() { UserId = messageDTO.UserId, DialogId = messageDTO.DialogId, TypeOfActionId = (int)ActionTypes.NewMessage, MessageId = message.Id }); await _context.SaveChangesAsync(); _botNotifier.NotifyAsync(); serviceResponse.Data = _mapper.Map <MessageGetDTO>(messageDTO); serviceResponse.Data.Id = message.Id; } catch (Exception ex) { serviceResponse.Success = false; serviceResponse.Message = ex.Message; return(serviceResponse); } return(serviceResponse); }
public async Task <ServiceResponse <int> > AddUserToDialogAsync(UserDialogAddDTO userDialogDTO) { ServiceResponse <int> serviceResponse = new ServiceResponse <int>(); try { var dialog = await _dialogRepository.GetAsync(userDialogDTO.DialogId); if (dialog.IsTeteATete) { serviceResponse.Success = false; serviceResponse.Message = "Только два пользователя"; return(serviceResponse); } UserDialog userDialog = new UserDialog() { DialogId = userDialogDTO.DialogId, UserId = userDialogDTO.UserId }; _dialogRepository.AddUserToDialog(userDialog); await _context.SaveChangesAsync(); _chatEventRepository.Add(new ChatEvent() { UserId = userDialogDTO.UserId, DialogId = userDialogDTO.DialogId, TypeOfActionId = (int)ActionTypes.UserAdded }); await _context.SaveChangesAsync(); _botNotifier.NotifyAsync(); serviceResponse.Data = userDialog.UserId; } catch (Exception ex) { serviceResponse.Success = false; serviceResponse.Message = ex.Message + ex.InnerException; return(serviceResponse); } return(serviceResponse); }