using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using H_Plus_Sports.Models; namespace HPlusSportsAPI.Controllers { [Produces("application/json")] [Route("api/Orders")] public class OrdersController : Controller { private readonly H_Plus_SportsContext _context; public OrdersController(H_Plus_SportsContext context) { _context = context; } private bool OrderExists(int id) { return _context.Order.Any(e => e.OrderId == id); } [HttpGet] [Produces(typeof(DbSet))] public IActionResult GetOrder() { return new ObjectResult(_context.Order); } [HttpGet("{id}")] [Produces(typeof(Order))] public async Task GetOrder([FromRoute] int id) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var order = await _context.Order.SingleOrDefaultAsync(m => m.OrderId == id); if (order == null) { return NotFound(); } return Ok(order); } [HttpPut("{id}")] [Produces(typeof(Order))] public async Task PutOrder([FromRoute] int id, [FromBody] Order order) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != order.OrderId) { return BadRequest(); } _context.Entry(order).State = EntityState.Modified; try { await _context.SaveChangesAsync(); return Ok(order); } catch (DbUpdateConcurrencyException) { if (!OrderExists(id)) { return NotFound(); } else { throw; } } } [HttpPost] [Produces(typeof(Order))] public async Task PostOrder([FromBody] Order order) { if (!ModelState.IsValid) { return BadRequest(ModelState); } _context.Order.Add(order); await _context.SaveChangesAsync(); return CreatedAtAction("GetOrder", new { id = order.OrderId }, order); } [HttpDelete("{id}")] [Produces(typeof(Order))] public async Task DeleteOrder([FromRoute] int id) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var order = await _context.Order.SingleOrDefaultAsync(m => m.OrderId == id); if (order == null) { return NotFound(); } _context.Order.Remove(order); await _context.SaveChangesAsync(); return Ok(order); } } }