distributed-computing/tasks/kazakov-ev/lab_3/worker-1/Program.cs
2023-12-18 12:17:09 +03:00

111 lines
2.8 KiB
C#

List<Streets> streets = new()
{
new Streets() { Uuid= Guid.Parse("6a1b4a72-5669-41fe-8d5b-106dc86f58bd"), Name = "Ленина", City = "Москва"},
new Streets() { Uuid= Guid.Parse("464bbdb8-39c0-4644-b9c0-3df1c484ea7e"), Name = "Транспортная", City = "Ульяновск"},
new Streets() { Uuid= Guid.Parse("f8692bea-b7e6-4164-b564-a921f16c35c9"), Name = "Невская", City = "Санкт-Петербург"},
};
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 streets.Select(r => new StreetEntityDto()
{
Uuid = r.Uuid,
Name = r.Name,
City = r.City,
});
})
.WithName("GetStreets")
.WithOpenApi();
app.MapGet("/{uuid}", (Guid uuid) =>
{
var street = streets.FirstOrDefault(r => r.Uuid == uuid);
if (street == null)
return Results.NotFound();
return Results.Json(new StreetEntityDto()
{
Uuid = street.Uuid,
Name = street.Name,
City = street.City,
});
})
.WithName("GetStreetByGUID")
.WithOpenApi();
app.MapPost("/{name}/{city}", (string Name, string City) =>
{
Guid NewGuid = Guid.NewGuid();
streets.Add(new Streets() { Uuid = NewGuid, Name = (string)Name, City = (string)City});
var street = streets.FirstOrDefault(r => r.Uuid == NewGuid);
if (street == null)
return Results.NotFound();
return Results.Json(new StreetEntityDto()
{
Uuid = street.Uuid,
Name = street.Name,
City = street.City,
});
})
.WithName("PostStreet")
.WithOpenApi();
app.MapPatch("/{uuid}/{name}/{city}", (Guid uuid, string ?name, string ?city) =>
{
var street = streets.FirstOrDefault(r => r.Uuid == uuid);
if (street == null)
return Results.NotFound();
if (name != null) street.Name = name;
if (city != null) street.City = city;
return Results.Json(new StreetEntityDto()
{
Uuid = street.Uuid,
Name = street.Name,
City = street.City,
});
})
.WithName("UpdateStreet")
.WithOpenApi();
app.MapDelete("/{uuid}", (Guid uuid) =>
{
var street = streets.FirstOrDefault(r => r.Uuid == uuid);
if (street == null)
return Results.NotFound();
streets.Remove(street);
return Results.Json(new StreetEntityDto()
{
Uuid = street.Uuid,
Name = street.Name,
City = street.City,
});
})
.WithName("DeleteStreetByGUID")
.WithOpenApi();
app.Run();
public class Streets
{
public Guid Uuid { get; set; }
public string Name { get; set; } = string.Empty;
public string City { get; set; } = string.Empty;
}
public class StreetEntityDto : Streets { }