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

                    var matchingNurs = User.FindAll("Nursery").FirstOrDefault(claim => claim.Value == child.NurseryId.ToString());
                    if (User.IsInRole("Admin") || matchingNurs != null)
                    {
                        child.Modified   = DateTime.Now;
                        child.ModifiedBy = User.Identity.Name;

                        if (child.Address != null)
                        {
                            _repository.SaveAddress(child.Address);
                        }
                        _repository.SaveChild(child);

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

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