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

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

                        _repository.AddEmployee(employee);

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

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