|
|
@ -9,6 +9,8 @@ using cat_cafe.Entities;
|
|
|
|
using cat_cafe.Repositories;
|
|
|
|
using cat_cafe.Repositories;
|
|
|
|
using AutoMapper;
|
|
|
|
using AutoMapper;
|
|
|
|
using cat_cafe.Dto;
|
|
|
|
using cat_cafe.Dto;
|
|
|
|
|
|
|
|
using Serilog;
|
|
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
|
|
|
|
namespace cat_cafe.Controllers
|
|
|
|
namespace cat_cafe.Controllers
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -18,20 +20,27 @@ namespace cat_cafe.Controllers
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private readonly CatContext _context;
|
|
|
|
private readonly CatContext _context;
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
|
|
|
private readonly ILogger<CatsController> _logger;
|
|
|
|
|
|
|
|
|
|
|
|
public CatsController(CatContext context, IMapper mapper)
|
|
|
|
public CatsController(CatContext context, IMapper mapper, ILogger<CatsController> logger)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_mapper = mapper;
|
|
|
|
_mapper = mapper;
|
|
|
|
_context = context;
|
|
|
|
_context = context;
|
|
|
|
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GET: api/Cats
|
|
|
|
// GET: api/Cats
|
|
|
|
[HttpGet]
|
|
|
|
[HttpGet]
|
|
|
|
public async Task<ActionResult<IEnumerable<CatDto>>> GetCats()
|
|
|
|
public async Task<ActionResult<IEnumerable<CatDto>>> GetCats()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
Log.Information(this.Request.Method + " => get All customers");
|
|
|
|
var cats = await _context.Cats.ToListAsync();
|
|
|
|
var cats = await _context.Cats.ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
return Ok(_mapper.Map<List<CatDto>>(cats));
|
|
|
|
Log.Information(this.Request.Method + " => "
|
|
|
|
|
|
|
|
+ this.Response.StatusCode.ToString() + " "
|
|
|
|
|
|
|
|
+ cats.GetType().ToString() + " length["
|
|
|
|
|
|
|
|
+ cats.Count + "]");
|
|
|
|
|
|
|
|
return Ok(_mapper.Map<List<CatDto>>(cats));
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -39,15 +48,20 @@ namespace cat_cafe.Controllers
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
public async Task<ActionResult<CatDto>> GetCat(long id)
|
|
|
|
public async Task<ActionResult<CatDto>> GetCat(long id)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
Log.Information(this.Request.Method + " => get by ID {@id}", id);
|
|
|
|
var cat = await _context.Cats.FindAsync(id);
|
|
|
|
var cat = await _context.Cats.FindAsync(id);
|
|
|
|
|
|
|
|
|
|
|
|
if (cat == null)
|
|
|
|
if (cat == null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
Log.Information(this.Request.Method + " => " + NotFound().StatusCode.ToString());
|
|
|
|
return NotFound();
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.Information(this.Request.Method + " => "
|
|
|
|
|
|
|
|
+ this.Response.StatusCode.ToString() + " "
|
|
|
|
|
|
|
|
+ cat.GetType().ToString() + " "
|
|
|
|
|
|
|
|
+ JsonConvert.SerializeObject(cat).ToString());
|
|
|
|
return Ok(_mapper.Map<CatDto>(cat));
|
|
|
|
return Ok(_mapper.Map<CatDto>(cat));
|
|
|
|
}
|
|
|
|
} //TODO finish logging
|
|
|
|
|
|
|
|
|
|
|
|
// PUT: api/Cats/5
|
|
|
|
// PUT: api/Cats/5
|
|
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
|
|