Пример #1
0
        public async Task CreateAsync(CreateVoteDTO vote)
        {
            var existingVote = this.votesRepository.All()
                               .Where(x => x.ForUserId == vote.ForUserId && x.VoterId == vote.VoterId)
                               .FirstOrDefault();

            if (existingVote == null)
            {
                var voteEntity = new Vote
                {
                    ForUserId = vote.ForUserId,
                    VoterId   = vote.VoterId,
                    VoteValue = vote.VoteValue,
                };

                await this.votesRepository.AddAsync(voteEntity);
            }
            else if (existingVote.VoteValue == vote.VoteValue)
            {
                this.votesRepository.HardDelete(existingVote);
            }
            else if (existingVote.VoteValue != vote.VoteValue)
            {
                existingVote.VoteValue = vote.VoteValue;
            }

            await this.votesRepository.SaveChangesAsync();
        }
Пример #2
0
        public async Task <IActionResult> Add([FromBody] CreateVoteInputModel vote)
        {
            var voterId   = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;
            var forUserId = this.usersService.GetUserIdByUsername(vote.ForUserUserName);

            var voteDTO = new CreateVoteDTO
            {
                ForUserId = forUserId,
                VoterId   = voterId,
                VoteValue = vote.VoteValue,
            };

            await this.votesService.CreateAsync(voteDTO);

            return(this.Ok());
        }