コード例 #1
0
        public IActionResult DeleteVote(int userId, int suggestionId)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            //search first if the user has already voted a suggestion
            VMVote vote = new VMVote
            {
                SuggestionId = suggestionId,
                UserId       = userId
            };

            if (_repository.isFullfilled(suggestionId))
            {
                return(StatusCode(406));
            }
            if (_repository.IsAuthor(vote))
            {
                return(BadRequest());
            }
            if (_repository.IsVote(vote))
            {
                _repository.VoteDelete(vote);
                return(Ok("Delete Successfully"));
            }
            else
            {
                return(BadRequest());
            }
        }
コード例 #2
0
        // get suggestion with the attribute bool alreadyvoted
        public override VMSuggestion Select(VMVote votes, string tenant)
        {
            User       user             = _dataContext.User.Find(votes.UserId);
            Tenants    tenantencontrado = _CRUDManagerTenant.Find(t => t.User.Tenant.URLOrigin == tenant);
            Suggestion row = _models.Include(r => r.Author).Include(s => s.Status).SingleOrDefault(x => x.Id == votes.SuggestionId);

            if (row.Deleted == true)
            {
                return(null);
            }

            if (row != null && row.Author.TenantId == tenantencontrado.Id)
            {
                VMSuggestion vmSuggestion = new VMSuggestion()
                {
                    Id           = row.Id,
                    Title        = row.Title,
                    Description  = row.Description,
                    QuantityVote = row.QuantityVote,
                    CreatedDate  = row.CreatedDate,
                    UpdatedDate  = row.UpdatedDate,
                    Author       = new VMUserSimple()
                    {
                        Id = row.Author.Id, FullName = row.Author.Name + " " + row.Author.LastName
                    },
                    Status = new VMStatus()
                    {
                        Id = row.Status.Id, Description = row.Status.Description
                    },
                    IsVoted = IsVote(votes)
                };

                if (row.StatusId == 1 || row.StatusId == 3)
                {
                    if (user.RoleId == 2 || user.RoleId == 3)
                    {
                        return(vmSuggestion);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(vmSuggestion);
                }
            }
            else
            {
                return(null);
            }
        }
コード例 #3
0
        public override void VoteDelete(VMVote vote)
        {
            Votes model = _voteModels.FirstOrDefault(u => u.UserId == vote.UserId && u.SuggestionId == vote.SuggestionId);

            if (_dataContext.Entry(model).State == EntityState.Detached)
            {
                _voteModels.Attach(model);
            }
            _voteModels.Remove(model);
            Suggestion suggestion = _models.Find(vote.SuggestionId);

            suggestion.QuantityVote--;
            _models.Update(suggestion);
            _dataContext.SaveChanges();
        }
コード例 #4
0
 public IActionResult Vote([FromBody] VMVote vmvote)
 {
     //search first if the user has already voted a suggestion
     if (_repository.IsVote(vmvote) || _repository.IsAuthor(vmvote))
     {
         return(BadRequest());
     }
     if (_repository.isFullfilled(vmvote.SuggestionId))
     {
         return(StatusCode(406));
     }
     if (_repository.Select(vmvote.SuggestionId) == null)
     {
         return(NotFound());
     }
     _repository.Vote(vmvote);
     return(Ok("Voted Successfully."));
 }
コード例 #5
0
        public IActionResult Load(string tenant, int userId, int suggestionId)
        {
            var test = User.Claims.FirstOrDefault(x => x.Value == tenant);


            VMVote vmvote = new VMVote()
            {
                SuggestionId = suggestionId,
                UserId       = userId
            };
            VMSuggestion vm = _repository.Select(vmvote, tenant);

            if (vm == null)
            {
                return(BadRequest("Could not find suggestion."));
            }
            else
            {
                return(Ok(vm));
            }
        }
コード例 #6
0
        public override VMMessage Vote(VMVote votes)
        {
            Votes vote = new Votes
            {
                SuggestionId = votes.SuggestionId,
                UserId       = votes.UserId
            };

            try
            {
                _voteModels.Add(vote);
                Suggestion suggestion = _models.Find(votes.SuggestionId);
                suggestion.QuantityVote++;
                _models.Update(suggestion);
                _dataContext.SaveChanges();
                return(new VMMessage(State.Successful));
            }
            catch (Exception ex)
            {
                return(new VMMessage(State.Error, ex));
            }
        }
コード例 #7
0
        public override bool IsAuthor(VMVote vmvote)
        {
            Suggestion suggestion = _models.FirstOrDefault(u => u.AuthorId == vmvote.UserId && u.Id == vmvote.SuggestionId && u.QuantityVote != 0);

            return(suggestion != null);
        }
コード例 #8
0
        public override bool IsVote(VMVote vmvote)
        {
            Votes vote = _voteModels.FirstOrDefault(u => u.UserId == vmvote.UserId && u.SuggestionId == vmvote.SuggestionId);

            return(vote != null);
        }
コード例 #9
0
 public abstract VMSuggestion Select(VMVote votes, string tenant);
コード例 #10
0
 public abstract void VoteDelete(VMVote vote);
コード例 #11
0
 public abstract bool IsAuthor(VMVote vmvote);
コード例 #12
0
 public abstract bool IsVote(VMVote vmvote);
コード例 #13
0
 public abstract VMMessage Vote(VMVote votes);