Return TeamDto instead of Team model for API.
This commit is contained in:
parent
f8d1ddd758
commit
30c3621deb
@ -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
|
||||||
|
|||||||
8
EntityFrameworkCore.API/Models/TeamDto.cs
Normal file
8
EntityFrameworkCore.API/Models/TeamDto.cs
Normal 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; }
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user