distributed-computing/tasks/mikhailov-ys/Lab_3/worker-1/Program.cs
2024-01-08 17:33:27 +04:00

118 lines
2.9 KiB
C#

List<Countries > countries = new()
{
new Countries () { Uuid= Guid.Parse("6a1b4a72-5669-41fe-8d5b-106dc86f58bd"), Name = "Russia", Area = "1222" },
new Countries () { Uuid= Guid.Parse("464bbdb8-39c0-4644-b9c0-3df1c484ea7e"), Name = "USA", Area = "0" },
new Countries () { Uuid= Guid.Parse("f8692bea-b7e6-4164-b564-a921f16c35c9"), Name = "Germany", Area = "1" },
};
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 countries.Select(r => new CountrieEntityDto()
{
Uuid = r.Uuid,
Name = r.Name,
Area = r.Area,
});
})
.WithName("GetCountries")
.WithOpenApi();
app.MapGet("/{uuid}", (Guid uuid) =>
{
var countrie = countries.FirstOrDefault(r => r.Uuid == uuid);
if (countrie == null)
return Results.NotFound();
return Results.Json(new CountrieEntityDto()
{
Uuid = countrie.Uuid,
Name = countrie.Name,
Area = countrie.Area,
});
})
.WithName("GetCountrieByGUID")
.WithOpenApi();
app.MapPost("/{name}/{area}", (string name, string area) =>
{
Guid NewGuid = Guid.NewGuid();
countries.Add(new Countries() { Uuid = NewGuid, Name = (string)name, Area = (string)area});
var countrie = countries.FirstOrDefault(r => r.Uuid == NewGuid);
if (countrie == null)
return Results.NotFound();
return Results.Json(new CountrieEntityDto()
{
Uuid = countrie.Uuid,
Name = countrie.Name,
Area = countrie.Area,
});
})
.WithName("PostCountrie")
.WithOpenApi();
app.MapPatch("/{uuid}/{name}/{area}", (Guid uuid, string ?name, string ?area) =>
{
var countrie = countries.FirstOrDefault(r => r.Uuid == uuid);
if (countrie == null)
return Results.NotFound();
if (name != null) countrie.Name = name;
if (area != ",") countrie.Area = area;
return Results.Json(new CountrieEntityDto()
{
Uuid = countrie.Uuid,
Name = countrie.Name,
Area = countrie.Area,
});
})
.WithName("UpdateCountrie")
.WithOpenApi();
app.MapDelete("/{uuid}", (Guid uuid) =>
{
var countrie = countries.FirstOrDefault(r => r.Uuid == uuid);
if (countrie == null)
return Results.NotFound();
countries.Remove(countrie);
return Results.Json(new CountrieEntityDto()
{
Uuid = countrie.Uuid,
Name = countrie.Name,
Area = countrie.Area,
});
})
.WithName("DeleteCountrieByGUID")
.WithOpenApi();
app.Run();
public class Countries
{
public Guid Uuid { get; set; }
public string Name { get; set; } = string.Empty;
public string Area { get; set; } = string.Empty;
}
public class CountrieEntityDto : Countries { }