//To isolate
        private int PostRemover(int postId, PaintStoreContext db)
        {
            var tempPost = db.Posts.First(x => x.Id == postId);

            UsersManager.UserPostsCountMinus(db, tempPost.UserId);

            var tagsToRemove = db.PostTags.Where(x => x.PostId == postId).ToList();

            foreach (var tagToRemove in tagsToRemove)
            {
                db.PostTags.Remove(tagToRemove);
                TagsManager.TagsCountMinus(db, tagToRemove.TagId);
            }

            db.Posts.Remove(db.Posts.First(x => x.Id == postId));
            foreach (var like in db.PostLikes.Where(x => x.PostId == postId))
            {
                db.PostLikes.Remove(db.PostLikes.First(x => x.Id == like.Id));
            }

            foreach (var comment in db.PostComments.Where(x => x.PostId == postId))
            {
                db.PostComments.Remove(db.PostComments.First(x => x.Id == comment.Id));
                foreach (var like in db.CommentLikes.Where(x => x.CommentId == comment.Id))
                {
                    db.CommentLikes.Remove(db.CommentLikes.First(x => x.Id == like.Id));
                }
            }

            _cloudinaryService.DeleteImage(tempPost.ImgLink);

            return(postId);
        }
Example #2
0
        public int AddPostTags(List <string> tagsList, int postId)
        {
            using (var db = _paintStoreContext)
            {
                var tagsToRemove = db.PostTags.Where(x => x.PostId == postId).ToList();
                foreach (var tagToRemove in tagsToRemove)
                {
                    db.PostTags.Remove(tagToRemove);
                    TagsManager.TagsCountMinus(db, tagToRemove.TagId);
                }

                foreach (var tag in tagsList)
                {
                    var tagToAdd = GetOrAddTag(tag, db);
                    db.PostTags.Add(new PostTags {
                        PostId = postId, TagId = tagToAdd.Id
                    });
                    TagsManager.TagsCountPlus(db, tagToAdd.Id);
                }
                db.SaveChanges();
            }
            return(postId);
        }