distributed-computing/tasks/yudenicheva-ds/lab_3/worker-2/Program.cs

141 lines
4.2 KiB
C#

using System.Reflection.PortableExecutable;
using System.Xml.Linq;
List<Characteristics> characteristics = new()
{
new Characteristics() { Uuid= Guid.NewGuid(), Name = "ledHeadlights", IsSingleSparePart = true, IdCar = Guid.Parse("6a1b4a72-5669-41fe-8d5b-106dc86f58bd") },
new Characteristics() { Uuid= Guid.NewGuid(), Name = "powerSteering", IsSingleSparePart = false, IdCar = Guid.Parse("f8692bea-b7e6-4164-b564-a921f16c35c9") },
new Characteristics() { Uuid= Guid.NewGuid(), Name = "factoryTint", IsSingleSparePart = false, IdCar = Guid.Parse("464bbdb8-39c0-4644-b9c0-3df1c484ea7e") },
new Characteristics() { Uuid= Guid.NewGuid(), Name = "centralLocking", IsSingleSparePart = 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 characteristics.Select(r => new CharacteristicsEntityDto()
{
Uuid = r.Uuid,
Name = r.Name,
IsSingleSparePart = r.IsSingleSparePart,
IdCar = r.IdCar,
});
})
.WithName("GetCharacteristics")
.WithOpenApi();
app.MapGet("/{uuid}", (Guid uuid) =>
{
var characteristic = characteristics.FirstOrDefault(r => r.Uuid == uuid);
if (characteristic == null)
return Results.NotFound();
return Results.Json(new CharacteristicsEntityDto()
{
Uuid = characteristic.Uuid,
Name = characteristic.Name,
IsSingleSparePart = characteristic.IsSingleSparePart,
IdCar = characteristic.IdCar,
});
})
.WithName("GetCharacteristicByGUID")
.WithOpenApi();
app.MapPost("/{name}/{isSingleSparePart}/{idCar}", (string? Name, bool IsSingleSparePart, Guid IdCar) =>
{
Guid NewGuid = Guid.NewGuid();
characteristics.Add(new Characteristics() { Uuid = NewGuid, Name = (string)Name, IsSingleSparePart = (bool)IsSingleSparePart, IdCar = (Guid)IdCar });
var characteristic = characteristics.FirstOrDefault(r => r.Uuid == NewGuid);
if (characteristic == null)
return Results.NotFound();
return Results.Json(new CharacteristicsEntityDto()
{
Uuid = characteristic.Uuid,
Name = characteristic.Name,
IsSingleSparePart = characteristic.IsSingleSparePart,
IdCar = characteristic.IdCar,
});
})
.WithName("PostCharacteristic")
.WithOpenApi();
app.MapPatch("/{uuid}/{name}/{isSingleSparePart}/{idCar}", (Guid uuid, string? name, bool isSingleSparePart, Guid idCar) =>
{
var characteristic = characteristics.FirstOrDefault(r => r.Uuid == uuid);
if (characteristic == null)
return Results.NotFound();
if (name != ",") characteristic.Name = name;
if (isSingleSparePart != characteristic.IsSingleSparePart) characteristic.IsSingleSparePart = isSingleSparePart;
if (idCar != characteristic.IdCar) characteristic.IdCar = idCar;
return Results.Json(new CharacteristicsEntityDto()
{
Uuid = characteristic.Uuid,
Name = characteristic.Name,
IsSingleSparePart = characteristic.IsSingleSparePart,
IdCar = characteristic.IdCar,
});
})
.WithName("UpdateCharacteristic")
.WithOpenApi();
app.MapDelete("/{uuid}", (Guid uuid) =>
{
var characteristic = characteristics.FirstOrDefault(r => r.Uuid == uuid);
if (characteristic == null)
return Results.NotFound();
characteristics.Remove(characteristic);
return Results.Json(new CharacteristicsEntityDto()
{
Uuid = characteristic.Uuid,
Name = characteristic.Name,
IsSingleSparePart = characteristic.IsSingleSparePart,
IdCar = characteristic.IdCar,
});
})
.WithName("DeleteCharacteristic")
.WithOpenApi();
app.MapGet("/Characteristics/", async () =>
{
var httpClient = new HttpClient();
var secondWorkerResponse = await httpClient.GetStringAsync("http://worker-1:8080/");
return secondWorkerResponse.ToArray();
})
.WithName("GetCars")
.WithOpenApi();
app.Run();
public class Characteristics
{
public Guid Uuid { get; set; }
public string Name { get; set; } = string.Empty;
public bool IsSingleSparePart { get; set; }
public Guid IdCar { get; set; }
}
public class CharacteristicsEntityDto : Characteristics { }
public class Cars
{
public Guid Uuid { get; set; }
public string CarMake { get; set; } = string.Empty;
public string Model { get; set; } = string.Empty;
}
public class CarEntityDto : Cars { }