public async Task PutPage_Valid_Returns200OkAsync() { var filters = new Models.PageOptionFilter { PageId = 1, }; var savePage = new SavePage() { BookId = 1, Name = "x", Description = "x", }; var page = new List <Models.Page> { new Models.Page() { PageId = 1 } }; this.PageRepositoryMock .Setup(x => x.GetAsync(It.IsAny <Models.PageOptionFilter>(), It.IsAny <CancellationToken>())) .ReturnsAsync(page); this.PageRepositoryMock.Setup(x => x.UpdateAsync(page.First(), It.IsAny <CancellationToken>())).ReturnsAsync(page.First()); var response = await this.client.PutAsJsonAsync("page/1", savePage).ConfigureAwait(false); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(ContentType.RestfulJson, response.Content.Headers.ContentType?.MediaType); var pageViewModel = await response.Content.ReadAsAsync <Page>(this.formatters).ConfigureAwait(false); Assert.Equal(pageViewModel.PageId, page.First().PageId); }
public async Task PostPage_Valid_Returns201CreatedAsync() { var filters = new Models.PageOptionFilter { PageId = 1, }; var savePage = new SavePage() { BookId = 1, Name = "x", Description = "x", }; var page = new Models.Page() { PageId = 1 }; this.PageRepositoryMock .Setup(x => x.AddAsync(It.IsAny <Models.Page>(), It.IsAny <CancellationToken>())) .ReturnsAsync(page); var response = await this.client.PostAsJsonAsync("page", savePage).ConfigureAwait(false); Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal(ContentType.RestfulJson, response.Content.Headers.ContentType?.MediaType); var pageViewModel = await response.Content.ReadAsAsync <Page>(this.formatters).ConfigureAwait(false); var uriString = AddQueryString("/page", filters); Assert.Equal(new Uri($"http://localhost{uriString}"), response.Headers.Location); }
/// <summary> /// Execute async. /// </summary> /// <param name="savePage">The save page.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>An action result.</returns> public async Task <IActionResult> ExecuteAsync(SavePage savePage, CancellationToken cancellationToken) { var page = this.savePageToPageMapper.Map(savePage); page = await this.pageRepository.AddAsync(page, cancellationToken).ConfigureAwait(false); var pageViewModel = this.pageToPageMapper.Map(page); var filters = new Models.PageOptionFilter { PageId = pageViewModel.PageId }; return(new CreatedAtRouteResult( PageControllerRoute.GetPage, filters, pageViewModel)); }
/// <summary> /// Execute async. /// </summary> /// <param name="pageId">The page id.</param> /// <param name="savePage">The save page.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>A action result.</returns> public async Task <IActionResult> ExecuteAsync(long pageId, SavePage savePage, CancellationToken cancellationToken) { var filters = new Models.PageOptionFilter { PageId = pageId }; var page = await this.pageRepository.GetAsync(filters, cancellationToken).ConfigureAwait(false); if (page is null || !page.Any()) { return(new NotFoundResult()); } var item = page.First(); this.savePageToPageMapper.Map(savePage, item); item = await this.pageRepository.UpdateAsync(item, cancellationToken).ConfigureAwait(false); var pageViewModel = this.pageToPageMapper.Map(item); return(new OkObjectResult(pageViewModel)); }
public async Task PutPage_PageNotFound_Returns404NotFoundAsync() { var filters = new Models.PageOptionFilter { PageId = 999, }; var savePage = new SavePage() { BookId = 1, Name = "x", Description = "x", }; var page = new List <Models.Page>(); this.PageRepositoryMock.Setup(x => x.GetAsync(It.IsAny <Models.PageOptionFilter>(), It.IsAny <CancellationToken>())).ReturnsAsync(page); var response = await this.client.PutAsJsonAsync("page/999", savePage).ConfigureAwait(false); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); var problemDetails = await response.Content.ReadAsAsync <ProblemDetails>(this.formatters).ConfigureAwait(false); Assert.Equal(StatusCodes.Status404NotFound, problemDetails.Status); }
public Task <IActionResult> PutAsync( [FromServices] PutPageCommand command, long pageId, [FromBody] SavePage page, CancellationToken cancellationToken) => command.ExecuteAsync(pageId, page, cancellationToken);