Beispiel #1
0
        public JsonResult Put([FromBody] NurseryViewModel vm)
        {
            try
            {
                var matchingNurs = User.FindAll("Nursery").FirstOrDefault(claim => claim.Value == vm.Id.ToString());

                if (User.IsInRole("Admin") || matchingNurs != null)
                {
                    var nursery = Mapper.Map <Nursery>(vm);
                    nursery.Address    = vm.Address;
                    nursery.Modified   = DateTime.Now;
                    nursery.ModifiedBy = User.Identity.Name;

                    _repository.SaveNursery(nursery);
                    _repository.SaveAddress(nursery.Address);

                    if (_repository.SaveAll())
                    {
                        return(Json(Mapper.Map <NurseryViewModel>(nursery)));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save nursery", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new
                {
                    Message = ex.Message
                }));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json(new { Message = "Failed", ModelState = ModelState }));
        }