Merge pull request ':Sparkles: Implement Cats' (#19) from implement-cats into master
continuous-integration/drone/push Build is passing Details

Reviewed-on: #19
Bar_DTO
Alexis Drai 2 years ago
commit 0c6b01198a

@ -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,26 @@ namespace cat_cafe.Controllers
public class CatsController : ControllerBase public class CatsController : ControllerBase
{ {
private readonly CatContext _context; private readonly CatContext _context;
private readonly IMapper _mapper;
public CatsController(CatContext context) public CatsController(CatContext context, IMapper mapper)
{ {
_mapper = mapper;
_context = context; _context = context;
} }
// GET: api/Cats // GET: api/Cats
[HttpGet] [HttpGet]
public async Task<ActionResult<IEnumerable<Cat>>> GetCats() public async Task<ActionResult<IEnumerable<CatDto>>> GetCats()
{ {
return await _context.Cats.ToListAsync(); var cats = await _context.Cats.ToListAsync();
return _mapper.Map<List<CatDto>>(cats);
} }
// GET: api/Cats/5 // GET: api/Cats/5
[HttpGet("{id}")] [HttpGet("{id}")]
public async Task<ActionResult<Cat>> GetCat(long id) public async Task<ActionResult<CatDto>> GetCat(long id)
{ {
var cat = await _context.Cats.FindAsync(id); var cat = await _context.Cats.FindAsync(id);
@ -39,19 +45,20 @@ namespace cat_cafe.Controllers
return NotFound(); return NotFound();
} }
return cat; return _mapper.Map<CatDto>(cat);
} }
// PUT: api/Cats/5 // PUT: api/Cats/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> PutCat(long id, Cat cat) public async Task<IActionResult> PutCat(long id, CatDto catDto)
{ {
if (id != cat.Id) if (id != catDto.Id)
{ {
return BadRequest(); return BadRequest();
} }
Cat cat = _mapper.Map<Cat>(catDto);
_context.Entry(cat).State = EntityState.Modified; _context.Entry(cat).State = EntityState.Modified;
try try
@ -76,12 +83,13 @@ namespace cat_cafe.Controllers
// POST: api/Cats // POST: api/Cats
// 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<Cat>> PostCat(Cat cat) public async Task<ActionResult<CatDto>> PostCat(CatDto catDto)
{ {
Cat cat = _mapper.Map<Cat>(catDto);
_context.Cats.Add(cat); _context.Cats.Add(cat);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return CreatedAtAction("GetCat", new { id = cat.Id }, cat); return CreatedAtAction("GetCat", new { id = catDto.Id }, _mapper.Map<CatDto>(cat));
} }
// DELETE: api/Cats/5 // DELETE: api/Cats/5

@ -0,0 +1,9 @@
using System;
namespace cat_cafe.Dto
{
public class CatDto
{
public long Id { get; set; }
public string? Name { get; set; }
}
}

@ -6,6 +6,6 @@
public string? Name { get; set; } public string? Name { get; set; }
public int Age { get; set; } = 0; public int Age { get; set; } = 0;
public string Meow() { return "meow"; }
} }
} }

@ -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<Cat, CatDto>().ReverseMap();
}
}
}

@ -14,6 +14,9 @@ opt.UseInMemoryDatabase("CatCafe"));
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddAutoMapper(typeof(Program));
builder.Services.AddControllersWithViews();
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.

@ -7,11 +7,11 @@ namespace cat_cafe.Repositories
{ {
public BarContext(DbContextOptions<BarContext> options) public BarContext(DbContextOptions<BarContext> options)
: base(options) : base(options)
{ {
} }
public DbSet<Bar> Bars { get; set; } = null!; public DbSet<Bar> Bars { get; set; } = null!;
} }
} }

@ -0,0 +1,6 @@
namespace cat_cafe.Services
{
public class CatsService
{
}
}

@ -7,6 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.1" /> <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.12" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.12" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.12" />

Loading…
Cancel
Save