public ActionResult Create(EmployeesVm model) { if (model is null) { return(RedirectToAction("Index")); } if (String.IsNullOrWhiteSpace(model.FirstName) || String.IsNullOrWhiteSpace(model.LastName) || String.IsNullOrWhiteSpace(model.Email)) { TempData["error"] = $"It is necessary to fill everything. Middlename if possible."; return(RedirectToAction("Create")); } EmailAddressAttribute emailAdressAttr = new EmailAddressAttribute(); if (!emailAdressAttr.IsValid(model.Email)) { TempData["error"] = $"Email typed wrong"; return(RedirectToAction("Create")); } EmployeesModel employee = new EmployeesModel() { Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, MiddleName = model.MiddleName, Team_Id = model.Team_Id, }; try { if (!_employeesService.Create(employee)) { TempData["error"] = $"Problems with create employee (Service error \"Create\")."; return(RedirectToAction("Create")); } } catch (Exception e) { TempData["error"] = $"Problems with saving information to database (services). {e.Message}"; return(RedirectToAction("Create")); } return(RedirectToAction("Index")); }
public IActionResult Add(DtoEmployee employee, Guid directorId, Guid departmentDirectorId) { if (ModelState.IsValid) { if (directorId != default(Guid)) { employee.Director = _service.Read(directorId); } if (departmentDirectorId != default(Guid)) { employee.DepartmentDirector = _service.Read(departmentDirectorId); } _service.Create(employee); return(new ContentResult { Content = employee.ToString(), StatusCode = 200 }); } return(BadRequest(ModelState)); }