Esempio n. 1
0
        public async Task <IActionResult> Edit(string id, [Bind("Type,PostId,UserId,IsDeleted,DeletedOn,Id,CreatedOn,ModifiedOn")] ForumVote forumVote)
        {
            if (id != forumVote.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(forumVote);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ForumVoteExists(forumVote.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["PostId"] = new SelectList(_context.ForumPosts, "Id", "Id", forumVote.PostId);
            ViewData["UserId"] = new SelectList(_context.Users, "Id", "Id", forumVote.UserId);
            return(View(forumVote));
        }
Esempio n. 2
0
        public async Task <IActionResult> Create([Bind("Type,PostId,UserId,IsDeleted,DeletedOn,Id,CreatedOn,ModifiedOn")] ForumVote forumVote)
        {
            if (ModelState.IsValid)
            {
                _context.Add(forumVote);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["PostId"] = new SelectList(_context.ForumPosts, "Id", "Id", forumVote.PostId);
            ViewData["UserId"] = new SelectList(_context.Users, "Id", "Id", forumVote.UserId);
            return(View(forumVote));
        }
Esempio n. 3
0
        public async Task <int> CreateAsync(int voteType, string postId, string userId)
        {
            var vote = new ForumVote
            {
                PostId = postId,
                UserId = userId,
                Type   = (VoteType)voteType,
            };

            await this.forumVotesRepository.AddAsync(vote);

            await this.forumVotesRepository.SaveChangesAsync();

            return(this.forumVotesRepository
                   .AllWithDeleted()
                   .Where(v => v.PostId == vote.PostId)
                   .Select(v => (int)v.Type).Sum());
        }
Esempio n. 4
0
        public async Task VoteAsync(int postId, string userId, bool isUpVote)
        {
            var vote = this.votesRepository.All()
                       .FirstOrDefault(x => x.PostId == postId && x.UserId == userId);

            if (vote != null)
            {
                vote.Type = isUpVote ? VoteType.UpVote : VoteType.DownVote;
            }
            else
            {
                vote = new ForumVote
                {
                    PostId = postId,
                    UserId = userId,
                    Type   = isUpVote ? VoteType.UpVote : VoteType.DownVote,
                };

                await this.votesRepository.AddAsync(vote);
            }

            await this.votesRepository.SaveChangesAsync();
        }