// POST api/values public IActionResult Post([FromBody] Computer Computer) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } context.Computer.Add(Computer); try { context.SaveChanges(); } catch (DbUpdateException) { if (ComputerExists(Computer.ComputerId)) { return(new StatusCodeResult(StatusCodes.Status409Conflict)); } else { throw; } } return(Ok(Computer)); }
public IActionResult PatchComputer([FromRoute] int id, [FromBody] int employeeId) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { //Remember to use FirstorDefault: Where returns IQueryable, not Computer(or other static type) var computer = context.Computer.FirstOrDefault(c => c.ComputerId == id); if (computer == null) { return(NotFound()); } computer.EmployeeId = employeeId; context.Update(computer); context.SaveChanges(); return(Ok(computer)); } catch (System.InvalidOperationException) { return(NotFound()); } }
// POST api/values public IActionResult Post([FromBody] EmployeeTraining EmployeeTraining) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } context.EmployeeTraining.Add(EmployeeTraining); try { context.SaveChanges(); } catch (DbUpdateException) { if (EmployeeTrainingExists(EmployeeTraining.EmployeeTrainingId)) { return(new StatusCodeResult(StatusCodes.Status409Conflict)); } else { throw; } } return(Ok(EmployeeTraining)); }
// POST api/values public IActionResult Post([FromBody] Department Department) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } context.Department.Add(Department); try { context.SaveChanges(); } catch (DbUpdateException) { if (DepartmentExists(Department.DepartmentId)) { return(new StatusCodeResult(StatusCodes.Status409Conflict)); } else { throw; } } return(Ok(Department)); }