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);
        }
示例#2
0
        public async Task <IActionResult> UpdateCategory(int id, [FromBody] PostCategoryDto categoryDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var response = await repository.UpdateCategory(id, categoryDto);

            return(Ok(response));
        }
示例#3
0
        public async Task <IActionResult> CreateCategory([FromBody] PostCategoryDto categoryDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var category = await repository.CreateCategory(categoryDto);

            return(CreatedAtRoute("GetCategory", new { id = category.Id }, category));
        }
示例#4
0
        public async Task <Category> CreateCategory(PostCategoryDto dto)
        {
            var category = mapper.Map <PostCategoryDto, Category>(dto);

            await context.Categories.AddAsync(category);

            await context.SaveChangesAsync();

            await fieldRepository.AddOrUpdateFields(category, dto.AdditionalFields);

            return(category);
        }
示例#5
0
        public async Task <IActionResult> Post([FromBody] PostCategoryDto model)
        {
            try
            {
                await _postCategoryCommanderServices.CreateAsync(model);

                return(Ok(model));
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(BadRequest());
            }
        }
示例#6
0
        public async Task <bool> UpdateCategory(int id, PostCategoryDto dto)
        {
            var category = await GetCategory(id);

            if (category == null)
            {
                throw new Exception("Category does not exist.");
            }

            mapper.Map <PostCategoryDto, Category>(dto, category);

            var response = await fieldRepository.AddOrUpdateFields(category, dto.AdditionalFields);

            return(response);
        }
        public async Task <IActionResult> AddPostCategory(PostCategoryDto postCategoryDto)
        {
            try
            {
                var postCategory = _mapper.Map <PostCategory>(postCategoryDto);
                _postCategoryService.AddPostCategory(postCategory);
                await _unitOfWork.Save();

                return(Ok(postCategory));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
        public async Task <ServiceResponse <List <GetCategoryDto> > > Add(PostCategoryDto categoryDto)
        {
            ServiceResponse <List <GetCategoryDto> > serviceResponse = new ServiceResponse <List <GetCategoryDto> >();

            try
            {
                await _context.Category.AddAsync(_mapper.Map <Category>(categoryDto));

                await _context.SaveChangesAsync();

                serviceResponse.Data = await GetAllCategories();
            }
            catch (Exception e)
            {
                serviceResponse.Success = false;
                serviceResponse.Message = e.Message;
            }

            return(serviceResponse);
        }
 public async Task <IActionResult> Add(PostCategoryDto categoryDto) => Ok(await _service.Add(categoryDto));
        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);
        }
        public async Task <IActionResult> UpdatePostCategoryById(long postCategoryId, PostCategoryDto postCategoryDto)
        {
            try
            {
                if (postCategoryId != postCategoryDto.Id)
                {
                    return(BadRequest());
                }
                var postCategory = await _postCategoryService.GetPostCategoryById(postCategoryId);

                if (postCategory == null)
                {
                    return(NotFound());
                }
                _mapper.Map(postCategoryDto, postCategory);
                _postCategoryService.UpdatePostCategory(postCategoryId, postCategory);
                await _unitOfWork.Save();

                return(NoContent());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }