|
|
|
@ -1,29 +1,24 @@
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using AutoMapper;
|
|
|
|
|
using cat_cafe.Dto;
|
|
|
|
|
using cat_cafe.Entities;
|
|
|
|
|
using cat_cafe.Repositories;
|
|
|
|
|
using cat_cafe.Dto;
|
|
|
|
|
using AutoMapper;
|
|
|
|
|
using Serilog;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Serilog;
|
|
|
|
|
|
|
|
|
|
namespace cat_cafe.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Route("api/v{version:apiVersion}/[controller]")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
[ApiVersion("2.0")]
|
|
|
|
|
[ApiVersion("1.0")]
|
|
|
|
|
public class CustomersController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly CatCafeContext _context;
|
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
private readonly ILogger<CustomersController> _logger;
|
|
|
|
|
|
|
|
|
|
public CustomersController(CatCafeContext context,IMapper mapper,ILogger<CustomersController> logger)
|
|
|
|
|
public CustomersController(CatCafeContext context, IMapper mapper, ILogger<CustomersController> logger)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
_mapper = mapper;
|
|
|
|
@ -32,7 +27,6 @@ namespace cat_cafe.Controllers
|
|
|
|
|
|
|
|
|
|
// GET: api/v1/Customers
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[MapToApiVersion("1.0")]
|
|
|
|
|
public async Task<ActionResult<IEnumerable<CustomerDto>>> GetCustomers()
|
|
|
|
|
{
|
|
|
|
|
Log.Information(this.Request.Method + " => get All customers");
|
|
|
|
@ -48,10 +42,9 @@ namespace cat_cafe.Controllers
|
|
|
|
|
|
|
|
|
|
// GET: api/v1/Customers/5
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
[MapToApiVersion("1.0")]
|
|
|
|
|
public async Task<ActionResult<CustomerDto>> GetCustomer(long id)
|
|
|
|
|
{
|
|
|
|
|
Log.Information(this.Request.Method + " => get by ID {@id}",id);
|
|
|
|
|
Log.Information(this.Request.Method + " => get by ID {@id}", id);
|
|
|
|
|
var customer = await _context.Customers.FindAsync(id);
|
|
|
|
|
|
|
|
|
|
if (customer == null)
|
|
|
|
@ -70,49 +63,34 @@ namespace cat_cafe.Controllers
|
|
|
|
|
// PUT: api/v1/Customers/5
|
|
|
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
|
|
|
[HttpPut("{id}")]
|
|
|
|
|
[MapToApiVersion("1.0")]
|
|
|
|
|
public async Task<IActionResult> PutCustomer(long id, CustomerDto customerDto)
|
|
|
|
|
{
|
|
|
|
|
Log.Information(this.Request.Method + " => put by ID {@id}", id);
|
|
|
|
|
if (id != customerDto.Id)
|
|
|
|
|
{
|
|
|
|
|
Log.Information(this.Request.Method + " => " + BadRequest().StatusCode.ToString()+" IDs not matching");
|
|
|
|
|
Log.Information(this.Request.Method + " => " + BadRequest().StatusCode.ToString() + " IDs not matching");
|
|
|
|
|
return BadRequest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Customer customer = _mapper.Map<Customer>(customerDto);
|
|
|
|
|
|
|
|
|
|
_context.Entry(customer).State = EntityState.Modified;
|
|
|
|
|
var customer = await _context.Customers
|
|
|
|
|
.SingleOrDefaultAsync(c => c.Id == id);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (DbUpdateConcurrencyException e)
|
|
|
|
|
if (customer == null)
|
|
|
|
|
{
|
|
|
|
|
if (!CustomerExists(id))
|
|
|
|
|
{
|
|
|
|
|
Log.Information(this.Request.Method + " => " + NotFound().StatusCode.ToString());
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Log.Error(this.Request.Method + " => " + e.Message);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
Log.Information(this.Request.Method + " => "
|
|
|
|
|
+ this.Response.StatusCode.ToString() + " "
|
|
|
|
|
+ customer.GetType().ToString() + " "
|
|
|
|
|
+ JsonConvert.SerializeObject(customer).ToString());
|
|
|
|
|
return Ok();
|
|
|
|
|
|
|
|
|
|
_mapper.Map(customerDto, customer);
|
|
|
|
|
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST: api/v1/Customers
|
|
|
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[MapToApiVersion("1.0")]
|
|
|
|
|
public async Task<ActionResult<Customer>> PostCustomer(CustomerDto customerDto)
|
|
|
|
|
public async Task<ActionResult<CustomerDto>> PostCustomer(CustomerDto customerDto)
|
|
|
|
|
{
|
|
|
|
|
Log.Information(this.Request.Method + " => post customer");
|
|
|
|
|
|
|
|
|
@ -125,12 +103,11 @@ namespace cat_cafe.Controllers
|
|
|
|
|
+ customer.GetType().ToString() + " "
|
|
|
|
|
+ JsonConvert.SerializeObject(customer).ToString());
|
|
|
|
|
|
|
|
|
|
return CreatedAtAction("GetCustomer", new { id = customer.Id }, _mapper.Map<Customer>( customer));
|
|
|
|
|
return CreatedAtAction("GetCustomer", new { id = customer.Id }, _mapper.Map<CustomerDto>(customer));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DELETE: api/v1/Customers/5
|
|
|
|
|
[HttpDelete("{id}")]
|
|
|
|
|
[MapToApiVersion("1.0")]
|
|
|
|
|
public async Task<IActionResult> DeleteCustomer(long id)
|
|
|
|
|
{
|
|
|
|
|
Log.Information(this.Request.Method + " => delete by ID {@id}", id);
|
|
|
|
|