113 lines
2.4 KiB
C#
113 lines
2.4 KiB
C#
|
using System.Xml.Linq;
|
||
|
|
||
|
List<Cars> cars = new()
|
||
|
{
|
||
|
new Cars() { Uuid= Guid.Parse("6a1b4a72-5669-41fe-8d5b-106dc86f58bd"), CarMake = "Audi", Model = "R8"},
|
||
|
new Cars() { Uuid= Guid.Parse("464bbdb8-39c0-4644-b9c0-3df1c484ea7e"), CarMake = "BMW", Model = "X7"},
|
||
|
new Cars() { Uuid= Guid.Parse("f8692bea-b7e6-4164-b564-a921f16c35c9"), CarMake = "Kia", Model = "Rio"},
|
||
|
};
|
||
|
|
||
|
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,
|
||
|
CarMake = r.CarMake,
|
||
|
Model = r.Model,
|
||
|
});
|
||
|
})
|
||
|
.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,
|
||
|
CarMake = car.CarMake,
|
||
|
Model = car.Model,
|
||
|
});
|
||
|
})
|
||
|
.WithName("GetCarByGUID")
|
||
|
.WithOpenApi();
|
||
|
|
||
|
app.MapPost("/{carmake}/{modek}", (string CarMake, string Model) =>
|
||
|
{
|
||
|
Guid NewGuid = Guid.NewGuid();
|
||
|
cars.Add(new Cars() { Uuid = NewGuid, CarMake = (string)CarMake, Model = (string)Model });
|
||
|
|
||
|
var car = cars.FirstOrDefault(r => r.Uuid == NewGuid);
|
||
|
if (car == null)
|
||
|
return Results.NotFound();
|
||
|
return Results.Json(new CarEntityDto()
|
||
|
{
|
||
|
Uuid = car.Uuid,
|
||
|
CarMake = car.CarMake,
|
||
|
Model = car.Model,
|
||
|
});
|
||
|
})
|
||
|
.WithName("PostCar")
|
||
|
.WithOpenApi();
|
||
|
|
||
|
app.MapPatch("/{uuid}/{carmake}/{model}", (Guid uuid, string? carmake, string? model) =>
|
||
|
{
|
||
|
var car = cars.FirstOrDefault(r => r.Uuid == uuid);
|
||
|
if (car == null)
|
||
|
return Results.NotFound();
|
||
|
if (carmake != null) car.CarMake = carmake;
|
||
|
if (model != null) car.Model = model;
|
||
|
|
||
|
return Results.Json(new CarEntityDto()
|
||
|
{
|
||
|
Uuid = car.Uuid,
|
||
|
CarMake = car.CarMake,
|
||
|
Model = car.Model,
|
||
|
});
|
||
|
})
|
||
|
.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,
|
||
|
CarMake = car.CarMake,
|
||
|
Model = car.Model,
|
||
|
});
|
||
|
})
|
||
|
.WithName("DeleteCarByGUID")
|
||
|
.WithOpenApi();
|
||
|
|
||
|
app.Run();
|
||
|
|
||
|
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 { }
|