public async Task <ActionResult <TestSessionDTO> > PostTestSession(TestSessionDTO TestSessionDTO)
        {
            var TestSession = CreateNewSession(TestSessionDTO);
            await _context.SaveChangesAsync();

            //return CreatedAtAction("GetTestSession", new { id = TestSession.Id }, TestSession);
            return(CreatedAtAction(nameof(GetTestSession), new { id = TestSession.Id }, TestSessionToDTO(TestSession)));
        }
Beispiel #2
0
        public async Task <ActionResult <QuestionDTO> > PostQuestion(QuestionDTO questionDTO)
        {
            var question = CreateFromDTO(questionDTO);

            _context.Question.Add(question);
            await _context.SaveChangesAsync();

            //return CreatedAtAction("GetQuestion", new { id = question.Id }, question);
            return(CreatedAtAction(nameof(GetQuestion), new { id = question.Id }, QuestionToDTO(question)));
        }
        public async Task <IActionResult> PutUser(long id, UserDTO UserDTO)
        {
            if (id != UserDTO.Id)
            {
                return(BadRequest());
            }

            var User = await _context.User.FindAsync(id);

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

            User = UpdatePutableFields(User, UserDTO);

            _context.Entry(User).State = EntityState.Modified;

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

            return(NoContent());
        }
        public async Task <IActionResult> PutTopic(long id, TopicDTO TopicDTO)
        {
            if (id != TopicDTO.Id)
            {
                return(BadRequest());
            }

            var Topic = await _context.Topic.FindAsync(id);

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

            Topic = UpdatePutableFields(Topic, TopicDTO);

            _context.Entry(Topic).State = EntityState.Modified;

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

            return(NoContent());
        }
        public async Task <IActionResult> PutResultType(long id, ResultTypeDTO ResultTypeDTO)
        {
            if (id != ResultTypeDTO.Id)
            {
                return(BadRequest());
            }

            var ResultType = await _context.ResultType.FindAsync(id);

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

            ResultType = UpdatePutableFields(ResultType, ResultTypeDTO);

            _context.Entry(ResultType).State = EntityState.Modified;

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

            return(NoContent());
        }
        public async Task <IActionResult> PutSessionQuestion(long id, string action, long count = 0)
        {
            var SessionQuestion = await _context.SessionQuestion.FindAsync(id);

            if (SessionQuestion == null)
            {
                return(NotFound());
            }
            if (action.Equals("requeue") && count > 0 && count <= 3)
            {
                SessionQuestion.ResultTypeId          = 2;
                _context.Entry(SessionQuestion).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                // Cap it at the required number of attempts; if you miss a question twice in a row, should still only be 3
                var existingCount = fetchExistingQuestionCount(SessionQuestion);
                for (int i = 0; i < count - existingCount; i++)
                {
                    var NewSessionQuestion = new SessionQuestion
                    {
                        QuestionId    = SessionQuestion.QuestionId,
                        ResultTypeId  = 1,
                        TestSessionId = SessionQuestion.TestSessionId
                    };
                    _context.SessionQuestion.Add(NewSessionQuestion);
                    await _context.SaveChangesAsync();
                }
            }
            else if (action.Equals("answer"))
            {
                SessionQuestion.ResultTypeId          = 3;
                _context.Entry(SessionQuestion).State = EntityState.Modified;
                await _context.SaveChangesAsync();
            }
            else
            {
                return(BadRequest("Unable to determine a proper action to take"));
            }

            return(NoContent());
        }