コード例 #1
0
        public async Task <IActionResult> PutQuestion([FromRoute] long id, [FromBody] Question question)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != question.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
コード例 #2
0
        public async Task <IActionResult> PutQuestionItem(long id, QuestionItem questionItem)
        {
            if (id != questionItem.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
コード例 #3
0
        public async Task MinusCountKvot(int id_kvot)
        {
            Kvot tmp_kvot = await db.SetKvots.FirstOrDefaultAsync(u => u.Id == id_kvot);

            tmp_kvot.CountKvot = tmp_kvot.CountKvot - 1;
            await db.SaveChangesAsync();
        }
コード例 #4
0
ファイル: PollsController.cs プロジェクト: TijmenC/AnonQ
        public async Task <IActionResult> PutPolls(int id, PollsDTO pollsDTO)
        {
            if (id != pollsDTO.Id)
            {
                return(BadRequest());
            }

            var poll = await _context.Polls.FindAsync(id);

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

            poll.QuestionId = pollsDTO.QuestionId;
            poll.Poll       = pollsDTO.Poll;
            poll.Votes      = pollsDTO.Votes;


            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException) when(!PollsExists(id))
            {
                return(NotFound());
            }

            return(NoContent());
        }
コード例 #5
0
        public async Task <IActionResult> PutComment(int id, CommentDTO CommentDTO)
        {
            if (id != CommentDTO.Id)
            {
                return(BadRequest());
            }

            var todoItem = await _context.Comments.FindAsync(id);

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

            todoItem.QuestionId = CommentDTO.QuestionId;
            todoItem.Text       = CommentDTO.Text;
            todoItem.Votes      = CommentDTO.Votes;


            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException) when(!CommentExists(id))
            {
                return(NotFound());
            }

            return(NoContent());
        }
コード例 #6
0
        public async Task <IActionResult> CreateQuestion([FromBody] Question question)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            question.PostDate = DateTime.Now;

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

            return(Ok("Question created successfully"));
        }
