public IHttpActionResult PutEmployee(int id, EmployeeVM emp) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != emp.EmployeeID) { return BadRequest(); } Employee employee = new Employee(); employee.EmployeeID = emp.EmployeeID; employee.Name = emp.Name; employee.ImgUrl = emp.ImgUrl; employee.Email = emp.Email; employee.HiredYear = emp.HiredYear; employee.City = emp.City; employee.Country = emp.Country; db.Entry(employee).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!employeeExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public IHttpActionResult PostEmployee(Employee employee) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Employee.Add(employee); db.SaveChanges(); EmployeeVM emp = new EmployeeVM(); emp.EmployeeID = employee.EmployeeID; emp.Name = employee.Name; emp.ImgUrl = employee.ImgUrl; emp.Email = employee.Email; emp.HiredYear = employee.HiredYear; emp.City = employee.City; emp.Country = employee.Country; return CreatedAtRoute("DefaultApi", new { id = emp.EmployeeID }, emp); }