diff --git a/cat_cafe/Controllers/CatsController.cs b/cat_cafe/Controllers/CatsController.cs index 2a57c4e..b6f4160 100644 --- a/cat_cafe/Controllers/CatsController.cs +++ b/cat_cafe/Controllers/CatsController.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,26 @@ namespace cat_cafe.Controllers public class CatsController : ControllerBase { private readonly CatContext _context; + private readonly IMapper _mapper; - public CatsController(CatContext context) + public CatsController(CatContext context, IMapper mapper) { + _mapper = mapper; _context = context; } // GET: api/Cats [HttpGet] - public async Task>> GetCats() + public async Task>> GetCats() { - return await _context.Cats.ToListAsync(); + var cats = await _context.Cats.ToListAsync(); + return _mapper.Map>(cats); + } // GET: api/Cats/5 [HttpGet("{id}")] - public async Task> GetCat(long id) + public async Task> GetCat(long id) { var cat = await _context.Cats.FindAsync(id); @@ -39,19 +45,20 @@ namespace cat_cafe.Controllers return NotFound(); } - return cat; + return _mapper.Map(cat); } // PUT: api/Cats/5 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 [HttpPut("{id}")] - public async Task PutCat(long id, Cat cat) + public async Task PutCat(long id, CatDto catDto) { - if (id != cat.Id) + if (id != catDto.Id) { return BadRequest(); } + Cat cat = _mapper.Map(catDto); _context.Entry(cat).State = EntityState.Modified; try @@ -76,12 +83,13 @@ namespace cat_cafe.Controllers // POST: api/Cats // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 [HttpPost] - public async Task> PostCat(Cat cat) + public async Task> PostCat(CatDto catDto) { + Cat cat = _mapper.Map(catDto); _context.Cats.Add(cat); await _context.SaveChangesAsync(); - return CreatedAtAction("GetCat", new { id = cat.Id }, cat); + return CreatedAtAction("GetCat", new { id = catDto.Id }, _mapper.Map(cat)); } // DELETE: api/Cats/5 diff --git a/cat_cafe/Dto/CatDto.cs b/cat_cafe/Dto/CatDto.cs new file mode 100644 index 0000000..86cf631 --- /dev/null +++ b/cat_cafe/Dto/CatDto.cs @@ -0,0 +1,9 @@ +using System; +namespace cat_cafe.Dto +{ + public class CatDto + { + public long Id { get; set; } + public string? Name { get; set; } + } +} diff --git a/cat_cafe/Entities/Cat.cs b/cat_cafe/Entities/Cat.cs index 0068971..25999eb 100644 --- a/cat_cafe/Entities/Cat.cs +++ b/cat_cafe/Entities/Cat.cs @@ -6,6 +6,6 @@ public string? Name { get; set; } public int Age { get; set; } = 0; - + public string Meow() { return "meow"; } } } diff --git a/cat_cafe/Mappers/CatMapper.cs b/cat_cafe/Mappers/CatMapper.cs new file mode 100644 index 0000000..4c1b74c --- /dev/null +++ b/cat_cafe/Mappers/CatMapper.cs @@ -0,0 +1,14 @@ +using AutoMapper; +using cat_cafe.Dto; +using cat_cafe.Entities; + +namespace cat_cafe.Mappers +{ + public class CatMapper : Profile + { + public CatMapper() + { + CreateMap().ReverseMap(); + } + } +} diff --git a/cat_cafe/Program.cs b/cat_cafe/Program.cs index b315b51..dae595a 100644 --- a/cat_cafe/Program.cs +++ b/cat_cafe/Program.cs @@ -14,6 +14,9 @@ opt.UseInMemoryDatabase("CatCafe")); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.AddAutoMapper(typeof(Program)); +builder.Services.AddControllersWithViews(); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/cat_cafe/Repositories/BarContext.cs b/cat_cafe/Repositories/BarContext.cs index 8b085c9..f0f4672 100644 --- a/cat_cafe/Repositories/BarContext.cs +++ b/cat_cafe/Repositories/BarContext.cs @@ -7,11 +7,11 @@ namespace cat_cafe.Repositories { public BarContext(DbContextOptions options) : base(options) - { - } + { + } public DbSet Bars { get; set; } = null!; - } + } } diff --git a/cat_cafe/Services/CatsService.cs b/cat_cafe/Services/CatsService.cs new file mode 100644 index 0000000..8516d8f --- /dev/null +++ b/cat_cafe/Services/CatsService.cs @@ -0,0 +1,6 @@ +namespace cat_cafe.Services +{ + public class CatsService + { + } +} diff --git a/cat_cafe/cat_cafe.csproj b/cat_cafe/cat_cafe.csproj index d01b369..90b4d9f 100644 --- a/cat_cafe/cat_cafe.csproj +++ b/cat_cafe/cat_cafe.csproj @@ -7,6 +7,7 @@ +