コード例 #7
0
        public async Task <IActionResult> PutQuestionItem(
            [FromServices] QuestionContext context,
            long id,
            [FromBody] QuestionItemDTO questionItemDTO)
        {
            if (id != questionItemDTO.Id)
            {
                return(BadRequest());
            }
            var questionItem = await context.QuestionItems.FindAsync(id);

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

            questionItem.Question        = questionItemDTO.Question;
            questionItem.Options         = questionItemDTO.Options;
            questionItem.CorrectQuestion = questionItemDTO.CorrectQuestion;
            questionItem.KnowMore        = questionItemDTO.KnowMore;

            if (ModelState.IsValid)
            {
                context.QuestionItems.Add(questionItem);
                await context.SaveChangesAsync();
            }
            else
            {
                return(BadRequest(ModelState));
            }

            try
            {
                await context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!QuestionItemExists(context, id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #8
0
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            var course = await _courseContext.Course.FindAsync(id);

            var locations = _locationContext.Location.Where(l => l.CourseId == course.CourseId);

            foreach (var location in locations)
            {
                var questionsToLocations = _questionContext.Question.Where(q => q.LocationId == location.LocationId);
                foreach (var question in questionsToLocations)
                {
                    var answersToQuestion = _answerContext.Answer.Where(ans => ans.QuestionId == question.QuestionId);
                    _answerContext.Answer.RemoveRange(answersToQuestion);

                    _questionContext.Remove(question);
                }
                _locationContext.Location.Remove(location);
            }

            _courseContext.Course.Remove(course);

            await _answerContext.SaveChangesAsync();

            await _questionContext.SaveChangesAsync();

            await _locationContext.SaveChangesAsync();

            await _courseContext.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
コード例 #9
0
        public async Task <IActionResult> Create(Question question, int id) // passing in location Id
        {
            question.LocationId = id;
            if (ModelState.IsValid)
            {
                _questionContext.Add(question);
                await _questionContext.SaveChangesAsync();

                var location = _locationContext.Location.Find(question.LocationId);
                if (location == null)
                {
                    return(NotFound());
                }

                return(RedirectToAction("Details", "Courses", new { id = location.CourseId }));
            }
            return(View(question));
        }
コード例 #10
0
        public async Task BindIDFilter(int id_p, int id_q, string path)
        {
            using (QuestionContext q_context = new QuestionContext())
            {
                Models.Question.File file = await q_context.GetFiles.FirstOrDefaultAsync(u => u.PathFile == path);

                file.ProjectID  = id_p;
                file.QuestionID = id_q;
                await q_context.SaveChangesAsync();
            }
        }
コード例 #11
0
        public async Task <ActionResult <Question> > SubmitTicket(Question question)
        {
            _questionContext.Questions.Add(question);
            await _questionContext.SaveChangesAsync();

            QuestionSubmittedEvent ticket1 = new QuestionSubmittedEvent(question.id, question.userId, question.question);

            eventHandler.newEvent(new Event(DateTime.Now, ticket1), "QuestionSubmitted");

            return(CreatedAtAction(nameof(SubmitTicket), new { userId = question.userId }, question));
        }
コード例 #12
0
ファイル: QuestionController.cs プロジェクト: TijmenC/AnonQ
        public async Task <IActionResult> UpdateQuestion(int id, QuestionDTO questionDTO)
        {
            if (id != questionDTO.Id)
            {
                return(BadRequest());
            }

            var question = await _context.Questions.FindAsync(id);

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

            question.Title           = questionDTO.Title;
            question.Description     = questionDTO.Description;
            question.Image           = questionDTO.Image;
            question.Tag             = questionDTO.Tag;
            question.CommentsEnabled = questionDTO.CommentsEnabled;
            question.DeletionTime    = questionDTO.DeletionTime;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException) when(!QuestionExists(id))
            {
                return(NotFound());
            }

            return(NoContent());
        }
コード例 #13
0
        public async Task <IActionResult> Ask(string place, string presenter, string slug, int slide,
                                              [FromBody] QuestionDto dto, CancellationToken ct)
        {
            var showIdentifier = ShowIdentifier(place, presenter, slug);
            var from           = User.FindFirstValue(DeckHubClaimTypes.Handle);

            if (string.IsNullOrEmpty(from))
            {
                return(Forbid());
            }

            var question = new Question
            {
                Uuid  = Guid.NewGuid().ToString(),
                Show  = showIdentifier,
                Slide = slide,
                Text  = dto.Text,
                From  = from,
                Time  = dto.Time
            };

            try
            {
                _context.Questions.Add(question);
                await _context.SaveChangesAsync(ct);

                _redis.PublishQuestion(question);
            }
            catch (Exception ex)
            {
                _logger.LogError(EventIds.DatabaseError, ex, ex.Message);
                throw;
            }

            return(CreatedAtAction("Get", new { uuid = question.Uuid }, QuestionDto.FromQuestion(question)));
        }
コード例 #14
0
        public async Task <ActionResult <QuestionItem> > DeleteQuestionItem(
            [FromServices] QuestionContext context,
            long id)
        {
            var questionItem = await context.QuestionItems.FindAsync(id);

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

            context.QuestionItems.Remove(questionItem);
            await context.SaveChangesAsync();

            return(questionItem);
        }
コード例 #15
0
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            var location = await _locationContext.Location.FindAsync(id);

            var questionsToLocations = _questionContext.Question.Where(q => q.LocationId == location.LocationId);

            foreach (var question in questionsToLocations)
            {
                var answersToQuestion = _answerContext.Answer.Where(ans => ans.QuestionId == question.QuestionId);
                _answerContext.Answer.RemoveRange(answersToQuestion);

                _questionContext.Remove(question);
            }
            _locationContext.Location.Remove(location);

            await _answerContext.SaveChangesAsync();

            await _questionContext.SaveChangesAsync();

            await _locationContext.SaveChangesAsync();

            return(RedirectToAction("Details", "Courses", new { id = location.CourseId }));
        }
コード例 #16
0
        public async Task <ActionResult <QuestionItemDTO> > PostQuestionItem(
            [FromServices] QuestionContext context,
            [FromBody] QuestionItemDTO questionItemDTO)
        {
            var questionItem = new QuestionItem
            {
                Id              = questionItemDTO.Id,
                Question        = questionItemDTO.Question,
                Options         = questionItemDTO.Options,
                CorrectQuestion = questionItemDTO.CorrectQuestion,
                KnowMore        = questionItemDTO.KnowMore,
            };

            if (ModelState.IsValid)
            {
                context.QuestionItems.Add(questionItem);
            }
            else
            {
                return(BadRequest(ModelState));
            }

            try
            {
                await context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return(CreatedAtAction(
                       nameof(GetQuestionItem),
                       new { id = questionItem.Id },
                       ItemToDTO(questionItem)
                       ));
        }