Beispiel #1
0
        public JsonResult Post([FromBody] ChildViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var child = Mapper.Map <Child>(vm);

                    var matchingNurs = User.FindAll("Nursery").FirstOrDefault(claim => claim.Value == _repository.GetClassNurseryId((int)vm.ClassId).ToString());
                    if (User.IsInRole("Admin") || matchingNurs != null)
                    {
                        child.Created    = DateTime.Now;
                        child.CreatedBy  = User.Identity.Name;
                        child.Modified   = DateTime.Now;
                        child.ModifiedBy = User.Identity.Name;

                        _repository.AddChild(child);

                        if (_repository.SaveAll())
                        {
                            Response.StatusCode = (int)HttpStatusCode.Created;
                            return(Json(Mapper.Map <ChildViewModel>(child)));
                        }
                    }
                    else
                    {
                        Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                        return(Json("Unauthorized to create new child in this nursery"));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save new child", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json("Failed to save new child"));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json("Validation failed on new child"));
        }