distributed-computing/tasks/grenaderova-aa/lab_3/worker-2/Program.cs

139 lines
4.2 KiB
C#

List<SpareParts> spareParts = new()
{
new SpareParts() { Uuid= Guid.NewGuid(), Name = "battery", IsSingleSparePart = true, IdPhone = Guid.Parse("6a1b4a72-5669-41fe-8d5b-106dc86f58bd") },
new SpareParts() { Uuid= Guid.NewGuid(), Name = "microphone", IsSingleSparePart = false, IdPhone = Guid.Parse("f8692bea-b7e6-4164-b564-a921f16c35c9") },
new SpareParts() { Uuid= Guid.NewGuid(), Name = "camera", IsSingleSparePart = false, IdPhone = Guid.Parse("464bbdb8-39c0-4644-b9c0-3df1c484ea7e") },
new SpareParts() { Uuid= Guid.NewGuid(), Name = "cable", IsSingleSparePart = true, IdPhone = 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 spareParts.Select(r => new SparePartEntityDto()
{
Uuid = r.Uuid,
Name = r.Name,
IsSingleSparePart = r.IsSingleSparePart,
IdPhone = r.IdPhone,
});
})
.WithName("GetSpareParts")
.WithOpenApi();
app.MapGet("/{uuid}", (Guid uuid) =>
{
var sparePart = spareParts.FirstOrDefault(r => r.Uuid == uuid);
if (sparePart == null)
return Results.NotFound();
return Results.Json(new SparePartEntityDto()
{
Uuid = sparePart.Uuid,
Name = sparePart.Name,
IsSingleSparePart = sparePart.IsSingleSparePart,
IdPhone = sparePart.IdPhone,
});
})
.WithName("GetSparePartByGUID")
.WithOpenApi();
app.MapPost("/{name}/{isSingleSparePart}/{idPhone}", (string? Name, bool IsSingleSparePart, Guid IdPhone) =>
{
Guid NewGuid = Guid.NewGuid();
spareParts.Add(new SpareParts() { Uuid = NewGuid, Name = (string)Name, IsSingleSparePart = (bool)IsSingleSparePart, IdPhone = (Guid)IdPhone });
var sparePart = spareParts.FirstOrDefault(r => r.Uuid == NewGuid);
if (sparePart == null)
return Results.NotFound();
return Results.Json(new SparePartEntityDto()
{
Uuid = sparePart.Uuid,
Name = sparePart.Name,
IsSingleSparePart = sparePart.IsSingleSparePart,
IdPhone = sparePart.IdPhone,
});
})
.WithName("PostSparePart")
.WithOpenApi();
app.MapPatch("/{uuid}/{name}/{isSingleSparePart}/{idPhone}", (Guid uuid, string ?name, bool isSingleSparePart, Guid idPhone) =>
{
var sparePart = spareParts.FirstOrDefault(r => r.Uuid == uuid);
if (sparePart == null)
return Results.NotFound();
if (name != ",") sparePart.Name = name;
if (isSingleSparePart != sparePart.IsSingleSparePart) sparePart.IsSingleSparePart = isSingleSparePart;
if (idPhone != sparePart.IdPhone) sparePart.IdPhone = idPhone;
return Results.Json(new SparePartEntityDto()
{
Uuid = sparePart.Uuid,
Name = sparePart.Name,
IsSingleSparePart = sparePart.IsSingleSparePart,
IdPhone = sparePart.IdPhone,
});
})
.WithName("UpdateSparePart")
.WithOpenApi();
app.MapDelete("/{uuid}", (Guid uuid) =>
{
var sparePart = spareParts.FirstOrDefault(r => r.Uuid == uuid);
if (sparePart == null)
return Results.NotFound();
spareParts.Remove(sparePart);
return Results.Json(new SparePartEntityDto()
{
Uuid = sparePart.Uuid,
Name = sparePart.Name,
IsSingleSparePart = sparePart.IsSingleSparePart,
IdPhone = sparePart.IdPhone,
});
})
.WithName("DeleteSparePart")
.WithOpenApi();
app.MapGet("/SpareParts/", async () =>
{
var httpClient = new HttpClient();
var secondWorkerResponse = await httpClient.GetStringAsync("http://worker-1:8080/");
return secondWorkerResponse.ToArray();
})
.WithName("GetPhones")
.WithOpenApi();
app.Run();
public class SpareParts
{
public Guid Uuid { get; set; }
public string Name { get; set; } = string.Empty;
public bool IsSingleSparePart { get; set; }
public Guid IdPhone { get; set; }
}
public class SparePartEntityDto : SpareParts { }
public class Phones
{
public Guid Uuid { get; set; }
public string Model { get; set; } = string.Empty;
public string Brand { get; set; } = string.Empty;
}
public class PhoneEntityDto : Phones { }