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.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)
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)
{
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);
}
}
}