feat(backend): scaffold ASP.NET Core Web API with health endpoint

This commit is contained in:
Rafał Tyński
2026-10-03 18:30:43 +02:00
parent 74f14e93ea
commit f26ad1744e
7 changed files with 169 additions and 0 deletions
+82
View File
@@ -0,0 +1,82 @@
# AGENTS.md — Standardy warstwy Controller
Dotyczy projektu `backend` (ASP.NET Core Web API, .NET 10).
## Język nazewnictwa
- Wszystkie nazwy zmiennych, parametrów, pól, metod, klas i plików są **wyłącznie w języku angielskim**.
- Zakaz używania polskich nazw w kodzie (np. `uzytkownik`, `zamowienie`), w tym w parametrach tras i modelach.
- Komentarze i dokumentacja opisowa mogą być w języku polskim, ale identyfikatory w kodzie — tylko po angielsku.
## Organizacja kontrolerów
- Każdy kontroler w osobnym pliku w katalogu `Controllers/`, nazwa pliku zgodna z nazwą klasy.
- Nazwa klasy: `<Resource>Controller` (np. `CustomersController`), dziedziczy po `ControllerBase`.
- Stosuj atrybut `[ApiController]` oraz jawne trasy `[Route("api/<resource>")]` (np. `[Route("api/health")]`) — nie używaj tokenu `[controller]`.
- Katalog `Controllers/` grupuje wyłącznie kontrolery — logika biznesowa trafia do serwisów.
## Konwencje akcji
- Metody akcji w czasie teraźniejszym, opisujące operację: `GetById`, `GetAll`, `Create`, `Update`, `Delete`.
- Używaj atrybutów HTTP: `[HttpGet]`, `[HttpGet("{id}")]`, `[HttpPost]`, `[HttpPut("{id}")]`, `[HttpDelete("{id}")]`.
- Parametry tras w `{camelCase}` (np. `{customerId}`), zgodnie z nazwami w kodzie.
- Zwracaj typy wyników `ActionResult<T>` lub `IActionResult` z jawnymi kodami statusu:
- `200 OK` — pobranie/zaktualizowanie,
- `201 Created` — utworzenie (`CreatedAtAction`),
- `204 NoContent` — usunięcie,
- `400 BadRequest`, `404 NotFound` — błędy.
- Kontroler nie zawiera logiki biznesowej ani dostępu do bazy — deleguje do serwisów/repozytoriów.
- Używaj wstrzykiwania zależności przez konstruktor (tylko `readonly` pola).
## Modele i DTO
- Wejście i wyjście kontrolera stanowią dedykowane DTO, nie modele encji.
- Nazwy DTO: `<Resource>Request`, `<Resource>Response`.
- Walidacja wejścia przez adnotacje (`[Required]`, `[StringLength]`, `[Range]`).
- Nie zwracaj encji bazy bezpośrednio z akcji.
## Asynchroniczność
- Wszystkie operacje I/O (baza, HTTP) jako `async` z sufiksem `Async` i zwrotem `Task<...>`.
- Akceptuj `CancellationToken cancellationToken` i przekazuj go dalej.
## Przykład
```csharp
[ApiController]
[Route("api/customers")]
public class CustomersController : ControllerBase
{
private readonly ICustomerService _customerService;
public CustomersController(ICustomerService customerService)
{
_customerService = customerService;
}
[HttpGet("{customerId:int}")]
public async Task<ActionResult<CustomerResponse>> GetById(
int customerId,
CancellationToken cancellationToken)
{
var customer = await _customerService.GetByIdAsync(customerId, cancellationToken);
return customer is null
? NotFound()
: Ok(customer);
}
[HttpPost]
public async Task<ActionResult<CustomerResponse>> Create(
CreateCustomerRequest request,
CancellationToken cancellationToken)
{
var createdCustomer = await _customerService.CreateAsync(request, cancellationToken);
return CreatedAtAction(
nameof(GetById),
new { customerId = createdCustomer.Id },
createdCustomer);
}
}
```
+16
View File
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Mvc;
namespace TarninaHub.Controllers;
[ApiController]
[Route("api/health")]
public class HealthController : ControllerBase
{
[HttpGet]
public ActionResult<HealthResponse> Get()
{
return Ok(new HealthResponse("ok"));
}
}
public record HealthResponse(string Status);
+27
View File
@@ -0,0 +1,27 @@
var builder = WebApplication.CreateBuilder(args);
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddControllers();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
else
{
app.UseHttpsRedirection();
}
app.UseDefaultFiles();
app.UseStaticFiles();
app.MapControllers();
app.MapFallbackToFile("index.html");
app.Run();
+14
View File
@@ -0,0 +1,14 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7080",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
+13
View File
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.12" />
</ItemGroup>
</Project>
+8
View File
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
+9
View File
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}