예제 #1
0
        public void MuteUser(int subRedditId, int userId)
        {
            RedditDb context = new RedditDb();

            User user = context.Users.Find(userId);

            SubReddit subReddit = context.SubReddits.Find(subRedditId);

            subReddit.MutedUsers.Add(user);
            context.SaveChanges();
        }
예제 #2
0
        public void AddAdminToSubReddit(int subRedditId, int userId)
        {
            RedditDb context = new RedditDb();

            User user = context.Users.Find(userId);

            SubReddit subReddit = context.SubReddits.Find(subRedditId);

            user.AdminToSubReddits.Add(subReddit);
            subReddit.Admins.Add(user);
            context.SaveChanges();
        }
예제 #3
0
        public void UnSubscribe(int subRedditId, int userId)
        {
            RedditDb context = new RedditDb();

            User user = context.Users.Find(userId);

            SubReddit subReddit = context.SubReddits.Find(subRedditId);

            user.SubscribedToSubReddits.Remove(subReddit);
            subReddit.SubscribedUsers.Remove(user);
            context.SaveChanges();
        }
예제 #4
0
        public void ApprovePost(int postId, int userId)
        {
            RedditDb context = new RedditDb();

            User user = context.Users.Find(userId);
            Post post = context.Posts.Find(postId);

            if (context.Users.AsNoTracking().Where(u => u.AdminToSubReddits.Any(sr => sr.Admins.Any(us => us.Id == userId))).Count() > 0)
            {
                try
                {
                    post.IsApproved = true;
                    context.Entry(post).Property(x => x.IsApproved).IsModified = true;
                    context.SaveChanges();
                }

                catch (Exception)
                {
                }
            }
        }
예제 #5
0
        public void Vote(int userId, int contentId, int value, string type)
        {
            PostsRepository    postsRepo    = new PostsRepository();
            CommentsRepository commentsRepo = new CommentsRepository();
            VotesRepository    votesRepo    = new VotesRepository();
            UsersRepository    usersRepo    = new UsersRepository();

            RedditDb context = new RedditDb();

            //ако не съществува такъв запис в таблица Votes, се създава, а рейтингът на поста се увеличава със стойността на вота
            if (context.Votes.Where(a => a.UserId == userId && a.ContentId == contentId && a.Type.ToString() == type).Count() == 0)
            {
                if (type == "Post")
                {
                    Post post = postsRepo.GetById(contentId);
                    post.Rating += value;
                    postsRepo.Save(post);

                    usersRepo.ChangeKarma(post.UserId, value);

                    votesRepo.Insert(new Vote()
                    {
                        UserId = userId, ContentId = contentId, CreationDate = DateTime.Now, Value = value, Type = Entities.Type.Post
                    });
                    context.SaveChanges();
                }
                else if (type == "Comment")
                {
                    Comment comment = commentsRepo.GetById(contentId);
                    comment.Rating += value;
                    commentsRepo.Save(comment);

                    usersRepo.ChangeKarma(comment.UserId, value);

                    votesRepo.Insert(new Vote()
                    {
                        UserId = userId, ContentId = contentId, CreationDate = DateTime.Now, Value = value, Type = Entities.Type.Comment
                    });
                    context.SaveChanges();
                }
                else
                {
                    throw new Exception(message: "Something went wrong :(");
                }
            }

            //ако обаче съществува такъв, проверява дали стойността на вота е същата. Ако е - изтрива записа, ако не е - го променя
            else
            {
                if (context.Votes.Any(v => v.UserId == userId && v.ContentId == contentId && v.Value == value && v.Type.ToString() == type))
                {
                    if (type == "Post")
                    {
                        Post post = postsRepo.GetById(contentId);
                        post.Rating -= value;
                        postsRepo.Save(post);

                        usersRepo.ChangeKarma(post.UserId, -1 * value);

                        Vote vote = votesRepo.GetAll(v => v.UserId == userId && v.ContentId == contentId && v.Value == value && v.Type.ToString() == type).FirstOrDefault();
                        votesRepo.Delete(vote);
                        context.SaveChanges();
                    }
                    else if (type == "Comment")
                    {
                        Comment comment = commentsRepo.GetById(contentId);
                        comment.Rating -= value;
                        commentsRepo.Save(comment);

                        usersRepo.ChangeKarma(comment.UserId, -1 * value);

                        Vote vote = votesRepo.GetAll(v => v.UserId == userId && v.ContentId == contentId && v.Value == value && v.Type.ToString() == type).FirstOrDefault();
                        votesRepo.Delete(vote);
                        context.SaveChanges();
                    }
                    else
                    {
                        throw new Exception(message: "Something went wrong :(");
                    }
                }

                else
                {
                    if (type == "Post")
                    {
                        Post post = postsRepo.GetById(contentId);
                        post.Rating += 2 * value;
                        postsRepo.Save(post);

                        usersRepo.ChangeKarma(post.UserId, 2 * value);

                        Vote vote = votesRepo.GetAll(v => v.UserId == userId && v.ContentId == contentId && v.Value != value && v.Type.ToString() == type).FirstOrDefault();
                        vote.Value += 2 * value;
                        votesRepo.Update(vote);
                        context.SaveChanges();
                    }
                    else if (type == "Comment")
                    {
                        Comment comment = commentsRepo.GetById(contentId);
                        comment.Rating += 2 * value;
                        commentsRepo.Save(comment);

                        usersRepo.ChangeKarma(comment.UserId, 2 * value);

                        Vote vote = votesRepo.GetAll(v => v.UserId == userId && v.ContentId == contentId && v.Value != value && v.Type.ToString() == type).FirstOrDefault();
                        vote.Value += 2 * value;
                        votesRepo.Update(vote);
                        context.SaveChanges();
                    }
                }
            }
        }