Return TeamDto instead of Team model for API.

This commit is contained in:
Kevin Matsubara 2025-04-14 12:55:08 +02:00
parent f8d1ddd758
commit 30c3621deb
2 changed files with 23 additions and 2 deletions

View File

@ -23,9 +23,22 @@ namespace EntityFrameworkCore.API.Controllers
// GET: api/Teams // GET: api/Teams
[HttpGet] [HttpGet]
public async Task<ActionResult<IEnumerable<Team>>> GetTeams() public async Task<ActionResult<IEnumerable<TeamDto>>> GetTeams()
{ {
return await _context.Teams.ToListAsync(); if (_context.Teams == null)
{
return NotFound();
}
var teams = await _context.Teams
.Select(t => new TeamDto
{
Id = t.Id,
Name = t.Name,
CoachName = t.Coach.Name
})
.ToListAsync();
return teams;
} }
// GET: api/Teams/5 // GET: api/Teams/5

View File

@ -0,0 +1,8 @@
namespace EntityFrameworkCore.API;
public class TeamDto
{
public int Id { get; set; }
public string Name { get; set; }
public string CoachName { get; set; }
}