using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Threading.Tasks; using H_PLUS_Sports.Models; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace H_PLUS_Sports.Controllers { [Produces("application/json")] [Route("api/Customers")] // [ApiController] public class CustomersController : ControllerBase { private readonly H_Plus_SportsContext _context; public CustomersController(H_Plus_SportsContext context) { _context = context; } [HttpGet] 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")] public async Task GetCustomer([FromRoute] int id) { if ((CustomerExists(id))) { var customer = await _context.Customer.SingleOrDefaultAsync(m => m.CustomerId == id); return Ok(customer); } else { return NotFound(); } } private bool CustomerExists(int id) { return _context.Customer.Any(c => c.CustomerId == id); } [HttpPost] public async Task 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 PutCustomer([FromRoute] int id, [FromBody] Customer customer) { _context.Entry(customer).State = EntityState.Modified; await _context.SaveChangesAsync(); return Ok(customer); } [HttpDelete("{id}")] public async Task DeleteCustomer([FromRoute] int id) { var customer = await _context.Customer.SingleOrDefaultAsync(m => m.CustomerId == id); _context.Customer.Remove(customer); await _context.SaveChangesAsync(); return Ok(customer); } } }