generated from admin/template
Лаба 1
This commit is contained in:
@@ -0,0 +1,151 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
public record Pokemon(int Id, string Name, string Type, int Level);
|
||||||
|
public record PokemonInput(string Name, string Type, int Level);
|
||||||
|
|
||||||
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
|
||||||
|
builder.Services.AddOpenApi();
|
||||||
|
|
||||||
|
var app = builder.Build();
|
||||||
|
|
||||||
|
if (app.Environment.IsDevelopment())
|
||||||
|
{
|
||||||
|
app.MapOpenApi();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
var pokemons = new List<Pokemon>
|
||||||
|
{
|
||||||
|
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));
|
||||||
|
//задание 2.1
|
||||||
|
app.MapGet("/pokemons/{id}", (int id) =>
|
||||||
|
{
|
||||||
|
var pokemon = pokemons.FirstOrDefault(p => p.Id == id);
|
||||||
|
return pokemon is null ? Results.NotFound() : Results.Ok(pokemon);
|
||||||
|
})
|
||||||
|
.Produces<Pokemon>(StatusCodes.Status200OK)
|
||||||
|
.Produces(StatusCodes.Status404NotFound);
|
||||||
|
|
||||||
|
// задание 2.2: POST /pokemons
|
||||||
|
app.MapPost("/pokemons", (PokemonInput input) =>
|
||||||
|
{
|
||||||
|
var errors = Validate(input);
|
||||||
|
if (errors.Count > 0)
|
||||||
|
{
|
||||||
|
return Results.BadRequest(new { errors });
|
||||||
|
}
|
||||||
|
|
||||||
|
var exusting = pokemons / FirstOrDefault(p => p.Id == id);
|
||||||
|
if (exiting is null)
|
||||||
|
{
|
||||||
|
return Results.NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
})
|
||||||
|
.Produces<Pokemon>(StatusCodes.Status201Created)
|
||||||
|
.Produces<object>(StatusCodes.Status400BadRequest);
|
||||||
|
|
||||||
|
// Задание 2.2: PUT /pokemons/{id}
|
||||||
|
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;
|
||||||
|
if (erors.Count > 0)
|
||||||
|
{
|
||||||
|
return Results.BadRequest(new { errors });
|
||||||
|
|
||||||
|
}
|
||||||
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
builder.Services.AddOpenApi();
|
||||||
|
|
||||||
|
var app = builder.Build();
|
||||||
|
|
||||||
|
if (app.Environment.IsDevelopment())
|
||||||
|
{
|
||||||
|
app.MapOpenApi();
|
||||||
|
}
|
||||||
|
|
||||||
|
app.Run();
|
||||||
|
pokemons.Add(pokemon);
|
||||||
|
|
||||||
|
return Results.Created($"/pokemons/{newId}", pokemon);
|
||||||
|
})
|
||||||
|
.Produces<Pokemon>(StatusCodes.Status201Created)
|
||||||
|
.Produces<object>(StatusCodes.Status400BadRequest);
|
||||||
|
|
||||||
|
// Задание 2.2: PUT /pokemons/{id}
|
||||||
|
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);
|
||||||
|
})
|
||||||
|
|
||||||
|
.Produces<Pokemon>(StatusCodes.Status200OK)
|
||||||
|
.Produces(StatusCodes.Status404NotFound)
|
||||||
|
.Produces<object>(StatusCodes.Status400BadRequest);
|
||||||
|
//Задание 2.3. Валидация
|
||||||
|
static List<string> Validate(PokemonInput input)
|
||||||
|
{
|
||||||
|
var allowedTypes = new[] { "Fire", "Water", "Grass", "Electric",
|
||||||
|
"Psychic", "Normal", "Flying", "Poison" };
|
||||||
|
var errors = new List<string>();
|
||||||
|
|
||||||
|
// TODO 1: проверка NAME
|
||||||
|
if (string.IsNullOrWhiteSpace(input.Name) || input.Name.Length < 2)
|
||||||
|
{
|
||||||
|
errors.Add("Name должен быть не короче 2 символов");
|
||||||
|
}
|
||||||
|
// TODO 2: Проверка Type
|
||||||
|
if (!allowedTypes.Contains(input.Type))
|
||||||
|
{
|
||||||
|
errors.Add($"Type '{input.Type}' не поддерживается");
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO 3: Проверка Level
|
||||||
|
if (input.Level < 1 || input.Level > 100)
|
||||||
|
{
|
||||||
|
errors.Add("Level должен быть в диапозоне 1...........100");
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user