ExecutorsController.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 esoft_API.Models.Entities;
  12. namespace esoft_API.Controllers
  13. {
  14. public class ExecutorsController : ApiController
  15. {
  16. private esoftEntities db = new esoftEntities();
  17. // GET: api/Executors
  18. public IQueryable<Executor> GetExecutor()
  19. {
  20. return db.Executor;
  21. }
  22. // GET: api/Executors/5
  23. [ResponseType(typeof(Executor))]
  24. public IHttpActionResult GetExecutor(int id)
  25. {
  26. Executor executor = db.Executor.Find(id);
  27. if (executor == null)
  28. {
  29. return NotFound();
  30. }
  31. return Ok(executor);
  32. }
  33. // PUT: api/Executors/5
  34. [ResponseType(typeof(void))]
  35. public IHttpActionResult PutExecutor(int id, Executor executor)
  36. {
  37. if (!ModelState.IsValid)
  38. {
  39. return BadRequest(ModelState);
  40. }
  41. if (id != executor.ID)
  42. {
  43. return BadRequest();
  44. }
  45. db.Entry(executor).State = EntityState.Modified;
  46. try
  47. {
  48. db.SaveChanges();
  49. }
  50. catch (DbUpdateConcurrencyException)
  51. {
  52. if (!ExecutorExists(id))
  53. {
  54. return NotFound();
  55. }
  56. else
  57. {
  58. throw;
  59. }
  60. }
  61. return StatusCode(HttpStatusCode.NoContent);
  62. }
  63. // POST: api/Executors
  64. [ResponseType(typeof(Executor))]
  65. public IHttpActionResult PostExecutor(Executor executor)
  66. {
  67. if (!ModelState.IsValid)
  68. {
  69. return BadRequest(ModelState);
  70. }
  71. db.Executor.Add(executor);
  72. try
  73. {
  74. db.SaveChanges();
  75. }
  76. catch (DbUpdateException)
  77. {
  78. if (ExecutorExists(executor.ID))
  79. {
  80. return Conflict();
  81. }
  82. else
  83. {
  84. throw;
  85. }
  86. }
  87. return CreatedAtRoute("DefaultApi", new { id = executor.ID }, executor);
  88. }
  89. // DELETE: api/Executors/5
  90. [ResponseType(typeof(Executor))]
  91. public IHttpActionResult DeleteExecutor(int id)
  92. {
  93. Executor executor = db.Executor.Find(id);
  94. if (executor == null)
  95. {
  96. return NotFound();
  97. }
  98. db.Executor.Remove(executor);
  99. db.SaveChanges();
  100. return Ok(executor);
  101. }
  102. protected override void Dispose(bool disposing)
  103. {
  104. if (disposing)
  105. {
  106. db.Dispose();
  107. }
  108. base.Dispose(disposing);
  109. }
  110. private bool ExecutorExists(int id)
  111. {
  112. return db.Executor.Count(e => e.ID == id) > 0;
  113. }
  114. }
  115. }