using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using WCS.Entitys; using WCS.Common; namespace WCS.Controllers; [ApiController] [Route("api/[controller]")] public class WcsDeviceController : ControllerBase { private readonly ApplicationDbContext _context; public WcsDeviceController(ApplicationDbContext context) { _context = context; } [HttpGet] public async Task>> Get() { return await _context.WcsDevices.ToListAsync(); } [HttpGet("{id}")] public async Task> Get(int id) { var device = await _context.WcsDevices.FindAsync(id); if (device == null) { return NotFound(); } return device; } [HttpPost] public async Task> Post(WcsDevice device) { _context.WcsDevices.Add(device); await _context.SaveChangesAsync(); return CreatedAtAction(nameof(Get), new { id = device.Id }, device); } [HttpPut("{id}")] public async Task Put(int id, WcsDevice device) { if (id != device.Id) { return BadRequest(); } _context.Entry(device).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!_context.WcsDevices.Any(e => e.Id == id)) { return NotFound(); } else { throw; } } return NoContent(); } [HttpDelete("{id}")] public async Task Delete(int id) { var device = await _context.WcsDevices.FindAsync(id); if (device == null) { return NotFound(); } _context.WcsDevices.Remove(device); await _context.SaveChangesAsync(); return NoContent(); } }