Guest User

Untitled

a guest
Jun 23rd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Entity;
  5. using System.Data.Entity.Infrastructure;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.Http;
  9. using System.Web.Http;
  10. using System.Web.Http.Description;
  11. using WebAPI.Models;
  12.  
  13. namespace WebAPI.Controllers
  14. {
  15. public class EmployeeController : ApiController
  16. {
  17. private DBModel db = new DBModel();
  18.  
  19. // GET: api/Employee
  20. public IQueryable<Employee> GetEmployees()
  21. {
  22. //return db.Employees;
  23. return db.Employees;
  24. }
  25.  
  26.  
  27. // PUT: api/Employee/5
  28. [ResponseType(typeof(void))]
  29. public IHttpActionResult PutEmployee(int id, Employee employee)
  30. {
  31.  
  32. if (id != employee.EmployeeID)
  33. {
  34. return BadRequest();
  35. }
  36.  
  37. db.Entry(employee).State = EntityState.Modified;
  38.  
  39. try
  40. {
  41. db.SaveChanges();
  42. }
  43. catch (DbUpdateConcurrencyException)
  44. {
  45. if (!EmployeeExists(id))
  46. {
  47. return NotFound();
  48. }
  49. else
  50. {
  51. throw;
  52. }
  53. }
  54.  
  55. return StatusCode(HttpStatusCode.NoContent);
  56. }
  57.  
  58. // POST: api/Employee
  59. [ResponseType(typeof(Employee))]
  60. public IHttpActionResult PostEmployee(Employee employee)
  61. {
  62. db.Employees.Add(employee);
  63. db.SaveChanges();
  64.  
  65. return CreatedAtRoute("DefaultApi", new { id = employee.EmployeeID }, employee);
  66. }
  67.  
  68. // DELETE: api/Employee/5
  69. [ResponseType(typeof(Employee))]
  70. public IHttpActionResult DeleteEmployee(int id)
  71. {
  72. Employee employee = db.Employees.Find(id);
  73. if (employee == null)
  74. {
  75. return NotFound();
  76. }
  77.  
  78. db.Employees.Remove(employee);
  79. db.SaveChanges();
  80.  
  81. return Ok(employee);
  82. }
  83.  
  84. protected override void Dispose(bool disposing)
  85. {
  86. if (disposing)
  87. {
  88. db.Dispose();
  89. }
  90. base.Dispose(disposing);
  91. }
  92.  
  93. private bool EmployeeExists(int id)
  94. {
  95. return db.Employees.Count(e => e.EmployeeID == id) > 0;
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment