distributed-computing/tasks/kazakov-ev/lab_3/worker-2/Program.cs

139 lines
3.9 KiB
C#
Raw Permalink Normal View History

2023-12-18 13:17:09 +04:00
List<Houses> houses = new()
{
new Houses() { Uuid= Guid.NewGuid(), Number = "1", IsSingleHouse = true, IdStreet = Guid.Parse("6a1b4a72-5669-41fe-8d5b-106dc86f58bd") },
new Houses() { Uuid= Guid.NewGuid(), Number = "2", IsSingleHouse = false, IdStreet = Guid.Parse("f8692bea-b7e6-4164-b564-a921f16c35c9") },
new Houses() { Uuid= Guid.NewGuid(), Number = "3", IsSingleHouse = false, IdStreet = Guid.Parse("464bbdb8-39c0-4644-b9c0-3df1c484ea7e") },
new Houses() { Uuid= Guid.NewGuid(), Number = "3А", IsSingleHouse = true, IdStreet = Guid.Parse("464bbdb8-39c0-4644-b9c0-3df1c484ea7e") },
};
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 houses.Select(r => new HouseEntityDto()
{
Uuid = r.Uuid,
Number = r.Number,
IsSingleHouse = r.IsSingleHouse,
IdStreet = r.IdStreet,
});
})
.WithName("GetHouses")
.WithOpenApi();
app.MapGet("/{uuid}", (Guid uuid) =>
{
var house = houses.FirstOrDefault(r => r.Uuid == uuid);
if (house == null)
return Results.NotFound();
return Results.Json(new HouseEntityDto()
{
Uuid = house.Uuid,
Number = house.Number,
IsSingleHouse = house.IsSingleHouse,
IdStreet = house.IdStreet,
});
})
.WithName("GetHouseByGUID")
.WithOpenApi();
app.MapPost("/{number}/{isSingleHouse}/{idStreet}", (string? Number, bool IsSingleHouse, Guid IdStreet) =>
{
Guid NewGuid = Guid.NewGuid();
houses.Add(new Houses() { Uuid = NewGuid, Number = (string)Number, IsSingleHouse = (bool)IsSingleHouse, IdStreet = (Guid)IdStreet });
var house = houses.FirstOrDefault(r => r.Uuid == NewGuid);
if (house == null)
return Results.NotFound();
return Results.Json(new HouseEntityDto()
{
Uuid = house.Uuid,
Number = house.Number,
IsSingleHouse = house.IsSingleHouse,
IdStreet = house.IdStreet,
});
})
.WithName("PostHouse")
.WithOpenApi();
app.MapPatch("/{uuid}/{number}/{isSingleHouse}/{idStreet}", (Guid uuid, string ?number, bool isSingleHouse, Guid idStreet) =>
{
var house = houses.FirstOrDefault(r => r.Uuid == uuid);
if (house == null)
return Results.NotFound();
if (number != ",") house.Number = number;
if (isSingleHouse != house.IsSingleHouse) house.IsSingleHouse = isSingleHouse;
if (idStreet != house.IdStreet) house.IdStreet = idStreet;
return Results.Json(new HouseEntityDto()
{
Uuid = house.Uuid,
Number = house.Number,
IsSingleHouse = house.IsSingleHouse,
IdStreet = house.IdStreet,
});
})
.WithName("UpdateHouse")
.WithOpenApi();
app.MapDelete("/{uuid}", (Guid uuid) =>
{
var house = houses.FirstOrDefault(r => r.Uuid == uuid);
if (house == null)
return Results.NotFound();
houses.Remove(house);
return Results.Json(new HouseEntityDto()
{
Uuid = house.Uuid,
Number = house.Number,
IsSingleHouse = house.IsSingleHouse,
IdStreet = house.IdStreet,
});
})
.WithName("DeleteHouse")
.WithOpenApi();
app.MapGet("/Houses/", async () =>
{
var httpClient = new HttpClient();
var secondWorkerResponse = await httpClient.GetStringAsync("http://worker-1:8080/");
return secondWorkerResponse.ToArray();
})
.WithName("GetStreets")
.WithOpenApi();
app.Run();
public class Houses
{
public Guid Uuid { get; set; }
public string Number { get; set; } = string.Empty;
public bool IsSingleHouse { get; set; }
public Guid IdStreet { get; set; }
}
public class HouseEntityDto : Houses { }
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 { }