Exemple #1
0
        public void PostComment(CommentDto commentDto)
        {
            commentDto.Id          = Guid.NewGuid();
            commentDto.CommentDate = DateTime.Now;

            var comment = _mapper.Map <Comments>(commentDto);

            _context.Comments.Add(comment);
            _context.SaveChanges();
        }
        public void PostComment(CommentDto commentDto)
        {
            var authorId = _context.Authors.FirstOrDefault(a => a.Username.Equals(commentDto.Author)).Id;

            commentDto.AuthorId    = authorId;
            commentDto.Id          = Guid.NewGuid();
            commentDto.CommentDate = DateTime.Now;

            var comment = _mapper.Map <Comments>(commentDto);

            _context.Comments.Add(comment);
            _context.SaveChanges();
        }
Exemple #3
0
        public void SavePost(PostDto postDto, List <PostCategoryDto> postsCategoriesDto)
        {
            var post = _mapper.Map <Posts>(postDto);

            post.PostDate = DateTime.Now;

            var postCategories = _mapper.Map <IEnumerable <PostsCategories> >(postsCategoriesDto);

            _context.AddRange(postCategories);
            _context.Add(post);

            _context.SaveChanges();
        }
Exemple #4
0
        public bool Delete(string slug)
        {
            var blogPostTag = _context.BlogPostTags.Include(x => x.Slug).Where(x => x.Slug.Slug == slug).ToList();

            foreach (var post in blogPostTag)
            {
                _context.BlogPostTags.Remove(post);
                _context.SaveChanges();
            }
            var entity = _context.BlogPost.Where(x => x.Slug == slug).FirstOrDefault();

            if (entity != null)
            {
                _context.BlogPost.Remove(entity);
                _context.SaveChanges();
                return(true);
            }
            return(false);
        }
        public void SavePost(PostDto postDto)
        {
            var postsCategoriesDto = new List <PostCategoryDto>();

            postDto.Id = Guid.NewGuid();

            foreach (var categoryDto in postDto.Categories)
            {
                postsCategoriesDto.Add(new PostCategoryDto {
                    Id = Guid.NewGuid(), CategoryId = categoryDto.Id, PostId = postDto.Id
                });
            }

            var post = _mapper.Map <Posts>(postDto);

            post.PostDate = DateTime.Now;

            var postCategories = _mapper.Map <IEnumerable <PostsCategories> >(postsCategoriesDto);

            _context.AddRange(postCategories);
            _context.Add(post);

            _context.SaveChanges();
        }
 public void CreateCategories([FromBody] Categories category)
 {
     _context.Categories.Add(category);
     _context.SaveChanges();
 }
        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);
        }