Exemple #1
0
        public Post Update(string existingSlug, Dto.Post post)
        {
            var oldSlug = existingSlug?.ToLowerInvariant().Trim();
            var newSlug = post.Slug?.ToLowerInvariant().Trim();

            var existingPost = _context.Posts.SingleOrDefault(p => p.Slug == oldSlug);

            if (existingPost == null)
            {
                throw new PostNotFoundException(oldSlug);
            }

            // If the slug is changing, make sure it's still unique
            if (oldSlug != newSlug && _context.Posts.Any(p => p.Slug == newSlug))
            {
                throw new DuplicatePostException(newSlug);
            }

            existingPost.Slug          = newSlug;
            existingPost.Body          = post.Body;
            existingPost.DatePublished = post.DatePublished;
            existingPost.IsFeatured    = post.IsFeatured;
            existingPost.IsStatic      = post.IsStatic;
            existingPost.Title         = post.Title;

            _context.SaveChanges();

            return(existingPost);
        }
Exemple #2
0
        private ApiResponse EditPost(int id, EditPostRequest request)
        {
            var apiResp = new ApiResponse
            {
                ResponseCode = ResponseCode.Fail
            };

            var postDto = new Dto.Post
            {
                BlogId  = request.BlogId,
                Content = request.Content,
                Title   = request.Title,
                Id      = id
            };

            var resp = _postBusiness.Edit(postDto);

            if (resp.ResponseCode != ResponseCode.Success)
            {
                apiResp.ResponseMessage = resp.ResponseMessage;

                return(apiResp);
            }

            apiResp.ResponseCode = ResponseCode.Success;
            return(apiResp);
        }
Exemple #3
0
        public Post Create(Dto.Post post)
        {
            var slug = post.Slug?.ToLowerInvariant().Trim();

            if (string.IsNullOrEmpty(slug))
            {
                throw new Exception("Slug is required");
            }

            if (_context.Posts.Any(p => p.Slug == slug))
            {
                throw new DuplicatePostException(slug);
            }

            var newPost = new Post
            {
                Slug          = slug,
                Body          = post.Body,
                DateCreated   = post.DateCreated ?? DateTime.UtcNow,
                DatePublished = post.DatePublished,
                IsFeatured    = post.IsFeatured,
                IsStatic      = post.IsStatic,
                Title         = post.Title
            };

            _context.Posts.Add(newPost);
            _context.SaveChanges();

            return(newPost);
        }
Exemple #4
0
        private ApiResponse AddPost(NewPostRequest request)
        {
            var apiResp = new ApiResponse
            {
                ResponseCode = ResponseCode.Fail
            };

            var postDto = new Dto.Post
            {
                BlogId    = request.BlogId,
                Content   = request.Content,
                Title     = request.Title,
                CreatedAt = DateTime.Now
            };

            _postBusiness.Add(postDto);

            apiResp.ResponseCode = ResponseCode.Success;
            return(apiResp);
        }
Exemple #5
0
 public void Post(Dto.Post post)
 {
     _postBus.Create(post);
 }