Kevin Matsubara 16ddb2d336 Scaffold TeamsController in API project.
dotnet aspnet-codegenerator controller -name TeamsController -async -api -m Team -dc DeadBallZoneLeagueDbContext -outDir Controllers -dbProvider sqlite
2025-04-14 11:44:22 +02:00

109 lines
2.9 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<Team>>> GetTeams()
{
return await _context.Teams.ToListAsync();
}
// 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)
{
var team = await _context.Teams.FindAsync(id);
if (team == null)
{
return NotFound();
}
_context.Teams.Remove(team);
await _context.SaveChangesAsync();
return NoContent();
}
private bool TeamExists(int id)
{
return _context.Teams.Any(e => e.Id == id);
}
}
}