Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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));
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        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);
        }