Lagt in färdigfixade controllers

This commit is contained in:
2019-12-17 21:10:28 +01:00
parent 766869a6d6
commit 7df0f07d15
5 changed files with 382 additions and 98 deletions

View File

@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
using System.Threading.Tasks;
using H_PLUS_Sports.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using H_PLUS_Sports.Models;
namespace HPlusSportsAPI.Controllers
{
@ -20,41 +17,104 @@ namespace HPlusSportsAPI.Controllers
_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}", Name = "GetOrder")]
[HttpGet("{id}")]
[Produces(typeof(Order))]
public async Task<IActionResult> GetOrder([FromRoute] int id)
{
var order = await _context.Order.SingleOrDefaultAsync(m => m.OrderId == id);
return Ok(order);
}
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
[HttpPost]
public async Task<IActionResult> PostOrder([FromBody] Order order)
{
_context.Add(order);
await _context.SaveChangesAsync();
return CreatedAtAction("getOrder", new { id = order.OrderId }, order);
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 Ok(order);
return CreatedAtAction("GetOrder", new { id = order.OrderId }, order);
}
[HttpDelete("{id}")]
[Produces(typeof(Order))]
public async Task<IActionResult> DeleteOrder([FromRoute] int id)
{
var order = await _context.Order.SingleOrDefaultAsync(m => m.OrderId== 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);
}
}