コード例 #1
0
        public async Task <IActionResult> UpdateQuestionByNumber(int number, QuestionUpdateDto question)
        {
            var questionEntity = await _repository.GetQuestionByNumberAsync(number);

            if (questionEntity == null)
            {
                return(NotFound($"Could not find any question number: {number}"));
            }

            try
            {
                var entity = questionEntity;
                entity.Content       = question.Content;
                entity.CorrectAnswer = question.CorrectAnswer;
                entity.ChapterId     = question.ChapterId;

                _repository.Update(entity);

                if (!await _unitOfWork.SaveChangeAsync())
                {
                    return(BadRequest());
                }

                return(NoContent());
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }
        }
コード例 #2
0
        public async Task <IActionResult> UpdateQuestion([FromRoute] int id, [FromQuery] QuestionUpdateDto questionUpdateDto)
        {
            if (id != questionUpdateDto.Id)
            {
                return(BadRequest());
            }
            await _questionService.UpdateAsync(_mapper.Map <Question>(questionUpdateDto));

            return(NoContent());
        }
コード例 #3
0
        public ActionResult UpdateQuestion(int id, QuestionUpdateDto questionUpdateDto)
        {
            var questionModelFromRepo = _repo.GetQuestionById(id);

            if (questionModelFromRepo == null)
            {
                return(NotFound());
            }

            _mapper.Map(questionUpdateDto, questionModelFromRepo);
            _repo.UpdateQuestion(questionModelFromRepo);
            _repo.SaveChanges();

            return(NoContent());
        }
コード例 #4
0
        public async Task UpdateQuestion(int questionId, QuestionUpdateDto updateDto)
        {
            var user = await _authenticationService.GetCurrentUserAsync();

            var question = await _unitOfWork.Repository <Question>().GetAsync(q => q.Id == questionId) ?? throw new NotFoundException("Question");

            if (question.CreatedById != user.Id)
            {
                throw new NoPermissionsException("You don't have permission for updating this question as you dont't own");
            }

            question.QuestionText = updateDto.Question;

            await _unitOfWork.Repository <Question>().UpdateAsync(question);

            await _unitOfWork.SaveChangesAsync();
        }
コード例 #5
0
        public async Task <IActionResult> Put(int id, [FromBody] QuestionUpdateDto questionUpdateDto)
        {
            var questionFromRepo = await _questionsRepository.GetQuestion(id);

            if (questionFromRepo == null)
            {
                return(BadRequest("The question doesn't exist"));
            }


            questionUpdateDto.Id = id;
            questionFromRepo     = _mapper.Map <Question>(questionUpdateDto);
            var updatedQuestion = await _questionsRepository.UpdateQuestion(questionFromRepo);

            var questionToReturn = _mapper.Map <QuestionForDetailDto>(updatedQuestion);

            return(Ok(questionToReturn));
        }
コード例 #6
0
        public ActionResult UpdateQuestion(int id, QuestionUpdateDto questionUpdateDto)
        {
            var questionModel = _repository.GetQuestionById(id);

            if (questionModel == null)
            {
                return(NotFound());
            }
            if (questionUpdateDto.QuestionType != questionModel.QuestionType)
            {
                return(BadRequest("Not possible to change the question type for existing question."));
            }
            _mapper.Map(questionUpdateDto, questionModel);
            _repository.UpdateQuestion(questionModel);

            _repository.SaveChanges();
            return(NoContent());
        }
コード例 #7
0
        public async Task <ActionResult <Question> > update(int id, QuestionUpdateDto q)
        {
            if (id != q.Id)
            {
                return(BadRequest());
            }

            Question localQ = await this._context.Questions.Where(q => q.Id == id && q.additionalType == Question.GAME).FirstOrDefaultAsync();

            if (localQ is null)
            {
                return(NotFound());
            }

            var question = new Question()
            {
                Id             = localQ.Id,
                title          = q.title is null ? localQ.title : q.title,
                startDate      = q.startDate.Equals(new DateTime()) ? localQ.startDate : q.datePublished,
                endDate        = q.endDate.Equals(new DateTime()) ? localQ.endDate : q.datePublished,
                datePublished  = q.datePublished.Equals(new DateTime()) ? localQ.datePublished : q.datePublished,
                description    = q.description is null ? localQ.description : q.description,
                correctAnswer  = q.correctAnswer <= 0 ? localQ.correctAnswer : q.correctAnswer,
                additionalType = localQ.additionalType,
                alternateName  = localQ.alternateName,
                optionSet      = localQ.optionSet,
                dateModified   = DateTime.UtcNow,
                dateCreated    = localQ.dateCreated,
                answers        = localQ.answers is null ? new List <VoteAction>() : localQ.answers
            };

            this._context.Entry(localQ).State   = EntityState.Detached;
            this._context.Entry(question).State = EntityState.Modified;

            await this._context.SaveChangesAsync();

            Question updated = await this._context.Questions
                               .Where(q => q.Id == id && q.additionalType == Question.GAME)
                               .Include("answers")
                               .FirstOrDefaultAsync();

            return(updated);
        }
コード例 #8
0
        public async Task AddNewQuestions(int propertyId, QuestionUpdateDto questionsDto)
        {
            var propertyToUpdate = await _unitOfWork.Repository <Property>().GetAsync(p => p.Id == propertyId);

            var user = await _authentication.GetCurrentUserAsync();

            if (propertyToUpdate.UserId != user.Id)
            {
                throw new FieldAccessException();
            }

            var question = _mapper.Map <Question>(questionsDto);

            question.CreatedById    = user.Id;
            question.PropertyId     = propertyId;
            question.CreatedDateUtc = DateTime.Now;
            await _unitOfWork.Repository <Question>().AddAsync(question);

            await _unitOfWork.SaveChangesAsync();
        }
コード例 #9
0
 public void UpdateQuestion(Question question, QuestionUpdateDto questionUpdateDTO)
 {
     throw new System.NotImplementedException();
 }