diff --git a/cat_cafe/Controllers/BarsController.cs b/cat_cafe/Controllers/BarsController.cs index 26b907d..6466b83 100644 --- a/cat_cafe/Controllers/BarsController.cs +++ b/cat_cafe/Controllers/BarsController.cs @@ -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>> GetBars() + public async Task>> GetBars() { - return await _context.Bars.ToListAsync(); + var bars = await _context.Bars.ToListAsync(); + return _mapper.Map>(bars); } // GET: api/Bars/5 [HttpGet("{id}")] - public async Task> GetBar(long id) + public async Task> 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(bar); } // PUT: api/Bars/5 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 [HttpPut("{id}")] - public async Task PutBar(long id, Bar bar) + public async Task PutBar(long id, BarDto barDto) { - if (id != bar.Id) + if (id != barDto.Id) { return BadRequest(); } - + Bar bar = _mapper.Map(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> PostBar(Bar bar) + public async Task> PostBar(BarDto barDto) { + Bar bar = _mapper.Map(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(bar)); } // DELETE: api/Bars/5 diff --git a/cat_cafe/Dto/BarDto.cs b/cat_cafe/Dto/BarDto.cs new file mode 100644 index 0000000..85173dd --- /dev/null +++ b/cat_cafe/Dto/BarDto.cs @@ -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 cats { get; set; } = new List(); + } +} + diff --git a/cat_cafe/Mappers/BarMapper.cs b/cat_cafe/Mappers/BarMapper.cs new file mode 100644 index 0000000..9a5e110 --- /dev/null +++ b/cat_cafe/Mappers/BarMapper.cs @@ -0,0 +1,15 @@ +using System; +using AutoMapper; +using cat_cafe.Entities; + +namespace cat_cafe.Mappers +{ + public class BarMapper:Profile + { + public BarMapper() + { + CreateMap().ReverseMap(); + } + } +} + diff --git a/cat_cafe/log.txt b/cat_cafe/log.txt new file mode 100644 index 0000000..839ce7d --- /dev/null +++ b/cat_cafe/log.txt @@ -0,0 +1 @@ +2023-01-21 09:37:45.148 +01:00 [INF] program start