public async Task<IHttpActionResult> PostEmployee(EmployeeViewModel emp) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var branch = GetBranch(emp.Branch);//getting branch data var empstatus = GetStatus(emp.EmployementStatus);//getting employee status var emptype = GetType(emp.EmployementType);//getting employement status of employee //creating a new employee var employee = new Employee(); employee.FirstName = emp.FirstName; employee.LastName = emp.LastName; employee.OtherName = emp.OtherName; employee.Email = emp.Email; employee.PhoneNumber = emp.PhoneNumber; employee.Passport = emp.Passport; employee.Birthdate = emp.Birthdate; employee.Gender = emp.Gender; employee.ResidentialAddress = emp.ResidentialAddress; employee.Nationality = emp.Nationality; employee.BranchId = branch.BranchId; employee.CompanyEmail = emp.CompanyEmail; employee.CompanyPhoneNumber = emp.CompanyPhoneNumber; employee.EmployementStatusId = empstatus.EmployementStatusdId; employee.EmployementTypeId = emptype.EmploymentTypeId; //adding the employee to roles //adding titles to emloyee //adding a user profile db.Employees.Add(employee); var profile = new EmployeeProfile { EmployeeId = employee.EmployeeId, Name = employee.FirstName.Substring(0,3)+employee.LastName.Substring(employee.LastName.Length-3) }; db.EmployeeProfile.Add(profile); await db.SaveChangesAsync(); emp.EmployeeId = employee.EmployeeId; //returning back back employee along with his/her branch details //avoiding circular reference exception by returning from the model BranchOffice b = new BranchOffice { BranchId = branch.BranchId, BranchName = branch.BranchName }; emp.Branch = b; return CreatedAtRoute("DefaultApi", new { id = employee.EmployeeId }, emp); }
// PUT api/Employee/5 public async Task<IHttpActionResult> PutEmployee(int id, Employee employee) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != employee.EmployeeId) { return BadRequest(); } db.Entry(employee).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }