public async Task <ServiceResponse <DialogGetDTO> > CreateDialogAsync(DialogAddDTO dialogDTO) { ServiceResponse <DialogGetDTO> serviceResponse = new ServiceResponse <DialogGetDTO>(); try { if (dialogDTO.IsTeteATete && dialogDTO.UserIds.Count != 2) { serviceResponse.Success = false; serviceResponse.Message = "Только два пользователя"; return(serviceResponse); } Dialog dialog = _mapper.Map <Dialog>(dialogDTO); _dialogRepository.Add(dialog); await _context.SaveChangesAsync(); serviceResponse.Data = _mapper.Map <DialogGetDTO>(dialogDTO); serviceResponse.Data.Id = dialog.Id; } catch (Exception ex) { serviceResponse.Success = false; serviceResponse.Message = ex.Message; return(serviceResponse); } return(serviceResponse); }
public async Task <IActionResult> Post(DialogAddDTO dialogAddDTO) { ServiceResponse <DialogGetDTO> serviceResponse = await _dialogService.CreateDialogAsync(dialogAddDTO); if (serviceResponse.Success == false) { return(BadRequest(serviceResponse.Message)); } return(Ok(serviceResponse)); }
public async Task CreateDialogAsyncReturnsServiceResponseWithExMessage_IfErrorOccurs() { DialogAddDTO dialogAddDTO = new DialogAddDTO() { }; _dbContextMock.Setup(r => r.SaveChangesAsync(CancellationToken.None)).Callback(() => throw new Exception()); ServiceResponse <DialogGetDTO> serviceResponse = await _service.CreateDialogAsync(dialogAddDTO); Assert.Null(serviceResponse.Data); Assert.False(serviceResponse.Success); Assert.NotNull(serviceResponse.Message); }
public async Task CreateDialogAsyncAddsDialogCallsSaveChanges_and_ReturnsServiceResponseWithData() { DialogAddDTO dialogAddDTO = new DialogAddDTO() { Title = "some_title" }; ServiceResponse <DialogGetDTO> serviceResponse = await _service.CreateDialogAsync(dialogAddDTO); _dialogRepositoryMock.Verify(r => r.Add(It.IsAny <Dialog>()), Times.Once); _dbContextMock.Verify(r => r.SaveChangesAsync(CancellationToken.None), Times.AtLeast(1)); Assert.IsAssignableFrom <ServiceResponse <DialogGetDTO> >(serviceResponse); Assert.NotNull(serviceResponse.Data); Assert.True(serviceResponse.Success); Assert.Equal(dialogAddDTO.Title, serviceResponse.Data.Title); }