From 50a284e2d6a3dbf09150136ce9f64b8172350e59 Mon Sep 17 00:00:00 2001 From: zxc Date: Fri, 16 Oct 2026 15:03:36 +0300 Subject: [PATCH] part2 --- .../PokedexApi/delete -pokemons--id.yml | 39 +++++++ .../PokedexApi - v1/PokedexApi/folder.yml | 6 + .../PokedexApi - v1/PokedexApi/get -hello.yml | 34 ++++++ .../PokedexApi/get -pokemons--id.yml | 39 +++++++ .../PokedexApi/get -pokemons.yml | 31 +++++ .../PokedexApi/post -pokemons.yml | 47 ++++++++ .../PokedexApi/put -pokemons--id.yml | 55 +++++++++ .../environments/Environment 1.yml | 4 + .../lab-02/PokedexApi - v1/opencollection.yml | 10 ++ works/lab-02/part2/PokedexApi/PokedexApi.sln | 16 +++ .../PokedexApi/PokedexApi/PokedexApi.csproj | 14 +++ .../PokedexApi/PokedexApi/PokedexApi.http | 6 + .../part2/PokedexApi/PokedexApi/Program.cs | 110 ++++++++++++++++++ .../PokedexApi/Properties/launchSettings.json | 23 ++++ .../PokedexApi/appsettings.Development.json | 8 ++ .../PokedexApi/PokedexApi/appsettings.json | 9 ++ 16 files changed, 451 insertions(+) create mode 100644 works/lab-02/PokedexApi - v1/PokedexApi/delete -pokemons--id.yml create mode 100644 works/lab-02/PokedexApi - v1/PokedexApi/folder.yml create mode 100644 works/lab-02/PokedexApi - v1/PokedexApi/get -hello.yml create mode 100644 works/lab-02/PokedexApi - v1/PokedexApi/get -pokemons--id.yml create mode 100644 works/lab-02/PokedexApi - v1/PokedexApi/get -pokemons.yml create mode 100644 works/lab-02/PokedexApi - v1/PokedexApi/post -pokemons.yml create mode 100644 works/lab-02/PokedexApi - v1/PokedexApi/put -pokemons--id.yml create mode 100644 works/lab-02/PokedexApi - v1/environments/Environment 1.yml create mode 100644 works/lab-02/PokedexApi - v1/opencollection.yml create mode 100644 works/lab-02/part2/PokedexApi/PokedexApi.sln create mode 100644 works/lab-02/part2/PokedexApi/PokedexApi/PokedexApi.csproj create mode 100644 works/lab-02/part2/PokedexApi/PokedexApi/PokedexApi.http create mode 100644 works/lab-02/part2/PokedexApi/PokedexApi/Program.cs create mode 100644 works/lab-02/part2/PokedexApi/PokedexApi/Properties/launchSettings.json create mode 100644 works/lab-02/part2/PokedexApi/PokedexApi/appsettings.Development.json create mode 100644 works/lab-02/part2/PokedexApi/PokedexApi/appsettings.json diff --git a/works/lab-02/PokedexApi - v1/PokedexApi/delete -pokemons--id.yml b/works/lab-02/PokedexApi - v1/PokedexApi/delete -pokemons--id.yml new file mode 100644 index 0000000..bfea627 --- /dev/null +++ b/works/lab-02/PokedexApi - v1/PokedexApi/delete -pokemons--id.yml @@ -0,0 +1,39 @@ +info: + name: delete /pokemons/:id + type: http + seq: 6 + tags: + - PokedexApi + +http: + method: DELETE + url: "{{baseUrl}}/pokemons/:id" + params: + - name: id + value: "" + type: path + auth: inherit + +settings: + encodeUrl: true + timeout: 0 + followRedirects: true + maxRedirects: 5 + forwardAuthorizationHeader: false + +examples: + - name: 200 Response + description: OK + request: + url: "{{baseUrl}}/pokemons/:id" + method: DELETE + params: + - name: id + value: "" + type: path + response: + status: 200 + statusText: OK + body: + type: text + data: "" diff --git a/works/lab-02/PokedexApi - v1/PokedexApi/folder.yml b/works/lab-02/PokedexApi - v1/PokedexApi/folder.yml new file mode 100644 index 0000000..f040f0b --- /dev/null +++ b/works/lab-02/PokedexApi - v1/PokedexApi/folder.yml @@ -0,0 +1,6 @@ +info: + name: PokedexApi + type: folder + +request: + auth: inherit diff --git a/works/lab-02/PokedexApi - v1/PokedexApi/get -hello.yml b/works/lab-02/PokedexApi - v1/PokedexApi/get -hello.yml new file mode 100644 index 0000000..4f14b68 --- /dev/null +++ b/works/lab-02/PokedexApi - v1/PokedexApi/get -hello.yml @@ -0,0 +1,34 @@ +info: + name: get /hello + type: http + seq: 1 + tags: + - PokedexApi + +http: + method: GET + url: "{{baseUrl}}/hello" + auth: inherit + +settings: + encodeUrl: true + timeout: 0 + followRedirects: true + maxRedirects: 5 + forwardAuthorizationHeader: false + +examples: + - name: 200 Response + description: OK + request: + url: "{{baseUrl}}/hello" + method: GET + response: + status: 200 + statusText: OK + headers: + - name: Content-Type + value: text/plain + body: + type: text + data: "" diff --git a/works/lab-02/PokedexApi - v1/PokedexApi/get -pokemons--id.yml b/works/lab-02/PokedexApi - v1/PokedexApi/get -pokemons--id.yml new file mode 100644 index 0000000..d1396fc --- /dev/null +++ b/works/lab-02/PokedexApi - v1/PokedexApi/get -pokemons--id.yml @@ -0,0 +1,39 @@ +info: + name: get /pokemons/:id + type: http + seq: 4 + tags: + - PokedexApi + +http: + method: GET + url: "{{baseUrl}}/pokemons/:id" + params: + - name: id + value: "" + type: path + auth: inherit + +settings: + encodeUrl: true + timeout: 0 + followRedirects: true + maxRedirects: 5 + forwardAuthorizationHeader: false + +examples: + - name: 200 Response + description: OK + request: + url: "{{baseUrl}}/pokemons/:id" + method: GET + params: + - name: id + value: "" + type: path + response: + status: 200 + statusText: OK + body: + type: text + data: "" diff --git a/works/lab-02/PokedexApi - v1/PokedexApi/get -pokemons.yml b/works/lab-02/PokedexApi - v1/PokedexApi/get -pokemons.yml new file mode 100644 index 0000000..81a34eb --- /dev/null +++ b/works/lab-02/PokedexApi - v1/PokedexApi/get -pokemons.yml @@ -0,0 +1,31 @@ +info: + name: get /pokemons + type: http + seq: 2 + tags: + - PokedexApi + +http: + method: GET + url: "{{baseUrl}}/pokemons" + auth: inherit + +settings: + encodeUrl: true + timeout: 0 + followRedirects: true + maxRedirects: 5 + forwardAuthorizationHeader: false + +examples: + - name: 200 Response + description: OK + request: + url: "{{baseUrl}}/pokemons" + method: GET + response: + status: 200 + statusText: OK + body: + type: text + data: "" diff --git a/works/lab-02/PokedexApi - v1/PokedexApi/post -pokemons.yml b/works/lab-02/PokedexApi - v1/PokedexApi/post -pokemons.yml new file mode 100644 index 0000000..cf15f5b --- /dev/null +++ b/works/lab-02/PokedexApi - v1/PokedexApi/post -pokemons.yml @@ -0,0 +1,47 @@ +info: + name: post /pokemons + type: http + seq: 3 + tags: + - PokedexApi + +http: + method: POST + url: "{{baseUrl}}/pokemons" + body: + type: json + data: |- + { + "name": "", + "type": "", + "level": "" + } + auth: inherit + +settings: + encodeUrl: true + timeout: 0 + followRedirects: true + maxRedirects: 5 + forwardAuthorizationHeader: false + +examples: + - name: 200 Response + description: OK + request: + url: "{{baseUrl}}/pokemons" + method: POST + body: + type: json + data: |- + { + "name": "", + "type": "", + "level": "" + } + response: + status: 200 + statusText: OK + body: + type: text + data: "" diff --git a/works/lab-02/PokedexApi - v1/PokedexApi/put -pokemons--id.yml b/works/lab-02/PokedexApi - v1/PokedexApi/put -pokemons--id.yml new file mode 100644 index 0000000..27bfe8f --- /dev/null +++ b/works/lab-02/PokedexApi - v1/PokedexApi/put -pokemons--id.yml @@ -0,0 +1,55 @@ +info: + name: put /pokemons/:id + type: http + seq: 5 + tags: + - PokedexApi + +http: + method: PUT + url: "{{baseUrl}}/pokemons/:id" + params: + - name: id + value: "" + type: path + body: + type: json + data: |- + { + "name": "", + "type": "", + "level": "" + } + auth: inherit + +settings: + encodeUrl: true + timeout: 0 + followRedirects: true + maxRedirects: 5 + forwardAuthorizationHeader: false + +examples: + - name: 200 Response + description: OK + request: + url: "{{baseUrl}}/pokemons/:id" + method: PUT + params: + - name: id + value: "" + type: path + body: + type: json + data: |- + { + "name": "", + "type": "", + "level": "" + } + response: + status: 200 + statusText: OK + body: + type: text + data: "" diff --git a/works/lab-02/PokedexApi - v1/environments/Environment 1.yml b/works/lab-02/PokedexApi - v1/environments/Environment 1.yml new file mode 100644 index 0000000..56bae67 --- /dev/null +++ b/works/lab-02/PokedexApi - v1/environments/Environment 1.yml @@ -0,0 +1,4 @@ +name: Environment 1 +variables: + - name: baseUrl + value: http://localhost:5114 diff --git a/works/lab-02/PokedexApi - v1/opencollection.yml b/works/lab-02/PokedexApi - v1/opencollection.yml new file mode 100644 index 0000000..842f392 --- /dev/null +++ b/works/lab-02/PokedexApi - v1/opencollection.yml @@ -0,0 +1,10 @@ +opencollection: 1.0.0 + +info: + name: PokedexApi | v1 +bundled: false +extensions: + bruno: + ignore: + - node_modules + - .git diff --git a/works/lab-02/part2/PokedexApi/PokedexApi.sln b/works/lab-02/part2/PokedexApi/PokedexApi.sln new file mode 100644 index 0000000..9157d2e --- /dev/null +++ b/works/lab-02/part2/PokedexApi/PokedexApi.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PokedexApi", "PokedexApi\PokedexApi.csproj", "{597BEB96-604B-40F8-ACE3-13353406B597}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {597BEB96-604B-40F8-ACE3-13353406B597}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {597BEB96-604B-40F8-ACE3-13353406B597}.Debug|Any CPU.Build.0 = Debug|Any CPU + {597BEB96-604B-40F8-ACE3-13353406B597}.Release|Any CPU.ActiveCfg = Release|Any CPU + {597BEB96-604B-40F8-ACE3-13353406B597}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/works/lab-02/part2/PokedexApi/PokedexApi/PokedexApi.csproj b/works/lab-02/part2/PokedexApi/PokedexApi/PokedexApi.csproj new file mode 100644 index 0000000..fcce5d5 --- /dev/null +++ b/works/lab-02/part2/PokedexApi/PokedexApi/PokedexApi.csproj @@ -0,0 +1,14 @@ + + + + net10.0 + enable + enable + + + + + + + + diff --git a/works/lab-02/part2/PokedexApi/PokedexApi/PokedexApi.http b/works/lab-02/part2/PokedexApi/PokedexApi/PokedexApi.http new file mode 100644 index 0000000..9810ad5 --- /dev/null +++ b/works/lab-02/part2/PokedexApi/PokedexApi/PokedexApi.http @@ -0,0 +1,6 @@ +@PokedexApi_HostAddress = http://localhost:5114 + +GET {{PokedexApi_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/works/lab-02/part2/PokedexApi/PokedexApi/Program.cs b/works/lab-02/part2/PokedexApi/PokedexApi/Program.cs new file mode 100644 index 0000000..e39f9ed --- /dev/null +++ b/works/lab-02/part2/PokedexApi/PokedexApi/Program.cs @@ -0,0 +1,110 @@ +var builder = WebApplication.CreateBuilder(args); + +// Подключаем OpenAPI +builder.Services.AddOpenApi(); + +var app = builder.Build(); + +if (app.Environment.IsDevelopment()) +{ + app.MapOpenApi(); +} + +// Хранилище в памяти +var pokemons = new List +{ + new(1, "Pikachu", "Electric", 12), + new(2, "Charmander", "Fire", 8), + new(3, "Squirtle", "Water", 10) +}; + +// Эндпоинты +app.MapGet("/hello", () => "Привет, мир API!"); + +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(); + +// ВСЕ МОДЕЛИ И ВСПОМОГАТЕЛЬНЫЕ МЕТОДЫ — СТРОГО В САМОМ НИЗУ ФАЙЛА: + +static List Validate(PokemonInput input) +{ + var allowedTypes = new[] { "Fire", "Water", "Grass", "Electric", "Psychic", "Normal", "Flying", "Poison" }; + var errors = new List(); + + if (string.IsNullOrWhiteSpace(input.Name) || input.Name.Length < 2) + { + errors.Add("Name должен быть не короче 2 символов"); + } + + if (!allowedTypes.Contains(input.Type)) + { + errors.Add($"Type '{input.Type}' не поддерживается"); + } + + if (input.Level is < 1 or > 100) + { + errors.Add("Level должен быть в диапазоне 1..100"); + } + + return errors; +} + +// Объявление записей (records) тоже в самом низу +public record Pokemon(int Id, string Name, string Type, int Level); +public record PokemonInput(string Name, string Type, int Level); \ No newline at end of file diff --git a/works/lab-02/part2/PokedexApi/PokedexApi/Properties/launchSettings.json b/works/lab-02/part2/PokedexApi/PokedexApi/Properties/launchSettings.json new file mode 100644 index 0000000..af05615 --- /dev/null +++ b/works/lab-02/part2/PokedexApi/PokedexApi/Properties/launchSettings.json @@ -0,0 +1,23 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:5114", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "https://localhost:7027;http://localhost:5114", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/works/lab-02/part2/PokedexApi/PokedexApi/appsettings.Development.json b/works/lab-02/part2/PokedexApi/PokedexApi/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/works/lab-02/part2/PokedexApi/PokedexApi/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/works/lab-02/part2/PokedexApi/PokedexApi/appsettings.json b/works/lab-02/part2/PokedexApi/PokedexApi/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/works/lab-02/part2/PokedexApi/PokedexApi/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}