|
|
|
@ -7,6 +7,8 @@ using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using cat_cafe.Entities;
|
|
|
|
|
using cat_cafe.Repositories;
|
|
|
|
|
using AutoMapper;
|
|
|
|
|
using cat_cafe.Dto;
|
|
|
|
|
|
|
|
|
|
namespace cat_cafe.Controllers
|
|
|
|
|
{
|
|
|
|
@ -15,22 +17,25 @@ namespace cat_cafe.Controllers
|
|
|
|
|
public class BarsController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly BarContext _context;
|
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
|
|
|
|
|
public BarsController(BarContext context)
|
|
|
|
|
public BarsController(BarContext context,IMapper mapper)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
_mapper = mapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: api/Bars
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<ActionResult<IEnumerable<Bar>>> GetBars()
|
|
|
|
|
public async Task<ActionResult<IEnumerable<BarDto>>> GetBars()
|
|
|
|
|
{
|
|
|
|
|
return await _context.Bars.ToListAsync();
|
|
|
|
|
var bars = await _context.Bars.ToListAsync();
|
|
|
|
|
return _mapper.Map<List<BarDto>>(bars);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: api/Bars/5
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
public async Task<ActionResult<Bar>> GetBar(long id)
|
|
|
|
|
public async Task<ActionResult<BarDto>> GetBar(long id)
|
|
|
|
|
{
|
|
|
|
|
var bar = await _context.Bars.FindAsync(id);
|
|
|
|
|
|
|
|
|
@ -39,19 +44,19 @@ namespace cat_cafe.Controllers
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bar;
|
|
|
|
|
return _mapper.Map<BarDto>(bar);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PUT: api/Bars/5
|
|
|
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
|
|
|
[HttpPut("{id}")]
|
|
|
|
|
public async Task<IActionResult> PutBar(long id, Bar bar)
|
|
|
|
|
public async Task<IActionResult> PutBar(long id, BarDto barDto)
|
|
|
|
|
{
|
|
|
|
|
if (id != bar.Id)
|
|
|
|
|
if (id != barDto.Id)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Bar bar = _mapper.Map<Bar>(barDto);
|
|
|
|
|
_context.Entry(bar).State = EntityState.Modified;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
@ -76,12 +81,13 @@ namespace cat_cafe.Controllers
|
|
|
|
|
// POST: api/Bars
|
|
|
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<ActionResult<Bar>> PostBar(Bar bar)
|
|
|
|
|
public async Task<ActionResult<BarDto>> PostBar(BarDto barDto)
|
|
|
|
|
{
|
|
|
|
|
Bar bar = _mapper.Map<Bar>(barDto);
|
|
|
|
|
_context.Bars.Add(bar);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
return CreatedAtAction("GetBar", new { id = bar.Id }, bar);
|
|
|
|
|
return CreatedAtAction("GetBar", new { id = barDto.Id }, _mapper.Map<BarDto>(bar));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DELETE: api/Bars/5
|
|
|
|
|