public async Task <ServiceResponse <GetPollFormDto> > ClosePollForm(int pollId, string email) { //TODO: refactor both methods var response = new ServiceResponse <GetPollFormDto>(); var pollsterResponse = await GetAccount(email); if (pollsterResponse.Data == null) { return(response.Failure(pollsterResponse)); } var account = pollsterResponse.Data; var pollForm = await _context.PollForms.FindAsync(pollId); if (pollForm == null) { return(response.Failure(NoPollFormStr, HttpStatusCode.NotFound)); } var condition = pollForm.AuthorId != account.AccountId && account.UserType != UserType.User; if (condition) { return(response.Failure(AccountMismatchStr, HttpStatusCode.Unauthorized)); } pollForm.Archived = true; await _context.SaveChangesAsync(); return(response.Success(_mapper.Map <GetPollFormDto>(pollForm), PollUpdatedStr)); }
public async Task <ServiceResponse <GetQuestionDto> > CreateQuestion(CreateQuestionDto question, int pollId) { var dalQuestion = _mapper.Map <Question>(question); dalQuestion.Poll = pollId; await _context.Questions.AddAsync(dalQuestion); await _context.SaveChangesAsync(); var getQuestionDto = _mapper.Map <GetQuestionDto>(dalQuestion); return(new ServiceResponse <GetQuestionDto>().Success(getQuestionDto, QuestionCreatedStr)); }
public async Task <ServiceResponse <UpgradeKey> > RemoveUpgradeKey(string key) { var response = new ServiceResponse <UpgradeKey>(); var dalKey = await _context.UpgradeKeys.FirstOrDefaultAsync(k => k.Key == key); if (dalKey == null) { return(response.Failure(NoKeyStr, HttpStatusCode.NotFound)); } _context.UpgradeKeys.Remove(dalKey); await _context.SaveChangesAsync(); return(response.Success(dalKey, KeyRemovedStr)); }
public async Task <ServiceResponse <GetPollStatsDto> > CreatePollStats(int pollId) { var response = new ServiceResponse <GetPollStatsDto>(); var poll = await _context.PollForms.FindAsync(pollId); if (poll == null) { return(response.Failure(NoPollWithIdString, HttpStatusCode.NotFound)); } var stats = new PollStat { PollId = pollId, Completions = 0, Percentage = 0f }; await _context.PollStats.AddAsync(stats); await _context.SaveChangesAsync(); var pollStats = _mapper.Map <GetPollStatsDto>(stats); (pollStats.Title, pollStats.Description) = (poll.Title, poll.Description); return(response.Success(pollStats, PollStatCreatedStr)); }
public async Task <ServiceResponse <List <GetAnswerDto> > > AddAnswers(List <CreateAnswerDto> answers, string email) { var response = new ServiceResponse <List <GetAnswerDto> >(); var account = await _context.Accounts.FirstOrDefaultAsync(a => a.EMail == email); if (account == null) { return(response.Failure(AccountNotFoundStr, HttpStatusCode.NotFound)); } // First we take all question IDs from answer list var questionIDs = answers.Select(a => a.QuestionId).ToList(); // Then we take questions that have previously acquired IDs and select poll IDs of these questions var pollIDs = await _context.Questions .Where(q => questionIDs.Contains(q.QuestionId)) .Select(q => q.Poll) .Distinct() .ToListAsync(); // If there are more than one poll IDs associated with questions or none - return failure if (pollIDs.Count > 1) { return(response.Failure(AnswersForManyStr, HttpStatusCode.UnprocessableEntity)); } if (pollIDs.Count == 0) { return(response.Failure(NoQuestionStr, HttpStatusCode.UnprocessableEntity)); } // Then we take all questions that are bound to this poll to check if there are all questions in answers // var pollQuestionIDs = await _context.Questions // .Where(q => q.Poll == pollIDs.ElementAtOrDefault(0)) // .Select(q => q.QuestionId) // .ToListAsync(); // // After that we have to heck if amount of questions and answers are equal // if (!answers.Select(a => a.QuestionId).Distinct().SequenceEqual(pollQuestionIDs)) // return response.Failure(InvalidQuestionsStr, HttpStatusCode.UnprocessableEntity); //It had to be changed to account for the non obligatory questions var requiredQuestions = await _context.Questions .Where(q => q.Poll == pollIDs.FirstOrDefault() && !q.AllowEmpty) .Select(q => q.QuestionId) .ToListAsync(); var requiredAnswers = answers .Where(a => requiredQuestions.Any(q => q == a.QuestionId)) .Select(a => a.QuestionId).ToList(); if (!requiredAnswers.SequenceEqual(requiredQuestions)) { return(response.Failure(InvalidQuestionsStr, HttpStatusCode.UnprocessableEntity)); } // Lastly we have to check if there are any already answered questions by the current user var dalAnswers = answers.Select(a => _mapper.Map <Answer>(a)).ToList(); foreach (Answer dalAnswer in dalAnswers) { dalAnswer.AccountId = account.AccountId; var answer = await _context.Answers.FindAsync(dalAnswer.AccountId, dalAnswer.QuestionId); if (answer != null) { return(response.Failure(AlreadyAnsweredStr, HttpStatusCode.Conflict)); } } var getAnswers = dalAnswers.Select(a => _mapper.Map <GetAnswerDto>(a)).ToList(); await _context.Answers.AddRangeAsync(dalAnswers); await _context.SaveChangesAsync(); RunFunctions(pollIDs[0], account.AccountId); return(response.Success(getAnswers, AnswersCreatedStr)); }