Beispiel #1
0
        public JsonResult Put(int nurseryId, [FromBody] EmployeeViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var employee = Mapper.Map <Employee>(vm);

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

                        if (employee.Address != null)
                        {
                            _repository.SaveAddress(employee.Address);
                        }
                        _repository.SaveEmployee(employee);

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

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