Scaffold TeamsController in API project.
dotnet aspnet-codegenerator controller -name TeamsController -async -api -m Team -dc DeadBallZoneLeagueDbContext -outDir Controllers -dbProvider sqlite
This commit is contained in:
parent
8fc6bc96c0
commit
16ddb2d336
108
EntityFrameworkCore.API/Controllers/TeamsController.cs
Normal file
108
EntityFrameworkCore.API/Controllers/TeamsController.cs
Normal file
@ -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<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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -8,6 +8,16 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.3" />
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.3" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.3">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.3" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.3">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="9.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
32
README.md
32
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.
|
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
|
### Console project
|
||||||
|
|
||||||
[Microsoft.EntityFrameworkCore.Tools](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Tools/9.0.3)
|
[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.
|
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.
|
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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user