118 lines
3.2 KiB
C#
118 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using EntityFrameworkCore.Data;
|
|
using EntityFrameworkCore.Domain;
|
|
|
|
namespace EntityFrameworkCore.API.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class TeamsController : ControllerBase
|
|
{
|
|
private readonly DeadBallZoneLeagueDbContext _context;
|
|
|
|
public TeamsController(DeadBallZoneLeagueDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: api/Teams
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<TeamDto>>> GetTeams()
|
|
{
|
|
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
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<Team>> GetTeam(int id)
|
|
{
|
|
var team = await _context.Teams.FindAsync(id);
|
|
|
|
if (team == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return team;
|
|
}
|
|
|
|
// PUT: api/Teams/5
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
[HttpPut("{id}")]
|
|
public async Task<IActionResult> PutTeam(int id, Team team)
|
|
{
|
|
if (id != team.Id)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
_context.Entry(team).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!TeamExists(id))
|
|
{
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
// POST: api/Teams
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
[HttpPost]
|
|
public async Task<ActionResult<Team>> PostTeam(Team team)
|
|
{
|
|
_context.Teams.Add(team);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return CreatedAtAction("GetTeam", new { id = team.Id }, team);
|
|
}
|
|
|
|
// DELETE: api/Teams/5
|
|
[HttpDelete("{id}")]
|
|
public async Task<IActionResult> DeleteTeam(int id)
|
|
{
|
|
if (_context.Teams == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
await _context.Teams.Where(t => t.Id == id).ExecuteDeleteAsync();
|
|
return NoContent();
|
|
}
|
|
|
|
private bool TeamExists(int id)
|
|
{
|
|
return _context.Teams.Any(e => e.Id == id);
|
|
}
|
|
}
|
|
}
|