distributed-computing/tasks/melnikov-ky/lab_3/worker-2/Program.cs

139 lines
3.6 KiB
C#
Raw Permalink Normal View History

2023-12-18 16:06:00 +04:00
List<Parts> parts = new()
{
new Parts() { Uuid= Guid.NewGuid(), Name = "Engine", IsNew = true, IdCar = Guid.Parse("6a1b4a72-5669-41fe-8d5b-106dc86f58bd") },
new Parts() { Uuid= Guid.NewGuid(), Name = "Wheels", IsNew = false, IdCar = Guid.Parse("f8692bea-b7e6-4164-b564-a921f16c35c9") },
new Parts() { Uuid= Guid.NewGuid(), Name = "Transmission", IsNew = false, IdCar = Guid.Parse("464bbdb8-39c0-4644-b9c0-3df1c484ea7e") },
new Parts() { Uuid= Guid.NewGuid(), Name = "Radiator ", IsNew = true, IdCar = 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 parts.Select(r => new PartEntityDto()
{
Uuid = r.Uuid,
Name = r.Name,
IsNew = r.IsNew,
IdCar = r.IdCar,
});
})
.WithName("GetParts")
.WithOpenApi();
app.MapGet("/{uuid}", (Guid uuid) =>
{
var part = parts.FirstOrDefault(r => r.Uuid == uuid);
if (part == null)
return Results.NotFound();
return Results.Json(new PartEntityDto()
{
Uuid = part.Uuid,
Name = part.Name,
IsNew = part.IsNew,
IdCar = part.IdCar,
});
})
.WithName("GetPartByGUID")
.WithOpenApi();
app.MapPost("/{name}/{isNew}/{idCar}", (string? Name, bool IsNew, Guid IdCar) =>
{
Guid NewGuid = Guid.NewGuid();
parts.Add(new Parts() { Uuid = NewGuid, Name = (string)Name, IsNew = (bool)IsNew, IdCar = (Guid)IdCar });
var part = parts.FirstOrDefault(r => r.Uuid == NewGuid);
if (part == null)
return Results.NotFound();
return Results.Json(new PartEntityDto()
{
Uuid = part.Uuid,
Name = part.Name,
IsNew = part.IsNew,
IdCar = part.IdCar,
});
})
.WithName("PostPart")
.WithOpenApi();
app.MapPatch("/{uuid}/{name}/{isNew}/{idCar}", (Guid uuid, string ?name, bool isNew, Guid idCar) =>
{
var part = parts.FirstOrDefault(r => r.Uuid == uuid);
if (part == null)
return Results.NotFound();
if (name != ",") part.Name = name;
if (isNew != part.IsNew) part.IsNew = isNew;
if (idCar != part.IdCar) part.IdCar = idCar;
return Results.Json(new PartEntityDto()
{
Uuid = part.Uuid,
Name = part.Name,
IsNew = part.IsNew,
IdCar = part.IdCar,
});
})
.WithName("UpdatePart")
.WithOpenApi();
app.MapDelete("/{uuid}", (Guid uuid) =>
{
var part = parts.FirstOrDefault(r => r.Uuid == uuid);
if (part == null)
return Results.NotFound();
parts.Remove(part);
return Results.Json(new PartEntityDto()
{
Uuid = part.Uuid,
Name = part.Name,
IsNew = part.IsNew,
IdCar = part.IdCar,
});
})
.WithName("DeletePart")
.WithOpenApi();
app.MapGet("/Parts/", async () =>
{
var httpClient = new HttpClient();
var secondWorkerResponse = await httpClient.GetStringAsync("http://worker-1:8080/");
return secondWorkerResponse.ToArray();
})
.WithName("GetCars")
.WithOpenApi();
app.Run();
public class Parts
{
public Guid Uuid { get; set; }
public string Name { get; set; } = string.Empty;
public bool IsNew { get; set; }
public Guid IdCar { get; set; }
}
public class PartEntityDto : Parts { }
public class Cars
{
public Guid Uuid { get; set; }
public string Model { get; set; } = string.Empty;
public string Brand { get; set; } = string.Empty;
}
public class CarEntityDto : Cars { }