From 16ddb2d3362c47f162c4aa1de5be755324ab62f3 Mon Sep 17 00:00:00 2001 From: Kevin Matsubara Date: Mon, 14 Apr 2025 11:44:22 +0200 Subject: [PATCH] Scaffold TeamsController in API project. dotnet aspnet-codegenerator controller -name TeamsController -async -api -m Team -dc DeadBallZoneLeagueDbContext -outDir Controllers -dbProvider sqlite --- .../Controllers/TeamsController.cs | 108 ++++++++++++++++++ .../EntityFrameworkCore.API.csproj | 10 ++ README.md | 32 ++++++ 3 files changed, 150 insertions(+) create mode 100644 EntityFrameworkCore.API/Controllers/TeamsController.cs diff --git a/EntityFrameworkCore.API/Controllers/TeamsController.cs b/EntityFrameworkCore.API/Controllers/TeamsController.cs new file mode 100644 index 0000000..de385c0 --- /dev/null +++ b/EntityFrameworkCore.API/Controllers/TeamsController.cs @@ -0,0 +1,108 @@ +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); + } + } +} diff --git a/EntityFrameworkCore.API/EntityFrameworkCore.API.csproj b/EntityFrameworkCore.API/EntityFrameworkCore.API.csproj index 440c906..95c73e6 100644 --- a/EntityFrameworkCore.API/EntityFrameworkCore.API.csproj +++ b/EntityFrameworkCore.API/EntityFrameworkCore.API.csproj @@ -8,6 +8,16 @@ + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + diff --git a/README.md b/README.md index 19cc246..7d19c90 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,24 @@ A list of installed NuGet packages in this application. Note that the [Microsoft.EntityFrameworkCore.Tools](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Tools/9.0.3) is used for Powershell commands used in the Package Manager Console for Visual Studio and that [Microsoft.EntityFrameworkCore.Design](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Design/9.0.3) is used for cross-platform command tools. +### API project + +[Microsoft.VisualStudio.Web.CodeGeneration.Design](https://www.nuget.org/packages/Microsoft.VisualStudio.Web.CodeGeneration.Design/9.0.0) + +* `dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design --version 9.0.0` + +[Microsoft.EntityFrameworkCore.Design](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Design/9.0.3) + +* `dotnet add package Microsoft.EntityFrameworkCore.Design --version 9.0.3` + +[Microsoft.EntityFrameworkCore.Sqlite](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Sqlite/9.0.3) + +* `dotnet add package Microsoft.EntityFrameworkCore.Sqlite --version 9.0.3` + +[Microsoft.EntityFrameworkCore.Tools](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Tools/9.0.3) *Required for aspnet-codegenerator cli tool.* + +* `dotnet add package Microsoft.EntityFrameworkCore.Tools --version 9.0.3` + ### Console project [Microsoft.EntityFrameworkCore.Tools](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Tools/9.0.3) @@ -171,3 +189,17 @@ A web application will make connection to the database on demand, which is a sco You cannot create a new context in a file, because a new connection needs to be established every time and also closed, when the database is no longer needed. Also, *no tracking* is recommended, because multiple people can manipulate data. An Inversion of Control (IoC) container will be used to handle dependancy injection for the DbContext. And also the DbContext will need a constructor with parameters to initialize itself properly. + +## Scaffolding + +### Scaffolding tool + +[ASP.NET Core code generator tool](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/tools/dotnet-aspnet-codegenerator?view=aspnetcore-9.0). + +* `dotnet tool install -g dotnet-aspnet-codegenerator` or, if already installed: `update` + +To run this tool: + +* `dotnet aspnet-codegenerator controller -name TeamsController -async -api -m Team -dc DeadBallZoneLeagueDbContext -outDir Controllers -dbProvider sqlite` + +Note that an issue may arise, related to this: https://github.com/dotnet/Scaffolding/issues/3145