Exemple #1
0
        public async Task <IActionResult> PutPost(Guid id, Post post)
        {
            if (id != post.Id)
            {
                return(BadRequest());
            }

            _context.Entry(post).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PostExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <ServiceResponse <GetPostDto> > AddPostCategory(AddPostCategoryDto newPostCategory)
        {
            ServiceResponse <GetPostDto> response = new ServiceResponse <GetPostDto>();

            try
            {
                Post post = await _context.Posts
                            .FirstOrDefaultAsync(c => c.PostId == newPostCategory.PostId);

                if (post == null)
                {
                    response.Success = false;
                    response.Message = "Post not found.";
                    return(response);
                }
                Category category = await _context.Categories
                                    .FirstOrDefaultAsync(s => s.CategoryId == newPostCategory.CategoryId);

                if (category == null)
                {
                    response.Success = false;
                    response.Message = "Category not found.";
                    return(response);
                }
                PostCategories postCategory = new PostCategories
                {
                    Post     = post,
                    Category = category
                };

                await _context.PostCategories.AddAsync(postCategory);

                await _context.SaveChangesAsync();

                response.Data = _mapper.Map <GetPostDto>(post);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return(response);
        }
Exemple #3
0
        public async Task <ServiceResponse <Post> > CreatePost(AddPostDto newPost)
        {
            ServiceResponse <Post> response = new ServiceResponse <Post>();

            try
            {
                var author = await _authorService.FindAuthorById(newPost.AuthorId);

                var post = new Post()
                {
                    Title        = newPost.Title,
                    Summary      = newPost.Summary,
                    Body         = newPost.Body,
                    LastModified = newPost.LastModified
                };

                _context.Posts.Add(post);
                post.Author = author;
                await _context.SaveChangesAsync();

                //now checks which category has been specified
                if (!string.IsNullOrEmpty(newPost.Category))
                {
                    ////finds the category object that corresponds to the category name received
                    ////just one category is added from the frontend
                    var query = await _categoryService.FindCategoryByName(newPost.Category);

                    if (!query.Success)
                    {
                        response.Message = "There has been a problem retrieving the category";
                    }

                    var category     = query.Data.FirstOrDefault();
                    var postCategory = new PostCategories
                    {
                        Post     = post,
                        Category = category
                    };
                    _context.PostCategories.Add(postCategory);
                }
                else
                {
                    //        //assign the default category as General that is the category with Id 1
                    var category = await _context.Categories.Where(c => c.CategoryId == 1)
                                   .ToListAsync();

                    var postCategory = new PostCategories
                    {
                        Post     = post,
                        Category = category.FirstOrDefault()
                    };
                    _context.PostCategories.Add(postCategory);
                }
                await _context.SaveChangesAsync();

                response.Data = _mapper.Map <Post>(newPost);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return(response);
        }