Exemple #1
0
        public async Task <ActionResult <Question> > PostQuestion([FromForm] QuestionRequest request, IFormFile image)
        {
            var list = new List <string>();

            foreach (string option in request.option)
            {
                list.Add(option);
            }

            request.options = JsonConvert.SerializeObject(list);

            var question = new Question
            {
                title          = request.title,
                options        = request.options,
                theory_exam_id = request.theory_exam_id,
                correct_answer = request.correct_answer
            };

            _context.questions.Add(question);

            await _context.SaveChangesAsync();

            var image_name = ImagesHelper.save(image);

            _context.images.Add(new Image
            {
                name           = image_name,
                imageable_id   = question.id,
                imageable_type = "Question"
            });

            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetQuestion", new { id = question.id }, question));
        }
Exemple #2
0
        public async Task <IActionResult> PutQuestion(int id, [FromForm] QuestionRequest request, IFormFile image)
        {
            var list = new List <string>();

            foreach (string option in request.option)
            {
                list.Add(option);
            }

            request.options = JsonConvert.SerializeObject(list);

            var question = new Question
            {
                title          = request.title,
                options        = request.options,
                theory_exam_id = request.theory_exam_id,
                correct_answer = request.correct_answer,
                updated_at     = DateTime.Now,
            };

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

            if (image != null)
            {
                string image_name;

                var pImage = _context.images.Where(i => (i.imageable_id == request.id && i.imageable_type == "Question")).FirstOrDefault();

                ImagesHelper.delete(pImage.name);

                image_name = ImagesHelper.save(image);

                var local = _context.Set <Image>().Local.FirstOrDefault(i => i.id == pImage.id);

                if (local != null)
                {
                    _context.Entry(local).State = EntityState.Detached;
                }

                _context.Entry(new Image
                {
                    id             = pImage.id,
                    name           = image_name,
                    imageable_id   = request.id,
                    imageable_type = "Question"
                }).State = EntityState.Modified;
            }

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

            return(NoContent());
        }