Lagt in färdigfixade controllers
This commit is contained in:
@ -1,19 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using H_PLUS_Sports.Models;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Net;
|
||||
using H_PLUS_Sports.Models;
|
||||
|
||||
namespace H_PLUS_Sports.Controllers
|
||||
namespace HPlusSportsAPI.Controllers
|
||||
{
|
||||
[Produces("application/json")]
|
||||
[Route("api/Customers")]
|
||||
// [ApiController]
|
||||
public class CustomersController : ControllerBase
|
||||
public class CustomersController : Controller
|
||||
{
|
||||
private readonly H_Plus_SportsContext _context;
|
||||
|
||||
@ -22,64 +18,98 @@ namespace H_PLUS_Sports.Controllers
|
||||
_context = context;
|
||||
}
|
||||
|
||||
private bool CustomerExists(int id)
|
||||
{
|
||||
return _context.Customer.Any(e => e.CustomerId == id);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Produces(typeof(DbSet<Customer>))]
|
||||
public IActionResult GetCustomer()
|
||||
{
|
||||
var results = new ObjectResult(_context.Customer)
|
||||
{
|
||||
StatusCode = (int)HttpStatusCode.OK
|
||||
};
|
||||
|
||||
Request.HttpContext.Response.Headers.Add("X-Total-Count", _context.Customer.Count().ToString());
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
[HttpGet("{id}", Name = "GetCustomer")]
|
||||
[HttpGet("{id}")]
|
||||
[Produces(typeof(Customer))]
|
||||
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 Ok(customer);
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
else
|
||||
|
||||
var customer = await _context.Customer.SingleOrDefaultAsync(m => m.CustomerId == id);
|
||||
|
||||
if (customer == null)
|
||||
{
|
||||
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]
|
||||
[Produces(typeof(Customer))]
|
||||
public async Task<IActionResult> PostCustomer([FromBody] Customer customer)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
_context.Customer.Add(customer);
|
||||
await _context.SaveChangesAsync();
|
||||
return CreatedAtAction("getCustomer", new { id = customer.CustomerId }, customer);
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
public async Task<IActionResult> PutCustomer([FromRoute] int id, [FromBody] Customer customer)
|
||||
{
|
||||
_context.Entry(customer).State = EntityState.Modified;
|
||||
await _context.SaveChangesAsync();
|
||||
return Ok(customer);
|
||||
return CreatedAtAction("GetCustomer", new { id = customer.CustomerId }, customer);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[Produces(typeof(Customer))]
|
||||
public async Task<IActionResult> DeleteCustomer([FromRoute] int id)
|
||||
{
|
||||
var customer = await _context.Customer.SingleOrDefaultAsync(m => m.CustomerId == id);
|
||||
_context.Customer.Remove(customer);
|
||||
await _context.SaveChangesAsync();
|
||||
return Ok(customer);
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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,42 +17,104 @@ namespace HPlusSportsAPI.Controllers
|
||||
_context = context;
|
||||
}
|
||||
|
||||
private bool OrderItemExists(int id)
|
||||
{
|
||||
return _context.OrderItem.Any(e => e.OrderItemId == id);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Produces(typeof(DbSet<OrderItem>))]
|
||||
public IActionResult GetOrderItem()
|
||||
{
|
||||
return new ObjectResult(_context.OrderItem);
|
||||
}
|
||||
|
||||
[HttpGet("{id}", Name = "GetOrderItem")]
|
||||
[HttpGet("{id}")]
|
||||
[Produces(typeof(OrderItem))]
|
||||
public async Task<IActionResult> GetOrderItem([FromRoute] int id)
|
||||
{
|
||||
var orderItem = await _context.OrderItem.SingleOrDefaultAsync(m => m.OrderItemId == id);
|
||||
return Ok(orderItem);
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> PostOrderItem([FromBody] OrderItem orderItem)
|
||||
var orderItem = await _context.OrderItem.SingleOrDefaultAsync(m => m.OrderItemId == id);
|
||||
|
||||
if (orderItem == null)
|
||||
{
|
||||
_context.OrderItem.Add(orderItem);
|
||||
await _context.SaveChangesAsync();
|
||||
return CreatedAtAction("getOrderItem", new { id = orderItem.OrderItemId }, orderItem);
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return Ok(orderItem);
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
[Produces(typeof(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;
|
||||
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
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}")]
|
||||
|
||||
[Produces(typeof(OrderItem))]
|
||||
public async Task<IActionResult> DeleteOrderItem([FromRoute] int id)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var orderItem = await _context.OrderItem.SingleOrDefaultAsync(m => m.OrderItemId == id);
|
||||
if (orderItem == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
_context.OrderItem.Remove(orderItem);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return Ok(orderItem);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)
|
||||
var order = await _context.Order.SingleOrDefaultAsync(m => m.OrderId == id);
|
||||
|
||||
if (order == null)
|
||||
{
|
||||
_context.Add(order);
|
||||
await _context.SaveChangesAsync();
|
||||
return CreatedAtAction("getOrder", new { id = order.OrderId }, order);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,11 +1,9 @@
|
||||
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 +18,118 @@ namespace HPlusSportsAPI.Controllers
|
||||
_context = context;
|
||||
}
|
||||
|
||||
private bool ProductExists(string id)
|
||||
{
|
||||
return _context.Product.Any(e => e.ProductId == id);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Produces(typeof(DbSet<Product>))]
|
||||
public IActionResult GetProduct()
|
||||
{
|
||||
return new ObjectResult(_context.Product);
|
||||
}
|
||||
|
||||
[HttpGet("{id}", Name = "GetProduct")]
|
||||
[HttpGet("{id}")]
|
||||
[Produces(typeof(Product))]
|
||||
public async Task<IActionResult> GetProduct([FromRoute] string id)
|
||||
{
|
||||
var product = await _context.Product.SingleOrDefaultAsync(m => m.ProductId == id);
|
||||
return Ok(product);
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> PostProduct([FromBody] Product product)
|
||||
var product = await _context.Product.SingleOrDefaultAsync(m => m.ProductId == id);
|
||||
|
||||
if (product == null)
|
||||
{
|
||||
_context.Product.Add(product);
|
||||
await _context.SaveChangesAsync();
|
||||
return CreatedAtAction("getProduct", new { id = product.ProductId }, product);
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return Ok(product);
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
[Produces(typeof(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;
|
||||
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
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}")]
|
||||
[Produces(typeof(Product))]
|
||||
public async Task<IActionResult> DeleteProduct([FromRoute] string id)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var product = await _context.Product.SingleOrDefaultAsync(m => m.ProductId == id);
|
||||
if (product == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
_context.Product.Remove(product);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return Ok(product);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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,42 +17,105 @@ namespace HPlusSportsAPI.Controllers
|
||||
_context = context;
|
||||
}
|
||||
|
||||
private bool SalespersonExists(int id)
|
||||
{
|
||||
return _context.Salesperson.Any(e => e.SalespersonId == id);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Produces(typeof(DbSet<Salesperson>))]
|
||||
public IActionResult GetSalesperson()
|
||||
{
|
||||
return new ObjectResult(_context.Salesperson);
|
||||
}
|
||||
|
||||
[HttpGet("{id}", Name = "GetSalesPerson")]
|
||||
[HttpGet("{id}")]
|
||||
[Produces(typeof(Salesperson))]
|
||||
public async Task<IActionResult> GetSalesperson([FromRoute] int id)
|
||||
{
|
||||
var salesPerson = await _context.Salesperson.SingleOrDefaultAsync(m => m.SalespersonId== id);
|
||||
return Ok(salesPerson);
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> PostSalesperson([FromBody] Salesperson salesPerson)
|
||||
var salesperson = await _context.Salesperson.SingleOrDefaultAsync(m => m.SalespersonId == id);
|
||||
|
||||
if (salesperson == null)
|
||||
{
|
||||
_context.Salesperson.Add(salesPerson);
|
||||
await _context.SaveChangesAsync();
|
||||
return CreatedAtAction("getSalesPerson", new { id = salesPerson.SalespersonId}, salesPerson);
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return Ok(salesperson);
|
||||
}
|
||||
|
||||
[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();
|
||||
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}")]
|
||||
[Produces(typeof(Salesperson))]
|
||||
public async Task<IActionResult> DeleteSalesperson([FromRoute] int id)
|
||||
{
|
||||
var salesPerson = await _context.Salesperson.SingleOrDefaultAsync(m => m.SalespersonId == id);
|
||||
_context.Salesperson.Remove(salesPerson);
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
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();
|
||||
return Ok(salesPerson);
|
||||
|
||||
return Ok(salesperson);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user