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,19 +1,15 @@
using System; using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using H_PLUS_Sports.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Net;
using H_PLUS_Sports.Models;
namespace H_PLUS_Sports.Controllers namespace HPlusSportsAPI.Controllers
{ {
[Produces("application/json")] [Produces("application/json")]
[Route("api/Customers")] [Route("api/Customers")]
// [ApiController] public class CustomersController : Controller
public class CustomersController : ControllerBase
{ {
private readonly H_Plus_SportsContext _context; private readonly H_Plus_SportsContext _context;
@ -22,64 +18,98 @@ namespace H_PLUS_Sports.Controllers
_context = context; _context = context;
} }
private bool CustomerExists(int id)
{
return _context.Customer.Any(e => e.CustomerId == id);
}
[HttpGet] [HttpGet]
[Produces(typeof(DbSet<Customer>))]
public IActionResult GetCustomer() public IActionResult GetCustomer()
{ {
var results = new ObjectResult(_context.Customer) var results = new ObjectResult(_context.Customer)
{ {
StatusCode = (int)HttpStatusCode.OK StatusCode = (int)HttpStatusCode.OK
}; };
Request.HttpContext.Response.Headers.Add("X-Total-Count", _context.Customer.Count().ToString()); Request.HttpContext.Response.Headers.Add("X-Total-Count", _context.Customer.Count().ToString());
return results; return results;
} }
[HttpGet("{id}", Name = "GetCustomer")] [HttpGet("{id}")]
[Produces(typeof(Customer))]
public async Task<IActionResult> GetCustomer([FromRoute] int id) public async Task<IActionResult> GetCustomer([FromRoute] int id)
{ {
if ((CustomerExists(id))) if (!ModelState.IsValid)
{ {
var customer = await _context.Customer.SingleOrDefaultAsync(m => m.CustomerId == id); return BadRequest(ModelState);
return Ok(customer);
} }
else
var customer = await _context.Customer.SingleOrDefaultAsync(m => m.CustomerId == id);
if (customer == null)
{ {
return NotFound(); return NotFound();
} }
return Ok(customer);
} }
private bool CustomerExists(int id) [HttpPut("{id}")]
[Produces(typeof(Customer))]
public async Task<IActionResult> PutCustomer([FromRoute] int id, [FromBody] Customer customer)
{ {
return _context.Customer.Any(c => c.CustomerId == id); if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != customer.CustomerId)
{
return BadRequest();
}
_context.Entry(customer).State = EntityState.Modified;
await _context.SaveChangesAsync();
return Ok(customer);
} }
[HttpPost] [HttpPost]
[Produces(typeof(Customer))]
public async Task<IActionResult> PostCustomer([FromBody] Customer customer) public async Task<IActionResult> PostCustomer([FromBody] Customer customer)
{ {
if (!ModelState.IsValid) if (!ModelState.IsValid)
{ {
return BadRequest(ModelState); return BadRequest(ModelState);
} }
_context.Customer.Add(customer); _context.Customer.Add(customer);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return CreatedAtAction("getCustomer", new { id = customer.CustomerId }, customer);
}
[HttpPut("{id}")] return CreatedAtAction("GetCustomer", new { id = customer.CustomerId }, customer);
public async Task<IActionResult> PutCustomer([FromRoute] int id, [FromBody] Customer customer)
{
_context.Entry(customer).State = EntityState.Modified;
await _context.SaveChangesAsync();
return Ok(customer);
} }
[HttpDelete("{id}")] [HttpDelete("{id}")]
[Produces(typeof(Customer))]
public async Task<IActionResult> DeleteCustomer([FromRoute] int id) public async Task<IActionResult> DeleteCustomer([FromRoute] int id)
{ {
var customer = await _context.Customer.SingleOrDefaultAsync(m => m.CustomerId == id); if (!ModelState.IsValid)
_context.Customer.Remove(customer); {
await _context.SaveChangesAsync(); return BadRequest(ModelState);
return Ok(customer);
} }
var customer = await _context.Customer.SingleOrDefaultAsync(m => m.CustomerId == id);
if (customer == null)
{
return NotFound();
}
_context.Customer.Remove(customer);
await _context.SaveChangesAsync();
return Ok(customer);
}
} }
} }

View File

