// PUT api/BranchOfficeApi/5 public async Task<IHttpActionResult> PutBranchOffice(int id, BranchOffice branchoffice) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != branchoffice.BranchId) { return BadRequest(); } db.Entry(branchoffice).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BranchOfficeExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public async Task<IHttpActionResult> PostBranchOffice(BranchOffice branchoffice) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.BranchOffices.Add(branchoffice); await db.SaveChangesAsync(); return CreatedAtRoute("DefaultApi", new { id = branchoffice.BranchId }, branchoffice); }
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); }