generated from admin/template
15.10.26
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
<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.11"/>
|
||||
<PackageReference Include="Scalar.AspNetCore" Version="2.18.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,6 @@
|
||||
@PokedexApi_HostAddress = http://localhost:5211
|
||||
|
||||
GET {{PokedexApi_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
@@ -0,0 +1,86 @@
|
||||
using Scalar.AspNetCore;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
app.MapScalarApiReference();
|
||||
}
|
||||
|
||||
var pokemons = new List<Pokemon>
|
||||
{
|
||||
new(1, "Pikachu", "Electric", 12),
|
||||
new(2, "Charmander", "Fire", 8),
|
||||
new(3, "Squirtle", "Water", 10)
|
||||
};
|
||||
|
||||
app.MapGet("/pokemons", () => Results.Ok(pokemons));
|
||||
|
||||
app.MapGet("/pokemons/{id}", (int id) =>
|
||||
{
|
||||
var pokemon = pokemons.FirstOrDefault(p => p.Id == id);
|
||||
return pokemon is null ? Results.NotFound() : Results.Ok(pokemon);
|
||||
});
|
||||
|
||||
app.MapPost("/pokemons", (PokemonInput input) =>
|
||||
{
|
||||
var errors = Validate(input);
|
||||
if (errors.Count > 0)
|
||||
{
|
||||
return Results.BadRequest(new { errors });
|
||||
}
|
||||
|
||||
var newId = pokemons.Count > 0 ? pokemons.Max(p => p.Id) + 1 : 1;
|
||||
var pokemon = new Pokemon(newId, input.Name, input.Type, input.Level);
|
||||
pokemons.Add(pokemon);
|
||||
return Results.Created($"/pokemons/{newId}", pokemon);
|
||||
});
|
||||
|
||||
app.MapPut("/pokemons/{id}", (int id, PokemonInput input) =>
|
||||
{
|
||||
var existing = pokemons.FirstOrDefault(p => p.Id == id);
|
||||
if (existing is null)
|
||||
{
|
||||
return Results.NotFound();
|
||||
}
|
||||
|
||||
var errors = Validate(input);
|
||||
if (errors.Count > 0)
|
||||
{
|
||||
return Results.BadRequest(new { errors });
|
||||
}
|
||||
|
||||
var updated = new Pokemon(id, input.Name, input.Type, input.Level);
|
||||
pokemons[pokemons.IndexOf(existing)] = updated;
|
||||
return Results.Ok(updated);
|
||||
});
|
||||
|
||||
app.MapDelete("/pokemons/{id}", (int id) =>
|
||||
{
|
||||
var existing = pokemons.FirstOrDefault(p => p.Id == id);
|
||||
if (existing is null)
|
||||
{
|
||||
return Results.NotFound();
|
||||
}
|
||||
pokemons.Remove(existing);
|
||||
return Results.NoContent();
|
||||
});
|
||||
|
||||
app.Run();
|
||||
|
||||
List<string> Validate(PokemonInput input)
|
||||
{
|
||||
var errors = new List<string>();
|
||||
if (string.IsNullOrWhiteSpace(input.Name)) errors.Add("Имя не может быть пустым.");
|
||||
if (string.IsNullOrWhiteSpace(input.Type)) errors.Add("Тип не может быть пустым.");
|
||||
if (input.Level <= 0) errors.Add("Уровень должен быть больше 0.");
|
||||
return errors;
|
||||
}
|
||||
|
||||
public record Pokemon(int Id, string Name, string Type, int Level);
|
||||
public record PokemonInput(string Name, string Type, int Level);
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "http://localhost:5211",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "https://localhost:7050;http://localhost:5211",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user