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()); } }
// 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); } }
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(); }
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.")); }
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)); } }
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)); } }
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); }
public override bool IsVote(VMVote vmvote) { Votes vote = _voteModels.FirstOrDefault(u => u.UserId == vmvote.UserId && u.SuggestionId == vmvote.SuggestionId); return(vote != null); }
public abstract VMSuggestion Select(VMVote votes, string tenant);
public abstract void VoteDelete(VMVote vote);
public abstract bool IsAuthor(VMVote vmvote);
public abstract bool IsVote(VMVote vmvote);
public abstract VMMessage Vote(VMVote votes);