111 lines
3.0 KiB
C#
111 lines
3.0 KiB
C#
List<Categories> categories = new()
|
|
{
|
|
new Categories() { Uuid= Guid.Parse("6a1b4a72-5669-41fe-8d5b-106dc86f58bd"), Name = "Неуловимые мстители", Product = "Фильм"},
|
|
new Categories() { Uuid= Guid.Parse("464bbdb8-39c0-4644-b9c0-3df1c484ea7e"), Name = "Уэнздей", Product = "Сериал"},
|
|
new Categories() { Uuid= Guid.Parse("f8692bea-b7e6-4164-b564-a921f16c35c9"), Name = "Война и мир", Product = "Книга"},
|
|
};
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
var app = builder.Build();
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.MapGet("/", () =>
|
|
{
|
|
return categories.Select(r => new CategoryEntityDto()
|
|
{
|
|
Uuid = r.Uuid,
|
|
Name = r.Name,
|
|
Product = r.Product,
|
|
});
|
|
})
|
|
.WithName("GetCategories")
|
|
.WithOpenApi();
|
|
|
|
app.MapGet("/{uuid}", (Guid uuid) =>
|
|
{
|
|
var category = categories.FirstOrDefault(r => r.Uuid == uuid);
|
|
if (category == null)
|
|
return Results.NotFound();
|
|
return Results.Json(new CategoryEntityDto()
|
|
{
|
|
Uuid = category.Uuid,
|
|
Name = category.Name,
|
|
Product = category.Product,
|
|
});
|
|
})
|
|
.WithName("GetCategoryByGUID")
|
|
.WithOpenApi();
|
|
|
|
app.MapPost("/{name}/{product}", (string Name, string Product) =>
|
|
{
|
|
Guid NewGuid = Guid.NewGuid();
|
|
categories.Add(new Categories() { Uuid = NewGuid, Name = (string)Name, Product = (string)Product});
|
|
|
|
var category = categories.FirstOrDefault(r => r.Uuid == NewGuid);
|
|
if (category == null)
|
|
return Results.NotFound();
|
|
return Results.Json(new CategoryEntityDto()
|
|
{
|
|
Uuid = category.Uuid,
|
|
Name = category.Name,
|
|
Product = category.Product,
|
|
});
|
|
})
|
|
.WithName("PostCategory")
|
|
.WithOpenApi();
|
|
|
|
app.MapPatch("/{uuid}/{name}/{product}", (Guid uuid, string ?name, string ?product) =>
|
|
{
|
|
var category = categories.FirstOrDefault(r => r.Uuid == uuid);
|
|
if (category == null)
|
|
return Results.NotFound();
|
|
if (name != null) category.Name = name;
|
|
if (product != null) category.Product = product;
|
|
|
|
return Results.Json(new CategoryEntityDto()
|
|
{
|
|
Uuid = category.Uuid,
|
|
Name = category.Name,
|
|
Product = category.Product,
|
|
});
|
|
})
|
|
.WithName("UpdateCategory")
|
|
.WithOpenApi();
|
|
|
|
app.MapDelete("/{uuid}", (Guid uuid) =>
|
|
{
|
|
var category = categories.FirstOrDefault(r => r.Uuid == uuid);
|
|
if (category == null)
|
|
return Results.NotFound();
|
|
categories.Remove(category);
|
|
return Results.Json(new CategoryEntityDto()
|
|
{
|
|
Uuid = category.Uuid,
|
|
Name = category.Name,
|
|
Product = category.Product,
|
|
});
|
|
})
|
|
.WithName("DeleteCategoryByGUID")
|
|
.WithOpenApi();
|
|
|
|
app.Run();
|
|
|
|
public class Categories
|
|
{
|
|
public Guid Uuid { get; set; }
|
|
public string Name { get; set; } = string.Empty;
|
|
public string Product { get; set; } = string.Empty;
|
|
}
|
|
|
|
public class CategoryEntityDto : Categories { } |