public IActionResult Put([FromBody] QuizViewModel model)
        {
            // return a generic HTTP Status 500 (Server Error)
            // if the client payload is invalid.
            if (model == null)
            {
                return(new StatusCodeResult(500));
            }

            // map the ViewModel to the Model
            var quiz = model.Adapt <Quiz>();

            // override those properties
            //   that should be set from the server-side only
            quiz.CreatedDate      = DateTime.Now;
            quiz.LastModifiedDate = quiz.CreatedDate;

            // Set a temporary author using the Admin user's userId
            // as user login isn't supported yet: we'll change this later on.

            quiz.UserId = User.FindFirst(ClaimTypes.NameIdentifier).Value;

            // add the new quiz
            DbContext.Quizzes.Add(quiz);
            // persist the changes into the Database.
            DbContext.SaveChanges();

            // return the newly-created Quiz to the client.
            return(new JsonResult(quiz.Adapt <QuizViewModel>(),
                                  JsonSettings));
        }
Example #2
0
        public async Task <IActionResult> Post([FromBody] QuizViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (model.Id != 0)
            {
                ModelState.AddModelError(nameof(model.Id), "Add New Quiz id should not be set");
                return(BadRequest(ModelState));
            }

            var entity = _context.Quizzes.Add(model.Adapt <Quiz>()).Entity;
            await _context.SaveChangesAsync();

            //            return CreatedAtRoute(nameof(Get), new { id = entity.Id });
            return(Ok(entity.Adapt <QuizViewModel>()));
        }
        public IActionResult Put([FromBody] QuizViewModel model)
        {
            if (model == null)
            {
                return(new StatusCodeResult(500));
            }
            var quiz = model.Adapt <Quiz>();

            quiz.CreatedDate      = DateTime.Now;
            quiz.LastModifiedDate = DateTime.Now;
            quiz.UserId           = DbContext.Users.Where(u => u.UserName == "Admin").FirstOrDefault().Id;

            DbContext.Quizzes.Add(quiz);
            DbContext.SaveChanges();

            return(new JsonResult(
                       quiz.Adapt <QuizViewModel>(),
                       JsonSettings));
        }