Simple Commit
continuous-integration/drone/push Build is passing Details

pull/51/head
Najlae LAMBARAA 2 years ago
parent e6d2923542
commit f36310057a

@ -7,6 +7,8 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using cat_cafe.Entities; using cat_cafe.Entities;
using cat_cafe.Repositories; using cat_cafe.Repositories;
using AutoMapper;
using cat_cafe.Dto;
namespace cat_cafe.Controllers namespace cat_cafe.Controllers
{ {
@ -15,22 +17,25 @@ namespace cat_cafe.Controllers
public class BarsController : ControllerBase public class BarsController : ControllerBase
{ {
private readonly BarContext _context; private readonly BarContext _context;
private readonly IMapper _mapper;
public BarsController(BarContext context) public BarsController(BarContext context,IMapper mapper)
{ {
_context = context; _context = context;
_mapper = mapper;
} }
// GET: api/Bars // GET: api/Bars
[HttpGet] [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 // GET: api/Bars/5
[HttpGet("{id}")] [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); var bar = await _context.Bars.FindAsync(id);
@ -39,19 +44,19 @@ namespace cat_cafe.Controllers
return NotFound(); return NotFound();
} }
return bar; return _mapper.Map<BarDto>(bar);
} }
// PUT: api/Bars/5 // PUT: api/Bars/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
[HttpPut("{id}")] [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(); return BadRequest();
} }
Bar bar = _mapper.Map<Bar>(barDto);
_context.Entry(bar).State = EntityState.Modified; _context.Entry(bar).State = EntityState.Modified;
try try
@ -76,12 +81,13 @@ namespace cat_cafe.Controllers
// POST: api/Bars // POST: api/Bars
// 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
[HttpPost] [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); _context.Bars.Add(bar);
await _context.SaveChangesAsync(); 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 // DELETE: api/Bars/5

@ -0,0 +1,13 @@
using System;
using cat_cafe.Entities;
namespace cat_cafe.Dto
{
public class BarDto
{
public long Id { get; set; }
public string? Name { get; set; }
public List<CatDto> cats { get; set; } = new List<CatDto>();
}
}

@ -0,0 +1,15 @@
using System;
using AutoMapper;
using cat_cafe.Entities;
namespace cat_cafe.Mappers
{
public class BarMapper:Profile
{
public BarMapper()
{
CreateMap<Bar, BarMapper>().ReverseMap();
}
}
}

@ -0,0 +1 @@
2023-01-21 09:37:45.148 +01:00 [INF] program start
Loading…
Cancel
Save