@ -1,11 +1,8 @@
using System; using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using H_PLUS_Sports.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using H_PLUS_Sports.Models;
namespace HPlusSportsAPI.Controllers namespace HPlusSportsAPI.Controllers
{ {
@ -20,42 +17,104 @@ namespace HPlusSportsAPI.Controllers
_context = context; _context = context;
} }
private bool OrderItemExists(int id)
{
return _context.OrderItem.Any(e => e.OrderItemId == id);
}
[HttpGet] [HttpGet]
[Produces(typeof(DbSet<OrderItem>))]
public IActionResult GetOrderItem() public IActionResult GetOrderItem()
{ {
return new ObjectResult(_context.OrderItem); return new ObjectResult(_context.OrderItem);
} }
[HttpGet("{id}", Name = "GetOrderItem")] [HttpGet("{id}")]
[Produces(typeof(OrderItem))]
public async Task<IActionResult> GetOrderItem([FromRoute] int id) public async Task<IActionResult> GetOrderItem([FromRoute] int id)
{ {
var orderItem = await _context.OrderItem.SingleOrDefaultAsync(m => m.OrderItemId == id); if (!ModelState.IsValid)
return Ok(orderItem); {
return BadRequest(ModelState);
} }
[HttpPost] var orderItem = await _context.OrderItem.SingleOrDefaultAsync(m => m.OrderItemId == id);
public async Task<IActionResult> PostOrderItem([FromBody] OrderItem orderItem)
if (orderItem == null)
{ {
_context.OrderItem.Add(orderItem); return NotFound();
await _context.SaveChangesAsync(); }
return CreatedAtAction("getOrderItem", new { id = orderItem.OrderItemId }, orderItem);
return Ok(orderItem);
} }
[HttpPut("{id}")] [HttpPut("{id}")]
[Produces(typeof(OrderItem))]
public async Task<IActionResult> PutOrderItem([FromRoute] int id, [FromBody] OrderItem orderItem) public async Task<IActionResult> PutOrderItem([FromRoute] int id, [FromBody] OrderItem orderItem)
{ {
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != orderItem.OrderItemId)
{
return BadRequest();
}
_context.Entry(orderItem).State = EntityState.Modified; _context.Entry(orderItem).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return Ok(orderItem); return Ok(orderItem);
} }
catch (DbUpdateConcurrencyException)
{
if (!OrderItemExists(id))
{
return NotFound();
}
else
{
throw;
}
}
}
[HttpPost]
[Produces(typeof(OrderItem))]
public async Task<IActionResult> PostOrderItem([FromBody] OrderItem orderItem)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_context.OrderItem.Add(orderItem);
await _context.SaveChangesAsync();
return CreatedAtAction("GetOrderItem", new { id = orderItem.OrderItemId }, orderItem);
}
[HttpDelete("{id}")] [HttpDelete("{id}")]
[Produces(typeof(OrderItem))]
public async Task<IActionResult> DeleteOrderItem([FromRoute] int id) public async Task<IActionResult> DeleteOrderItem([FromRoute] int id)
{ {
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var orderItem = await _context.OrderItem.SingleOrDefaultAsync(m => m.OrderItemId == id); var orderItem = await _context.OrderItem.SingleOrDefaultAsync(m => m.OrderItemId == id);
if (orderItem == null)
{
return NotFound();
}
_context.OrderItem.Remove(orderItem); _context.OrderItem.Remove(orderItem);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return Ok(orderItem); return Ok(orderItem);
} }
} }

View File

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

View File

@ -1,11 +1,9 @@
using System; using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using H_PLUS_Sports.Models;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using H_PLUS_Sports.Models;
namespace HPlusSportsAPI.Controllers namespace HPlusSportsAPI.Controllers
{ {
@ -20,41 +18,118 @@ namespace HPlusSportsAPI.Controllers
_context = context; _context = context;
} }
private bool ProductExists(string id)
{
return _context.Product.Any(e => e.ProductId == id);
}
[HttpGet] [HttpGet]
[Produces(typeof(DbSet<Product>))]
public IActionResult GetProduct() public IActionResult GetProduct()
{ {
return new ObjectResult(_context.Product); return new ObjectResult(_context.Product);
} }
[HttpGet("{id}", Name = "GetProduct")] [HttpGet("{id}")]
[Produces(typeof(Product))]
public async Task<IActionResult> GetProduct([FromRoute] string id) public async Task<IActionResult> GetProduct([FromRoute] string id)
{ {
var product = await _context.Product.SingleOrDefaultAsync(m => m.ProductId == id); if (!ModelState.IsValid)
return Ok(product); {
return BadRequest(ModelState);
} }
[HttpPost] var product = await _context.Product.SingleOrDefaultAsync(m => m.ProductId == id);
public async Task<IActionResult> PostProduct([FromBody] Product product)
if (product == null)
{ {
_context.Product.Add(product); return NotFound();
await _context.SaveChangesAsync(); }
return CreatedAtAction("getProduct", new { id = product.ProductId }, product);
return Ok(product);
} }
[HttpPut("{id}")] [HttpPut("{id}")]
[Produces(typeof(Product))]
public async Task<IActionResult> PutProduct([FromRoute] string id, [FromBody] Product product) public async Task<IActionResult> PutProduct([FromRoute] string id, [FromBody] Product product)
{ {
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != product.ProductId)
{
return BadRequest();
}
_context.Entry(product).State = EntityState.Modified; _context.Entry(product).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return Ok(product); return Ok(product);
} }
catch (DbUpdateConcurrencyException)
{
if (!ProductExists(id))
{
return NotFound();
}
else
{
throw;
}
}
}
[HttpPost]
[Produces(typeof(Product))]
public async Task<IActionResult> PostProduct([FromBody] Product product)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_context.Product.Add(product);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (ProductExists(product.ProductId))
{
return new StatusCodeResult(StatusCodes.Status409Conflict);
}
else
{
throw;
}
}
return CreatedAtAction("GetProduct", new { id = product.ProductId }, product);
}
[HttpDelete("{id}")] [HttpDelete("{id}")]
[Produces(typeof(Product))]
public async Task<IActionResult> DeleteProduct([FromRoute] string id) public async Task<IActionResult> DeleteProduct([FromRoute] string id)
{ {
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var product = await _context.Product.SingleOrDefaultAsync(m => m.ProductId == id); var product = await _context.Product.SingleOrDefaultAsync(m => m.ProductId == id);
if (product == null)
{
return NotFound();
}
_context.Product.Remove(product); _context.Product.Remove(product);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return Ok(product); return Ok(product);
} }
} }

