using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System.Net; using H_Plus_Sports.Models; using H_Plus_Sports.Contracts; namespace HPlusSportsAPI.Controllers { [Produces("application/json")] [Route("api/Customers")] public class CustomersController : Controller { private readonly ICustomerRepository _customerRepository; public CustomersController( ICustomerRepository customerRepository) { _customerRepository = customerRepository; } private async Task CustomerExists(int id) { return await _customerRepository.Exist(id); } [HttpGet] [Produces(typeof(DbSet))] [ResponseCache(Duration = 60)] public IActionResult GetCustomer() { var results = new ObjectResult(_customerRepository.GetAll()) { StatusCode = (int)HttpStatusCode.OK }; Request.HttpContext.Response.Headers.Add("X-Total-Count", _customerRepository.GetAll().Count().ToString()); return results; } [HttpGet("{id}")] [Produces(typeof(Customer))] [ResponseCache(Duration = 60)] public async Task GetCustomer([FromRoute] int id) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var customer = await _customerRepository.Find(id); if (customer == null) { return NotFound(); } return Ok(customer); } [HttpPut("{id}")] [Produces(typeof(Customer))] public async Task PutCustomer([FromRoute] int id, [FromBody] Customer customer) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != customer.CustomerId) { return BadRequest(); } try { await _customerRepository.Update(customer); return Ok(customer); } catch (DbUpdateConcurrencyException) { if (!await CustomerExists(id)) { return NotFound(); } else { throw; } } } [HttpPost] [Produces(typeof(Customer))] public async Task PostCustomer([FromBody] Customer customer) { if (!ModelState.IsValid) { return BadRequest(ModelState); } await _customerRepository.Add(customer); return CreatedAtAction("GetCustomer", new { id = customer.CustomerId }, customer); } [HttpDelete("{id}")] [Produces(typeof(Customer))] public async Task DeleteCustomer([FromRoute] int id) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (! await CustomerExists(id)) { return NotFound(); } await _customerRepository.Remove(id); return Ok(); } } }