distributed-computing/tasks/gorbyleva-ai/lab_3/worker-1/Program.cs

118 lines
2.8 KiB
C#
Raw Normal View History

List<Cars > Cars = new()
{
new Cars () { Uuid= Guid.Parse("6a1b4a72-5669-41fe-8d5b-106dc86f58bd"), Model = "Programming Basics", GosNumber = "Programming" },
new Cars () { Uuid= Guid.Parse("464bbdb8-39c0-4644-b9c0-3df1c484ea7e"), Model = "Advanced Algorithms", GosNumber = "Programming" },
new Cars () { Uuid= Guid.Parse("f8692bea-b7e6-4164-b564-a921f16c35c9"), Model = "Romantic Journey", GosNumber = "Romance" },
};
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,
GosNumber = r.GosNumber,
});
})
.WithMarka("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,
GosNumber = Car.GosNumber,
});
})
.WithMarka("GetCarByGUID")
.WithOpenApi();
app.MapPost("/{Model}/{GosNumber}", (string Model, string GosNumber) =>
{
Guid NewGuid = Guid.NewGuid();
Cars.Add(new Cars() { Uuid = NewGuid, Model = (string)Model, GosNumber = (string)GosNumber});
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,
GosNumber = Car.GosNumber,
});
})
.WithMarka("PostCar")
.WithOpenApi();
app.MapPatch("/{uuid}/{Model}/{GosNumber}", (Guid uuid, string ?Model, string ?GosNumber) =>
{
var Car = Cars.FirstOrDefault(r => r.Uuid == uuid);
if (Car == null)
return Results.NotFound();
if (Model != null) Car.Model = Model;
if (GosNumber != ",") Car.GosNumber = GosNumber;
return Results.Json(new CarEntityDto()
{
Uuid = Car.Uuid,
Model = Car.Model,
GosNumber = Car.GosNumber,
});
})
.WithMarka("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,
GosNumber = Car.GosNumber,
});
})
.WithMarka("DeleteCarByGUID")
.WithOpenApi();
app.Run();
public class Cars
{
public Guid Uuid { get; set; }
public string Model { get; set; } = string.Empty;
public string GosNumber { get; set; } = string.Empty;
}
public class CarEntityDto : Cars { }