Добавил работы

This commit is contained in:
2026-11-26 11:07:33 +03:00
parent aa5382dfc3
commit 8a589cbbc1
152 changed files with 2945 additions and 460 deletions
@@ -0,0 +1,16 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PokemonAPI", "PokemonAPI\PokemonAPI.csproj", "{AB656F88-0D39-4163-8984-28C83362611C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AB656F88-0D39-4163-8984-28C83362611C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AB656F88-0D39-4163-8984-28C83362611C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB656F88-0D39-4163-8984-28C83362611C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB656F88-0D39-4163-8984-28C83362611C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
@@ -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.11"/>
</ItemGroup>
</Project>
@@ -0,0 +1,6 @@
@PokemonAPI_HostAddress = http://localhost:5076
GET {{PokemonAPI_HostAddress}}/weatherforecast/
Accept: application/json
###
@@ -0,0 +1,138 @@
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();
var app = builder.Build();
var pokemons = new List<Pokemon>
{
new(1, "Pikachu", "Electric", 12),
new(2, "Charmander", "Fire", 8),
new(3, "Squirtle", "Water", 10)
};
app.MapPost("/pokemons", (PokemonInput input) =>
{
var errors = Validate(input);
if (errors.Count > 0)
{
return Results.BadRequest(errors);
}
var newId = pokemons.Max(p => p.Id) + 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 == 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 == null)
{
return Results.NotFound();
}
pokemons.Remove(existing);
return Results.NoContent();
});
app.MapGet("/pokemons/{id}", (int id) =>
{
var pokemon = pokemons.FirstOrDefault(p => p.Id == id);
if (pokemon != null)
{
return Results.Ok(pokemon);
}
else
{
return Results.NotFound();
}
});
app.MapGet("/pokemons/type/{type}", (string type) =>
{
var filteredPokemons = pokemons
.Where(p => p.Type.Equals(type, StringComparison.OrdinalIgnoreCase))
.ToList();
if (filteredPokemons.Any())
{
return Results.Ok(filteredPokemons);
}
else
{
return Results.NotFound(new { message = $"Покемоны с типом '{type}' не найдены." });
}
});
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.MapGet("/hello", () => "Привет, мир API!");
app.MapGet("/pokemons", () => Results.Ok(pokemons));
app.Run();
static List<string> Validate(PokemonInput input)
{
var allowedTypes = new[] { "Fire", "Water", "Grass", "Electric",
"Psychic", "Normal", "Flying", "Poison" };
var errors = new List<string>();
if (string.IsNullOrEmpty(input.Name) || input.Name.Length < 3)
{
errors.Add("Name должен быть не короче 2 символов");
}
if (input.Type == null || !allowedTypes.Contains(input.Type))
{
errors.Add($"Type '{input.Type}' не поддерживается");
}
if (input.Level < 1 || input.Level > 100)
{
errors.Add("Level должен быть в диапазоне 1..100");
}
return errors;
}
public record Pokemon(
int Id,
string Name,
string Type,
int Level
);
public record PokemonInput(
string Name,
string Type,
int Level,
int Count
);
@@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5076",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7190;http://localhost:5076",
"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": "*"
}