From bb29354597cd22b72e37fcb2e513d5b7e3a70767 Mon Sep 17 00:00:00 2001 From: Alexis DRAI Date: Tue, 10 Jan 2023 17:29:09 +0100 Subject: [PATCH 1/3] :poop: --- cat_cafe/Dto/CatDto.cs | 10 ++++++++++ cat_cafe/Entities/Cat.cs | 2 +- cat_cafe/Mappers/CatMapper.cs | 7 +++++++ cat_cafe/Services/CatsService.cs | 6 ++++++ cat_cafe/cat_cafe.csproj | 1 + 5 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 cat_cafe/Dto/CatDto.cs create mode 100644 cat_cafe/Mappers/CatMapper.cs create mode 100644 cat_cafe/Services/CatsService.cs diff --git a/cat_cafe/Dto/CatDto.cs b/cat_cafe/Dto/CatDto.cs new file mode 100644 index 0000000..78eccf2 --- /dev/null +++ b/cat_cafe/Dto/CatDto.cs @@ -0,0 +1,10 @@ +using System; +namespace cat_cafe.Dto +{ + public class CatDto + { + public long Id { get; set; } + public string? Name { get; set; } + public int Age { get; set; } = 0; + } +} 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..6662783 --- /dev/null +++ b/cat_cafe/Mappers/CatMapper.cs @@ -0,0 +1,7 @@ +namespace cat_cafe.Mappers +{ + public class CatMapper + { + + } +} 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 @@ + From d663fe8b38a4ab6c46f59252923fda0c26b62b86 Mon Sep 17 00:00:00 2001 From: Alexis DRAI Date: Sat, 14 Jan 2023 09:22:54 +0100 Subject: [PATCH 2/3] :sparkles: Implement DTO + mapper profile in controller --- cat_cafe/Controllers/CatsController.cs | 26 +++++++++++++++++--------- cat_cafe/Dto/CatDto.cs | 1 - cat_cafe/Mappers/CatMapper.cs | 13 ++++++++++--- cat_cafe/Program.cs | 3 +++ 4 files changed, 30 insertions(+), 13 deletions(-) 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 index 78eccf2..86cf631 100644 --- a/cat_cafe/Dto/CatDto.cs +++ b/cat_cafe/Dto/CatDto.cs @@ -5,6 +5,5 @@ namespace cat_cafe.Dto { public long Id { get; set; } public string? Name { get; set; } - public int Age { get; set; } = 0; } } diff --git a/cat_cafe/Mappers/CatMapper.cs b/cat_cafe/Mappers/CatMapper.cs index 6662783..4c1b74c 100644 --- a/cat_cafe/Mappers/CatMapper.cs +++ b/cat_cafe/Mappers/CatMapper.cs @@ -1,7 +1,14 @@ -namespace cat_cafe.Mappers +using AutoMapper; +using cat_cafe.Dto; +using cat_cafe.Entities; + +namespace cat_cafe.Mappers { - public class CatMapper + 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. From 66f8b507d89ce8bbc57adeed6490c9453ddcc2fd Mon Sep 17 00:00:00 2001 From: etudiant Date: Tue, 10 Jan 2023 09:06:33 +0100 Subject: [PATCH 3/3] :memo: repo & entity added --- cat_cafe/Repositories/BarContext.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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!; - } + } }