Exemplo n.º 1
0
        public JsonResult Post([FromBody] NurseryViewModel vm)
        {
            try {
                if (ModelState.IsValid)
                {
                    var nursery = Mapper.Map <Nursery>(vm);
                    nursery.Address    = vm.Address;
                    nursery.Modified   = DateTime.Now;
                    nursery.ModifiedBy = User.Identity.Name;
                    nursery.CreatedBy  = User.Identity.Name;
                    nursery.Created    = DateTime.Now;

                    _logger.LogInformation("Attempting to save nursery");
                    _repository.AddAddress(nursery.Address);
                    _repository.AddNursery(nursery);

                    if (_repository.SaveAll())
                    {
                        Response.StatusCode = (int)HttpStatusCode.Created;
                        return(Json(Mapper.Map <NurseryViewModel>(nursery)));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save new 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 }));
        }