diff --git a/works/api/lab-01/Import/PokedexAPI import zip file with local settings.zip b/works/api/lab-01/Import/PokedexAPI import zip file with local settings.zip new file mode 100644 index 0000000..7ef425c Binary files /dev/null and b/works/api/lab-01/Import/PokedexAPI import zip file with local settings.zip differ diff --git a/works/api/lab-01/Import/PokedexApi - v1/PokedexApi/delete -pokemons--id.yml b/works/api/lab-01/Import/PokedexApi - v1/PokedexApi/delete -pokemons--id.yml new file mode 100644 index 0000000..741aadf --- /dev/null +++ b/works/api/lab-01/Import/PokedexApi - v1/PokedexApi/delete -pokemons--id.yml @@ -0,0 +1,39 @@ +info: + name: delete /pokemons/:id + type: http + seq: 4 + 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/api/lab-01/Import/PokedexApi - v1/PokedexApi/folder.yml b/works/api/lab-01/Import/PokedexApi - v1/PokedexApi/folder.yml new file mode 100644 index 0000000..f040f0b --- /dev/null +++ b/works/api/lab-01/Import/PokedexApi - v1/PokedexApi/folder.yml @@ -0,0 +1,6 @@ +info: + name: PokedexApi + type: folder + +request: + auth: inherit diff --git a/works/api/lab-01/Import/PokedexApi - v1/PokedexApi/get -hello.yml b/works/api/lab-01/Import/PokedexApi - v1/PokedexApi/get -hello.yml new file mode 100644 index 0000000..9028b14 --- /dev/null +++ b/works/api/lab-01/Import/PokedexApi - v1/PokedexApi/get -hello.yml @@ -0,0 +1,34 @@ +info: + name: get /hello + type: http + seq: 1 + tags: + - PokedexApi + +http: + method: GET + url: "{{local}}/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/api/lab-01/Import/PokedexApi - v1/PokedexApi/get -pokemons--id.yml b/works/api/lab-01/Import/PokedexApi - v1/PokedexApi/get -pokemons--id.yml new file mode 100644 index 0000000..7705caf --- /dev/null +++ b/works/api/lab-01/Import/PokedexApi - v1/PokedexApi/get -pokemons--id.yml @@ -0,0 +1,35 @@ +info: + name: get /pokemons/:id + type: http + seq: 2 + tags: + - PokedexApi + +http: + method: GET + url: "{{local}}/pokemons/1" + 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/api/lab-01/Import/PokedexApi - v1/PokedexApi/post -pokemons.yml b/works/api/lab-01/Import/PokedexApi - v1/PokedexApi/post -pokemons.yml new file mode 100644 index 0000000..4ede6c1 --- /dev/null +++ b/works/api/lab-01/Import/PokedexApi - v1/PokedexApi/post -pokemons.yml @@ -0,0 +1,47 @@ +info: + name: post /pokemons + type: http + seq: 5 + 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/api/lab-01/Import/PokedexApi - v1/PokedexApi/put -pokemons--id.yml b/works/api/lab-01/Import/PokedexApi - v1/PokedexApi/put -pokemons--id.yml new file mode 100644 index 0000000..11e1a90 --- /dev/null +++ b/works/api/lab-01/Import/PokedexApi - v1/PokedexApi/put -pokemons--id.yml @@ -0,0 +1,51 @@ +info: + name: put /pokemons/:id + type: http + seq: 3 + tags: + - PokedexApi + +http: + method: PUT + url: "{{local}}/pokemons/1" + 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/api/lab-01/Import/PokedexApi - v1/environments/Environment 1.yml b/works/api/lab-01/Import/PokedexApi - v1/environments/Environment 1.yml new file mode 100644 index 0000000..12d4c4c --- /dev/null +++ b/works/api/lab-01/Import/PokedexApi - v1/environments/Environment 1.yml @@ -0,0 +1,5 @@ +name: Environment 1 +variables: + - name: local + value: http://localhost:5166 + description: "" diff --git a/works/api/lab-01/Import/PokedexApi - v1/opencollection.yml b/works/api/lab-01/Import/PokedexApi - v1/opencollection.yml new file mode 100644 index 0000000..842f392 --- /dev/null +++ b/works/api/lab-01/Import/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/api/lab-01/PokedexApi/PokedexApi/PokedexApi.http b/works/api/lab-01/PokedexApi/PokedexApi/PokedexApi.http new file mode 100644 index 0000000..13febbf --- /dev/null +++ b/works/api/lab-01/PokedexApi/PokedexApi/PokedexApi.http @@ -0,0 +1,6 @@ +@PokedexApi_HostAddress = http://localhost:5166 + +GET {{PokedexApi_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/works/api/lab-01/PokedexApi/PokedexApi/Program.cs b/works/api/lab-01/PokedexApi/PokedexApi/Program.cs new file mode 100644 index 0000000..7751949 --- /dev/null +++ b/works/api/lab-01/PokedexApi/PokedexApi/Program.cs @@ -0,0 +1,112 @@ +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddOpenApi(); + +var app = builder.Build(); + +var pokemons = new List +{ + new(1, "Pikachu", "Electric", 12), + new(2, "Charmander", "Fire", 8), + new(3, "Squirtle", "Water", 10) +}; + +if (app.Environment.IsDevelopment()) +{ + app.MapOpenApi(); + +} + +app.MapGet("/hello", () => "Привет, мир API!"); + +// METHOD GET +app.MapGet("/pokemons/{id}", (int id) => +{ + var pokemon = pokemons.FirstOrDefault(p => p.Id == id); + + if (pokemon == null) + { + return Results.NotFound(); + } + else + { + return Results.Ok(pokemon); + } +}); + + + +// METHOD POST +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); +}); + +// METHOD PUT + +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.Run(); +// написали свою валидацию, потому что нет встроенного метода +List Validate(PokemonInput input) +{ + var errors = new List(); + if (string.IsNullOrWhiteSpace(input.Name) || input.Name.Length < 2) errors.Add("Имя не может быть короче 2 символов"); + if (input.Level <= 0 || input.Level > 100) errors.Add("Уровень должен быть от 1 до 100"); + var allowedTypes = new[] { "Fire", "Water", "Grass", "Electric", "Physic", "Normal", "Flying", "Poison" }; + if (!allowedTypes.Contains(input.Type)) errors.Add($"Тип должен быть одним из: {string.Join(", ", allowedTypes)}"); + return errors; +} + +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/api/lab-01/PokedexApi/PokedexApi/Properties/launchSettings.json b/works/api/lab-01/PokedexApi/PokedexApi/Properties/launchSettings.json new file mode 100644 index 0000000..b631bda --- /dev/null +++ b/works/api/lab-01/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:5166", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "https://localhost:7279;http://localhost:5166", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/works/api/lab-01/PokedexApi/PokedexApi/appsettings.Development.json b/works/api/lab-01/PokedexApi/PokedexApi/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/works/api/lab-01/PokedexApi/PokedexApi/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/works/api/lab-01/PokedexApi/PokedexApi/appsettings.json b/works/api/lab-01/PokedexApi/PokedexApi/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/works/api/lab-01/PokedexApi/PokedexApi/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}