Example #1
0
        public static void AddVoteToQuestion(VotingQuestion question, VoteType voteType)
        {
            switch (voteType)
            {
            case VoteType.Yes: question.SumYes += 1; break;

            case VoteType.No: question.SumNo += 1; break;

            case VoteType.Abstention: question.SumAbstention += 1; break;

            default: return;
            }
        }
Example #2
0
 public VotingQuestionWithAnswers(VotingQuestion question)
 {
     QuestionID          = question.QuestionID;
     ConferenceID        = question.ConferenceID;
     MajorityID          = question.MajorityID;
     QuestionText        = question.QuestionText;
     ArrivedCouncilCount = question.ArrivedCouncilCount;
     IsOpen        = question.IsOpen;
     IsSecret      = question.IsSecret;
     Vote          = question.Vote;
     ResolvedOn    = question.ResolvedOn;
     SumYes        = ShowVote(question) ? question.SumYes : 0;
     SumNo         = ShowVote(question) ? question.SumNo : 0;
     SumAbstention = ShowVote(question) ? question.SumAbstention : 0;
 }
Example #3
0
        public async Task <IActionResult> PutVotingQuestion(
            [FromHeader(Name = "conference_id")] int conference_id,
            [FromHeader(Name = "jwttoken")] string jwttoken,
            [FromQuery] string apikey,
            [FromRoute] int id,
            [FromBody] VotingQuestion votingQuestion)
        {
            if (this.jwtService.PermissionLevelValid(jwttoken, "superadmin") && this.auth.KeyIsValid(apikey))
            {
                if (!this.ModelState.IsValid)
                {
                    return(this.BadRequest(this.ModelState));
                }

                if (id != votingQuestion.QuestionID)
                {
                    return(this.BadRequest());
                }

                this._context.Entry(votingQuestion).State = EntityState.Modified;

                try
                {
                    await this._context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!this.VotingQuestionExists(id))
                    {
                        return(this.NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                return(this.NoContent());
            }

            return(this.Unauthorized());
        }
Example #4
0
        public async Task <IActionResult> PostVotingQuestion(
            [FromHeader(Name = "conference_id")] int conference_id,
            [FromHeader(Name = "jwttoken")] string jwttoken,
            [FromQuery] string apikey,
            [FromBody] VotingQuestion votingQuestion)
        {
            if (this.jwtService.PermissionLevelValid(jwttoken, "superadmin") && this.auth.KeyIsValid(apikey))
            {
                if (!this.ModelState.IsValid)
                {
                    return(this.BadRequest(this.ModelState));
                }

                this._context.VotingQuestion.Add(votingQuestion);
                await this._context.SaveChangesAsync();

                return(this.CreatedAtAction("GetVotingQuestion", new { id = votingQuestion.QuestionID }, votingQuestion));
            }

            return(this.Unauthorized());
        }
Example #5
0
        public async Task <IActionResult> PostVote(
            [FromHeader(Name = "conference_id")] int conference_id,
            [FromHeader(Name = "jwttoken")] string jwttoken,
            [FromQuery] string apikey,
            [FromBody] VoteObject voteObject)
        {
            if (!this.jwtService.PermissionLevelValid(jwttoken, "user") || !this.auth.KeyIsValid(apikey))
            {
                return(this.Unauthorized());
            }

            Conference_Application application = await this._context.Conference_Application.FindAsync(conference_id, this.jwtService.GetUIDfromJwtKey(jwttoken));

            if (application == null || application.Status != Conference_ApplicationController.StatusToString(CAStatus.IsAttendee))
            {
                return(this.Unauthorized()); // user is not attending the conference
            }

            VotingQuestion question = await this._context.VotingQuestion.FindAsync(voteObject.QuestionID);

            if (!this.ModelState.IsValid || question == null || !question.IsOpen || question.ResolvedOn != null)
            {
                return(this.BadRequest(this.ModelState)); // modelState not valid, question does not exist or is not open for voting
            }

            if (question.IsSecret && (application.Priority != 1 || application.IsAlumnus || application.IsBuFaKCouncil || application.IsHelper))
            {
                return(this.BadRequest("Only user with priority 1 are allowed to vote"));
            }

            int          councilID     = this.jwtService.GetCouncilfromJwtKey(jwttoken);
            VotingAnswer currentAnswer = this._context.VotingAnswer.Where(x => x.CouncilID == councilID && x.QuestionID == voteObject.QuestionID).FirstOrDefault();

            if (currentAnswer == null)
            {
                VotingAnswer votingAnswer = new VotingAnswer() // create new votingAnswer
                {
                    CouncilID  = councilID,
                    Priority   = application.Priority,
                    QuestionID = voteObject.QuestionID,
                    Vote       = question.IsSecret ? string.Empty : voteObject.Vote,
                };
                this._context.VotingAnswer.Add(votingAnswer);

                if (question.IsSecret)
                {
                    // add the vote to the secret question
                    VotingQuestionsController.AddVoteToQuestion(question, VotingQuestionsController.GetVoteType(voteObject.Vote));
                    this._context.Update(question);
                }

                await this._context.SaveChangesAsync();

                return(this.CreatedAtAction("PostVote", new { id = votingAnswer.AnswerID }, votingAnswer));
            }

            if (currentAnswer.Priority < application.Priority || question.IsSecret)
            {
                return(this.Conflict()); // there is already a vote from that council (with a higher priority or its a secret question)
            }

            currentAnswer.Vote     = voteObject.Vote; // update the current Answer to the new vote
            currentAnswer.Priority = application.Priority;
            this._context.Update(currentAnswer);
            await this._context.SaveChangesAsync();

            return(this.CreatedAtAction("PostVote", new { id = currentAnswer.AnswerID }, currentAnswer));
        }
Example #6
0
 private static bool ShowVote(VotingQuestion question)
 {
     return(question.ResolvedOn != null);
 }