WcsDeviceController.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.EntityFrameworkCore;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using WCS.Entitys;
  8. using WCS.Common;
  9. namespace WCS.Controllers;
  10. [ApiController]
  11. [Route("api/[controller]")]
  12. public class WcsDeviceController : ControllerBase
  13. {
  14. private readonly ApplicationDbContext _context;
  15. public WcsDeviceController(ApplicationDbContext context)
  16. {
  17. _context = context;
  18. }
  19. [HttpGet]
  20. public async Task<ActionResult<IEnumerable<WcsDevice>>> Get()
  21. {
  22. return await _context.WcsDevices.ToListAsync();
  23. }
  24. [HttpGet("{id}")]
  25. public async Task<ActionResult<WcsDevice>> Get(int id)
  26. {
  27. var device = await _context.WcsDevices.FindAsync(id);
  28. if (device == null)
  29. {
  30. return NotFound();
  31. }
  32. return device;
  33. }
  34. [HttpPost]
  35. public async Task<ActionResult<WcsDevice>> Post(WcsDevice device)
  36. {
  37. _context.WcsDevices.Add(device);
  38. await _context.SaveChangesAsync();
  39. return CreatedAtAction(nameof(Get), new { id = device.Id }, device);
  40. }
  41. [HttpPut("{id}")]
  42. public async Task<IActionResult> Put(int id, WcsDevice device)
  43. {
  44. if (id != device.Id)
  45. {
  46. return BadRequest();
  47. }
  48. _context.Entry(device).State = EntityState.Modified;
  49. try
  50. {
  51. await _context.SaveChangesAsync();
  52. }
  53. catch (DbUpdateConcurrencyException)
  54. {
  55. if (!_context.WcsDevices.Any(e => e.Id == id))
  56. {
  57. return NotFound();
  58. }
  59. else
  60. {
  61. throw;
  62. }
  63. }
  64. return NoContent();
  65. }
  66. [HttpDelete("{id}")]
  67. public async Task<IActionResult> Delete(int id)
  68. {
  69. var device = await _context.WcsDevices.FindAsync(id);
  70. if (device == null)
  71. {
  72. return NotFound();
  73. }
  74. _context.WcsDevices.Remove(device);
  75. await _context.SaveChangesAsync();
  76. return NoContent();
  77. }
  78. }