Files
HplusSports/H_PLUS_Sports/Controllers/OrdersController.cs

121 lines
3.0 KiB
C#

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<Order>))]
public IActionResult GetOrder()
{
return new ObjectResult(_context.Order);
}
[HttpGet("{id}")]
[Produces(typeof(Order))]
public async Task<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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);
}
}
}