View File

@ -1,11 +1,8 @@
using System; using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using H_PLUS_Sports.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using H_PLUS_Sports.Models;
namespace HPlusSportsAPI.Controllers namespace HPlusSportsAPI.Controllers
{ {
@ -20,42 +17,105 @@ namespace HPlusSportsAPI.Controllers
_context = context; _context = context;
} }
private bool SalespersonExists(int id)
{
return _context.Salesperson.Any(e => e.SalespersonId == id);
}
[HttpGet] [HttpGet]
[Produces(typeof(DbSet<Salesperson>))]
public IActionResult GetSalesperson() public IActionResult GetSalesperson()
{ {
return new ObjectResult(_context.Salesperson); return new ObjectResult(_context.Salesperson);
} }
[HttpGet("{id}", Name = "GetSalesPerson")] [HttpGet("{id}")]
[Produces(typeof(Salesperson))]
public async Task<IActionResult> GetSalesperson([FromRoute] int id) public async Task<IActionResult> GetSalesperson([FromRoute] int id)
{ {
var salesPerson = await _context.Salesperson.SingleOrDefaultAsync(m => m.SalespersonId== id); if (!ModelState.IsValid)
return Ok(salesPerson); {
return BadRequest(ModelState);
} }
[HttpPost] var salesperson = await _context.Salesperson.SingleOrDefaultAsync(m => m.SalespersonId == id);
public async Task<IActionResult> PostSalesperson([FromBody] Salesperson salesPerson)
if (salesperson == null)
{ {
_context.Salesperson.Add(salesPerson); return NotFound();
await _context.SaveChangesAsync(); }
return CreatedAtAction("getSalesPerson", new { id = salesPerson.SalespersonId}, salesPerson);
return Ok(salesperson);
} }
[HttpPut("{id}")] [HttpPut("{id}")]
public async Task<IActionResult> PutSalesperson([FromRoute] int id, [FromBody] Salesperson salesPerson) [Produces(typeof(Salesperson))]
public async Task<IActionResult> PutSalesperson([FromRoute] int id, [FromBody] Salesperson salesperson)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != salesperson.SalespersonId)
{
return BadRequest();
}
_context.Entry(salesperson).State = EntityState.Modified;
try
{ {
_context.Entry(salesPerson).State = EntityState.Modified;
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return Ok(salesPerson); return Ok(salesperson);
}
catch (DbUpdateConcurrencyException)
{
if (!SalespersonExists(id))
{
return NotFound();
}
else
{
throw;
}
}
}
[HttpPost]
[Produces(typeof(Salesperson))]
public async Task<IActionResult> PostSalesperson([FromBody] Salesperson salesperson)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_context.Salesperson.Add(salesperson);
await _context.SaveChangesAsync();
return CreatedAtAction("GetSalesperson", new { id = salesperson.SalespersonId }, salesperson);
} }
[HttpDelete("{id}")] [HttpDelete("{id}")]
[Produces(typeof(Salesperson))]
public async Task<IActionResult> DeleteSalesperson([FromRoute] int id) public async Task<IActionResult> DeleteSalesperson([FromRoute] int id)
{ {
var salesPerson = await _context.Salesperson.SingleOrDefaultAsync(m => m.SalespersonId == id); if (!ModelState.IsValid)
_context.Salesperson.Remove(salesPerson); {
return BadRequest(ModelState);
}
var salesperson = await _context.Salesperson.SingleOrDefaultAsync(m => m.SalespersonId == id);
if (salesperson == null)
{
return NotFound();
}
_context.Salesperson.Remove(salesperson);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return Ok(salesPerson);
return Ok(salesperson);
} }
} }
} }