public async Task <IActionResult> Create(EmployeesInputViewModel input) { if (!this.ModelState.IsValid) { return(this.View(input)); } if (input.Gender != "Male" && input.Gender != "Female") { this.ModelState.AddModelError(string.Empty, "Sorry, gender input type is wrong!"); return(this.View(input)); } var existEmployee = await this.employeesServices .ExistEmployee(input.FirstName, input.LastName, input.Birthday); if (existEmployee == true) { this.ModelState.AddModelError(string.Empty, "Sorry, customer is already exists!"); return(this.View(input)); } await this.employeesServices.AddNewEmployee(input); return(this.RedirectToAction()); }
public async Task AddNewEmployee(EmployeesInputViewModel input) { var currentEmployee = await this.repositoryEmployee.All() .FirstOrDefaultAsync(x => x.FirstName == input.FirstName && x.LastName == input.LastName && x.Birthday == input.Birthday); if (currentEmployee != null) { // Here can return string message for that the same customer is found! return; } currentEmployee = new Employee { FirstName = input.FirstName, LastName = input.LastName, Birthday = input.Birthday, Gender = Enum.Parse <Gender>(input.Gender), }; var address = await this.repositoryAddress.All() .FirstOrDefaultAsync(x => x.Street == input.Street && x.HouseNumber == input.HouseNumber && x.ZipCode == input.ZipCode); if (address == null) { address = new Address { Street = input.Street, HouseNumber = input.HouseNumber, City = input.City, Country = input.Country, ZipCode = input.ZipCode, }; await this.repositoryAddress.AddAsync(address); await this.repositoryAddress.SaveChangesAsync(); } var department = await this.repositoryDepartment.All() .FirstOrDefaultAsync(x => x.Name == input.Department); if (department == null) { department = new Department { Name = input.Department, }; await this.repositoryDepartment.AddAsync(department); await this.repositoryDepartment.SaveChangesAsync(); } currentEmployee.Address = address; currentEmployee.Department = department; await this.repositoryEmployee.AddAsync(currentEmployee); await this.repositoryEmployee.SaveChangesAsync(); }