This commit is contained in:
Puzanova
2026-10-16 15:36:40 +03:00
parent 336f87d014
commit 12fc104a6a
43 changed files with 1007 additions and 193 deletions
+16
View File
@@ -0,0 +1,16 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PokedexApi2", "PokedexApi2\PokedexApi2.csproj", "{726EA966-3EC2-4E25-9DC9-5C82383F659B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{726EA966-3EC2-4E25-9DC9-5C82383F659B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{726EA966-3EC2-4E25-9DC9-5C82383F659B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{726EA966-3EC2-4E25-9DC9-5C82383F659B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{726EA966-3EC2-4E25-9DC9-5C82383F659B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
@@ -0,0 +1,15 @@
namespace PokedexApi.Models;
public record Pokemon(int Id, string Name, string Type, int Level);
// TODO 1: объяви record Pokemon
// Поля: Id (int), Name (string), Type (string), Level (int)
public record PokemonInput( string Name, string Type, int Level);
// TODO 2: объяви record PokemonInput
// Поля: Name (string), Type (string), Level (int)
public record LevelUpdate( int Level);
// TODO 3: объяви record LevelUpdate
// Поле: Level (int)
@@ -0,0 +1,18 @@
<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>
<ItemGroup>
<Folder Include="Endpoints\" />
<Folder Include="Validation\" />
</ItemGroup>
</Project>
@@ -0,0 +1,6 @@
@PokedexApi2_HostAddress = http://localhost:5189
GET {{PokedexApi2_HostAddress}}/weatherforecast/
Accept: application/json
###
@@ -0,0 +1,15 @@
using PokedexApi.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.Run();
@@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5189",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7085;http://localhost:5189",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
@@ -0,0 +1,30 @@
using PokedexApi.Models;
namespace PokedexApi.Storage;
public class PokemonStorage
{
private List<Pokemon> _pokemons = new List<Pokemon>
{
new Pokemon(1, "Pikachu", "Electric", 12),
new Pokemon(2, "Charmander", "Fire", 8),
new Pokemon(3, "Squirtle", "Water", 10)
};
// TODO 1: объяви приватный список _pokemons
// с тремя стартовыми покемонами:
// (1, "Pikachu", "Electric", 12)
// (2, "Charmander", "Fire", 8)
// (3, "Squirtle", "Water", 10)
// TODO 2: объяви приватное поле _lock типа object
// TODO 3: реализуй метод GetAll()
// TODO 4: реализуй метод GetById(int id)
// TODO 5: реализуй метод Filter(string? type, int? minLevel)
// TODO 6: реализуй метод SearchByName(string query)
// TODO 7: реализуй метод Add(PokemonInput input)
// TODO 8: реализуй метод Update(int id, PokemonInput input)
// TODO 9: реализуй метод UpdateLevel(int id, int level)
// TODO 10: реализуй метод Delete(int id)
}
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
@@ -0,0 +1,9 @@
# Secrets
.env*
# Dependencies
node_modules
# OS files
.DS_Store
Thumbs.db
+21
View File
@@ -0,0 +1,21 @@
info:
name: "1"
type: http
seq: 1
http:
method: GET
url: https://pokeapi.co/api/v2/pokemon/pikachu
params:
- name: ""
value: ""
type: query
description: Получить покемона по имени (GET)
auth: inherit
settings:
encodeUrl: true
timeout: 0
followRedirects: true
maxRedirects: 5
forwardAuthorizationHeader: false
+16
View File
@@ -0,0 +1,16 @@
info:
name: "2"
type: http
seq: 2
http:
method: GET
url: https://pokeapi.co/api/v2/pokemon/25
auth: inherit
settings:
encodeUrl: true
timeout: 0
followRedirects: true
maxRedirects: 5
forwardAuthorizationHeader: false
+23
View File
@@ -0,0 +1,23 @@
info:
name: "3"
type: http
seq: 3
http:
method: GET
url: " https://pokeapi.co/api/v2/pokemon?limit=5&offset=0"
params:
- name: limit
value: "5"
type: query
- name: offset
value: "0"
type: query
auth: inherit
settings:
encodeUrl: true
timeout: 0
followRedirects: true
maxRedirects: 5
forwardAuthorizationHeader: false
+16
View File
@@ -0,0 +1,16 @@
info:
name: "4"
type: http
seq: 4
http:
method: GET
url: https://pokeapi.co/api/v2/type/electric
auth: inherit
settings:
encodeUrl: true
timeout: 0
followRedirects: true
maxRedirects: 5
forwardAuthorizationHeader: false
+24
View File
@@ -0,0 +1,24 @@
info:
name: "5"
type: http
seq: 5
http:
method: GET
url: |-
POST https://jsonplaceholder.typicode.com/posts
Content-Type: application/json
{
"title": "Мой первый покемон",
"body": "Поймал Пикачу!",
"userId": 1
}
auth: inherit
settings:
encodeUrl: true
timeout: 0
followRedirects: true
maxRedirects: 5
forwardAuthorizationHeader: false
@@ -0,0 +1,10 @@
opencollection: 1.0.0
info:
name: Lab1 — PokeAPI
bundled: false
extensions:
bruno:
ignore:
- node_modules
- .git
@@ -0,0 +1,16 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PokedexApi1", "PokedexApi1\PokedexApi1.csproj", "{BC657A49-E88A-47A7-AD2C-E00C6BDEF14D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BC657A49-E88A-47A7-AD2C-E00C6BDEF14D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BC657A49-E88A-47A7-AD2C-E00C6BDEF14D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BC657A49-E88A-47A7-AD2C-E00C6BDEF14D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BC657A49-E88A-47A7-AD2C-E00C6BDEF14D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
@@ -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 @@
@PokedexApi1_HostAddress = http://localhost:5104
GET {{PokedexApi1_HostAddress}}/weatherforecast/
Accept: application/json
###
@@ -0,0 +1,106 @@
using Scalar.AspNetCore;
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)
};
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.MapScalarApiReference();
}
app.MapGet("/hello", () => "Привет, мир API!");
app.MapGet("/pokemons", () => Results.Ok(pokemons));
app.MapGet("/pokemons/{id:int}", (int id) =>
{
var pokemon = pokemons.FirstOrDefault(p => p.Id == id);
return pokemon is null ? Results.NotFound() : Results.Ok(pokemon);
});
// POST /pokemons — создать нового покемона
app.MapPost("/pokemons", (PokemonInput input) =>
{
var errors = Validate(input);
if (errors.Count > 0)
{
return Results.BadRequest(new {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);
});
// PUT /pokemons/{id} — заменить покемона по id
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);
});
// DELETE /pokemons/{id} — удалить покемона по id
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.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.IsNullOrWhiteSpace(input.Name)) errors.Add("Name должен быть не короче 2 символов");
if (string.IsNullOrWhiteSpace(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);
@@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5104",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7273;http://localhost:5104",
"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": "*"
}
@@ -0,0 +1,39 @@
info:
name: delete /pokemons/:id
type: http
seq: 6
tags:
- PokedexApi1
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: ""
@@ -0,0 +1,6 @@
info:
name: PokedexApi1
type: folder
request:
auth: inherit
@@ -0,0 +1,34 @@
info:
name: get /hello
type: http
seq: 1
tags:
- PokedexApi1
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: ""
@@ -0,0 +1,39 @@
info:
name: get /pokemons/:id
type: http
seq: 4
tags:
- PokedexApi1
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: ""
@@ -0,0 +1,31 @@
info:
name: get /pokemons
type: http
seq: 2
tags:
- PokedexApi1
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: ""
@@ -0,0 +1,47 @@
info:
name: post /pokemons
type: http
seq: 3
tags:
- PokedexApi1
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: ""
@@ -0,0 +1,55 @@
info:
name: put /pokemons/:id
type: http
seq: 5
tags:
- PokedexApi1
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: ""
@@ -0,0 +1,4 @@
name: local
variables:
- name: baseUrl
value: http://localhost:5104
@@ -0,0 +1,10 @@
opencollection: 1.0.0
info:
name: PokedexApi1 | v1
bundled: false
extensions:
bruno:
ignore:
- node_modules
- .git