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>> GetTeams() { return await _context.Teams.ToListAsync(); } // GET: api/Teams/5 [HttpGet("{id}")] public async Task> 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 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> 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 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); } } }