public async Task <IActionResult> PostPostsCategories([FromBody] PostsCategories postsCategories) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _context.PostsCategories.Add(postsCategories); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (PostsCategoriesExists(postsCategories.PostId)) { return(new StatusCodeResult(StatusCodes.Status409Conflict)); } else { throw; } } return(CreatedAtAction("GetPostsCategories", new { id = postsCategories.PostId }, postsCategories)); }
public async Task <IActionResult> PutPostsCategories([FromRoute] Guid id, [FromBody] PostsCategories postsCategories) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != postsCategories.PostId) { return(BadRequest()); } _context.Entry(postsCategories).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PostsCategoriesExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public void SavePost_SavesPost() { TestAuthor = new Authors() { Id = Guid.NewGuid(), FirstName = "Jan", LastName = "Podskalicky", Email = "*****@*****.**", Password = "******", Phone = "0", Username = "******" }; testDBContext.Authors.Add(TestAuthor); TestCategory = new Categories { Id = Guid.NewGuid(), Name = "Test category" }; testDBContext.Categories.Add(TestCategory); testDBContext.SaveChanges(); var testPostDto = new PostDto() { Id = Guid.NewGuid(), Title = "Test Post 1", Perex = "This is a test post 1", Content = "This is a text of test post 1", Author = testAutoMapper.Map <AuthorDto>(TestAuthor), Categories = new List <CategoryDto>() { testAutoMapper.Map <CategoryDto>(TestCategory), } }; TestPostsCategories = new PostsCategories { Id = Guid.NewGuid(), CategoryId = TestCategory.Id, PostId = testPostDto.Id }; TestPostsCategoriesDto = testAutoMapper.Map <PostCategoryDto>(TestPostsCategories); var testPostManager = new PostManager(testDBContext, testAutoMapper); testPostManager.SavePost(testPostDto, new List <PostCategoryDto> { TestPostsCategoriesDto }); var retrievedPost = testPostManager.GetPostById(testPostDto.Id); testPostDto.AssertAreEqual(retrievedPost); }
private void AddTestPostsToDB() { TestAuthor = new Authors() { Id = Guid.NewGuid(), FirstName = "Jan", LastName = "Podskalicky", Email = "*****@*****.**", Password = "******", Phone = "0", Username = "******" }; testDBContext.Authors.Add(TestAuthor); TestCategory = new Categories { Id = Guid.NewGuid(), Name = "Test category" }; testDBContext.Categories.Add(TestCategory); TestPost1 = new Posts() { Id = Guid.NewGuid(), Title = "Test Post 1", Perex = "This is a test post 1", PostText = "This is a text of test post 1", PostDate = DateTime.Now, AuthorId = TestAuthor.Id }; TestPost2 = new Posts() { Id = Guid.NewGuid(), Title = "Test Post 2", Perex = "This is a test post 2", PostText = "This is a text of test post 2", PostDate = DateTime.Now, AuthorId = TestAuthor.Id }; testDBContext.Posts.Add(TestPost1); testDBContext.Posts.Add(TestPost2); TestPostsCategories = new PostsCategories { Id = Guid.NewGuid(), CategoryId = TestCategory.Id, PostId = TestPost1.Id }; testDBContext.PostsCategories.Add(TestPostsCategories); testDBContext.SaveChanges(); TestPostDto1 = testAutoMapper.Map <PostDto>(TestPost1); TestPostDto2 = testAutoMapper.Map <PostDto>(TestPost2); TestAuthorDto = testAutoMapper.Map <AuthorDto>(TestAuthor); TestPostsCategoriesDto = testAutoMapper.Map <PostCategoryDto>(TestPostsCategories); TestCategoryDto = testAutoMapper.Map <CategoryDto>(TestCategory); }