public ActionResult _Register(int topicID, VoteKind vote, VoteLinkLevel linkLevel) { if (db.Topics.Find(topicID).IsReadOnly) return HTTPStatus(HttpStatusCode.Forbidden, "Das Thema ist schreibgeschützt."); int cuid = GetCurrentUserID(); Vote dbvote = db.Votes.SingleOrDefault(v => v.Voter.ID == cuid && v.Topic.ID == topicID); if (dbvote == null) return HTTPStatus(HttpStatusCode.Forbidden, "Es liegt keine Stimmberechtigung vor."); dbvote.Kind = vote; db.SaveChanges(); return _List(db.Topics.Find(topicID), linkLevel); }
public ActionResult _Register2(int topicID, int voterID, VoteKind vote, VoteLinkLevel linkLevel) { if (db.Topics.Find(topicID).IsReadOnly) return HTTPStatus(HttpStatusCode.Forbidden, "Das Thema ist schreibgeschützt."); if (IsTopicLocked(topicID)) return HTTPStatus(HttpStatusCode.Forbidden, "Das Thema ist gesperrt."); User voter = db.Users.Find(voterID); db.Votes.Single(v => v.Voter.ID == voter.ID && v.Topic.ID == topicID).Kind = vote; string message = string.Format("{0} hat in Vertretung für {1} abgestimmt mit \"{2}\".", GetCurrentUser().ShortName, voter.ShortName, vote.DisplayName()); db.Comments.Add(new Comment {Author = db.Users.Find(voterID), TopicID = topicID, Content = message}); db.SaveChanges(); return _List(db.Topics.Find(topicID), linkLevel); }
public Vote(User voter, VoteKind vote) { Voter = voter; Kind = vote; }
public object Any(Vote request) { Guid userId = UserSession.GetUserId(); VoteResponse response = new VoteResponse { Result = ErrorCode.OK, }; TableRepository tableRepository = new TableRepository(); UserQuestionEntry userQuestionEntry = tableRepository.Get <UserQuestionEntry>(Tables.UserQuestion, request.QuestionId, userId); // Création d'un nouveau vote if (userQuestionEntry == null) { DateTime dateTime = DateTime.UtcNow; userQuestionEntry = new UserQuestionEntry(request.QuestionId, userId) { Creation = dateTime, Modification = dateTime }; } else { userQuestionEntry.Modification = DateTime.UtcNow; } Guid target = request.VoteTarget == VoteTarget.Question ? request.QuestionId : request.OwnerId; HashSet <string> votesUp = new HashSet <string>(userQuestionEntry.VotesUp?.Split('|') ?? new string[] { }); HashSet <string> votesDown = new HashSet <string>(userQuestionEntry.VotesDown?.Split('|') ?? new string[] { }); VoteKind oldValue = VoteKind.None; if (votesUp.Contains(target.ToString())) { oldValue = VoteKind.Up; } else if (votesDown.Contains(target.ToString())) { oldValue = VoteKind.Down; } response.VoteKind = request.VoteKind; votesUp.Remove(target.ToString()); votesDown.Remove(target.ToString()); if (response.VoteKind == oldValue) { response.VoteKind = VoteKind.None; } else { switch (response.VoteKind) { case VoteKind.Up: votesUp.Add(target.ToString()); break; case VoteKind.Down: votesDown.Add(target.ToString()); break; } } userQuestionEntry.VotesUp = votesUp.Join("|"); userQuestionEntry.VotesDown = votesDown.Join("|"); if (request.VoteTarget == VoteTarget.Answer) { AnswerEntry answerEntry = tableRepository.Get <AnswerEntry>(Tables.Answers, request.QuestionId, request.OwnerId); answerEntry.Votes -= (int)oldValue; answerEntry.Votes += (int)response.VoteKind; tableRepository.InsertOrMerge(answerEntry, Tables.Answers); response.VoteValue = answerEntry.Votes; // badges if (response.VoteKind == VoteKind.Up) { switch (answerEntry.Votes) { case 1: AllBadges.Approved.CreateIfNotExist(tableRepository, answerEntry.GetAnswerOwnerId()); break; case 10: AllBadges.NiceAnswer.CreateForQuestion(tableRepository, answerEntry.GetAnswerOwnerId(), request.QuestionId); break; case 25: AllBadges.GoodAnswer.CreateForQuestion(tableRepository, answerEntry.GetAnswerOwnerId(), request.QuestionId); break; case 100: AllBadges.GreatAnswer.CreateForQuestion(tableRepository, answerEntry.GetAnswerOwnerId(), request.QuestionId); break; } } } else { QuestionEntry questionEntry = tableRepository.Get <QuestionEntry>(Tables.Questions, request.OwnerId, request.QuestionId); questionEntry.Votes -= (int)oldValue; questionEntry.Votes += (int)response.VoteKind; tableRepository.InsertOrMerge(questionEntry, Tables.Questions); response.VoteValue = questionEntry.Votes; // badges if (response.VoteKind == VoteKind.Up) { switch (questionEntry.Votes) { case 1: AllBadges.Padawan.CreateIfNotExist(tableRepository, questionEntry.GetOwnerId()); break; case 10: AllBadges.NiceQuestion.CreateForQuestion(tableRepository, questionEntry.GetOwnerId(), request.QuestionId); break; case 25: AllBadges.GoodQuestion.CreateForQuestion(tableRepository, questionEntry.GetOwnerId(), request.QuestionId); break; case 100: AllBadges.GreatQuestion.CreateForQuestion(tableRepository, questionEntry.GetOwnerId(), request.QuestionId); break; } } } // insert le vote tableRepository.InsertOrReplace(userQuestionEntry, Tables.UserQuestion); // badges if (response.VoteKind == VoteKind.Up) { AllBadges.Supporter.CreateIfNotExist(tableRepository, userId); } else if (response.VoteKind == VoteKind.Down) { AllBadges.Critic.CreateIfNotExist(tableRepository, userId); } return(response); }