distributed-computing/tasks/melnikov-ky/lab_3/worker-1/Program.cs
2023-12-18 16:06:00 +04:00

111 lines
2.6 KiB
C#

List<Cars> cars = new()
{
new Cars() { Uuid= Guid.Parse("6a1b4a72-5669-41fe-8d5b-106dc86f58bd"), Model = "Priora", Brand = "Lada"},
new Cars() { Uuid= Guid.Parse("464bbdb8-39c0-4644-b9c0-3df1c484ea7e"), Model = "CRV", Brand = "Honda"},
new Cars() { Uuid= Guid.Parse("f8692bea-b7e6-4164-b564-a921f16c35c9"), Model = "Jumper", Brand = "Citroen"},
};
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 cars.Select(r => new CarEntityDto()
{
Uuid = r.Uuid,
Model = r.Model,
Brand = r.Brand,
});
})
.WithName("GetCars")
.WithOpenApi();
app.MapGet("/{uuid}", (Guid uuid) =>
{
var car = cars.FirstOrDefault(r => r.Uuid == uuid);
if (car == null)
return Results.NotFound();
return Results.Json(new CarEntityDto()
{
Uuid = car.Uuid,
Model = car.Model,
Brand = car.Brand,
});
})
.WithName("GetCarByGUID")
.WithOpenApi();
app.MapPost("/{model}/{brand}", (string Model, string Brand) =>
{
Guid NewGuid = Guid.NewGuid();
cars.Add(new Cars() { Uuid = NewGuid, Model = (string)Model, Brand = (string)Brand});
var car = cars.FirstOrDefault(r => r.Uuid == NewGuid);
if (car == null)
return Results.NotFound();
return Results.Json(new CarEntityDto()
{
Uuid = car.Uuid,
Model = car.Model,
Brand = car.Brand,
});
})
.WithName("PostCar")
.WithOpenApi();
app.MapPatch("/{uuid}/{model}/{brand}", (Guid uuid, string ?model, string ?brand) =>
{
var car = cars.FirstOrDefault(r => r.Uuid == uuid);
if (car == null)
return Results.NotFound();
if (model != null) car.Model = model;
if (brand != null) car.Brand = brand;
return Results.Json(new CarEntityDto()
{
Uuid = car.Uuid,
Model = car.Model,
Brand = car.Brand,
});
})
.WithName("UpdateCar")
.WithOpenApi();
app.MapDelete("/{uuid}", (Guid uuid) =>
{
var car = cars.FirstOrDefault(r => r.Uuid == uuid);
if (car == null)
return Results.NotFound();
cars.Remove(car);
return Results.Json(new CarEntityDto()
{
Uuid = car.Uuid,
Model = car.Model,
Brand = car.Brand,
});
})
.WithName("DeleteCarByGUID")
.WithOpenApi();
app.Run();
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 { }