public IActionResult GetPaginationPhotos(PaginationPhotosRequestDto paginationPhotosRequestDto) { try { var pagination = _photoService.GetPaginationPhotos(paginationPhotosRequestDto); return(Ok(new { pagination })); } catch (InvalidOperationException invalidOperationException) { return(NoContent()); } catch (InvalidDataException invalidOperationException) { return(BadRequest(new { invalidOperationException.Message })); } catch (Exception exeption) { return(new ObjectResult(new { Error = exeption.Message }) { StatusCode = StatusCodes.Status500InternalServerError }); } }
/// <summary> /// Get collection of pagination photos of specific user by its id, the number of all pages, the number of all photos, the number of specific page. /// </summary> /// <param name="id"><see cref="Guid"/> user id.</param> /// <param name="paginationPhotosRequestDto">Object of type <see cref="PaginationPhotosRequestDto"/>.</param> /// <returns>Returns objects of type <see cref="PaginationPhotosResponseDto"/>.</returns> /// <exception cref="InvalidOperationException">Thrown when user with this id hasn`t photos.</exception> public PaginationPhotosResponseDto GetPaginationPhotosByUserId(Guid id, PaginationPhotosRequestDto paginationPhotosRequestDto) { try { var allPhotosCountByUserId = _dataService.GetCountAllPhotosByUserId(id); int skipNumber = (paginationPhotosRequestDto.PageNumber - 1) * paginationPhotosRequestDto.PageSize; int takeNumber = paginationPhotosRequestDto.PageSize; var photoFromDb = _dataService.GetPaginationPhotosByUserId(id, skipNumber, takeNumber); if (photoFromDb.Count() == 0) { throw new InvalidOperationException(); } var allPagesNumber = _dataService.GetAllPagesNumber(allPhotosCountByUserId, paginationPhotosRequestDto.PageSize); return(new PaginationPhotosResponseDto { AllPagesNumber = allPagesNumber, AllPhotosNumber = allPhotosCountByUserId, Photos = photoFromDb }); } catch (InvalidOperationException invalidOperationException) { throw invalidOperationException; } }
public void GetPaginationPhotos_EmptyContainerOfPhotos_ThrowInvalidOperationException() { var authServiceMock = new Mock <IAuthService>(); var dataServiceMock = new Mock <IDataService>(); var paginationPhotosRequestDto = new PaginationPhotosRequestDto() { PageNumber = 2, PageSize = 10 }; dataServiceMock.Setup(u => u.GetPaginationPhotos((paginationPhotosRequestDto.PageNumber - 1) * paginationPhotosRequestDto.PageSize, paginationPhotosRequestDto.PageSize)) .Throws(new InvalidDataException("Photos don`t exist.")); var controller = new PhotosController(dataServiceMock.Object, authServiceMock.Object); var result = controller.GetPaginationPhotos(paginationPhotosRequestDto); var statusCode = ((BadRequestObjectResult)result).StatusCode; var jsonValue = JsonConvert.SerializeObject(((BadRequestObjectResult)result).Value); var dictionary = JsonConvert.DeserializeObject <Dictionary <object, object> >(jsonValue); Assert.True(statusCode == 400); Assert.Equal("Photos don`t exist.", dictionary["Message"]); dataServiceMock.VerifyAll(); }
/// <summary> /// Get collection of pagination photos of specific user by its id, the number of all pages, the number of all photos, the number of specific page. /// </summary> /// <param name="paginationGettingDto">Object of type <see cref="PaginationPhotosRequestDto"/>.</param> /// <returns>Returns objects of type <see cref="PaginationPhotosResponseDto"/>.</returns> /// <exception cref="InvalidDataException">Thrown when photo doesn`t exist.</exception> /// <exception cref="InvalidOperationException">Thrown when number of photos returned is zero.</exception> public PaginationPhotosResponseDto GetPaginationPhotos(PaginationPhotosRequestDto paginationGettingDto) { try { var allPhotosNumber = _dataService.GetCountAllPhotos(); var allPagesNumber = _dataService.GetAllPagesNumber(allPhotosNumber, paginationGettingDto.PageSize); int skipNumber = (paginationGettingDto.PageNumber - 1) * paginationGettingDto.PageSize; int takeNumber = paginationGettingDto.PageSize; var photosFromDb = _dataService.GetPaginationPhotos(skipNumber, takeNumber); if (photosFromDb == null) { throw new InvalidDataException("Photos don`t exist."); } if (photosFromDb.Count() == 0) { throw new InvalidOperationException(); } var pageNumber = paginationGettingDto.PageNumber; return(new PaginationPhotosResponseDto { AllPhotosNumber = allPhotosNumber, AllPagesNumber = allPagesNumber, Photos = photosFromDb, PageNumber = pageNumber }); } catch (InvalidOperationException invalidOperationException) { throw invalidOperationException; } catch (InvalidDataException invalidDataException) { throw invalidDataException; } }
public void GetAllUserPhotos_UserWithoutPhotos_ThrowInvalidOperationException() { var authServiceMock = new Mock <IAuthService>(); var dataServiceMock = new Mock <IDataService>(); Guid userId = Guid.NewGuid(); var paginationPhotosRequestDto = new PaginationPhotosRequestDto() { PageNumber = 2, PageSize = 10 }; dataServiceMock.Setup(u => u.GetPaginationPhotosByUserId(userId, (paginationPhotosRequestDto.PageNumber - 1) * paginationPhotosRequestDto.PageSize, paginationPhotosRequestDto.PageSize)) .Throws(new InvalidOperationException()); var controller = new PhotosController(dataServiceMock.Object, authServiceMock.Object); var context = new ControllerContext() { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(new ClaimsIdentity(new List <Claim> { new Claim(ClaimTypes.Name, userId.ToString()) }, "Bearer")) } }; controller.ControllerContext = context; var result = controller.GetAllUserPaginationPhotos(paginationPhotosRequestDto); var statusCode = ((NoContentResult)result).StatusCode; Assert.True(statusCode == 204); dataServiceMock.VerifyAll(); }
public IActionResult GetAllUserPaginationPhotos(PaginationPhotosRequestDto paginationDto) { try { var pagination = _photoService.GetPaginationPhotosByUserId(Guid.Parse(User.Identity.Name), paginationDto); return(Ok(new { pagination })); } catch (InvalidOperationException invalidOperationException) { return(NoContent()); } catch (Exception exeption) { return(new ObjectResult(new { Error = exeption.Message }) { StatusCode = StatusCodes.Status500InternalServerError }); } }