Esempio n. 1
0
        private async Task <bool> InsertProgQuestion(ProgQuestionDTO newQuestion)
        {
            if (newQuestion != null)
            {
                var model = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap <ProgQuestionDTO, ProgrammingQuestion>();
                    cfg.AddGlobalIgnore("CreatedBy");
                })
                            .CreateMapper()
                            .Map <ProgQuestionDTO, ProgrammingQuestion>(newQuestion);

                model.CreatedBy = await userService.GetUserByName(newQuestion.CreatedBy);

                model.CreationTime = DateTime.Now;

                for (int i = 0; i < newQuestion.Methods.Count; i++)
                {
                    var method = newQuestion.Methods[i];
                    if (method.Parameters != null)
                    {
                        object[] parameters = new object[method.Parameters.Length];
                        for (int j = 0; j < parameters.Length; j++)
                        {
                            var node = (JsonElement)method.Parameters[i];
                            if (node.ValueKind == JsonValueKind.Number)
                            {
                                model.Methods.ToArray()[i].Parameters[j] = JsonConvert.DeserializeObject(method.Parameters[i].ToString());
                            }
                            else
                            {
                                model.Methods.ToArray()[i].Parameters[j] = node.GetString();
                            }
                        }
                    }
                }

                return(await progQuestionRepository.CreateAsync(model));
            }
            else
            {
                return(false);
            }
        }
 public async Task <IActionResult> Post([FromBody] ProgQuestionDTO model)
 {
     model.CreatedBy = User.FindFirstValue(ClaimTypes.NameIdentifier);
     if (model?.Methods.Count == 0)
     {
         var normalQuestion = model as QuestionDTO;
         if (await questionService.Insert(normalQuestion))
         {
             return(Ok());
         }
         else
         {
             return(BadRequest());
         }
     }
     if (await questionService.Insert(model))
     {
         return(Ok());
     }
     else
     {
         return(BadRequest());
     }
 }