111 lines
2.7 KiB
C#
111 lines
2.7 KiB
C#
|
List<Phones> phones = new()
|
||
|
{
|
||
|
new Phones() { Uuid= Guid.Parse("6a1b4a72-5669-41fe-8d5b-106dc86f58bd"), Model = "Galaxy A", Brand = "Samsung"},
|
||
|
new Phones() { Uuid= Guid.Parse("464bbdb8-39c0-4644-b9c0-3df1c484ea7e"), Model = "XS", Brand = "Apple"},
|
||
|
new Phones() { Uuid= Guid.Parse("f8692bea-b7e6-4164-b564-a921f16c35c9"), Model = "Redmi Note 10", Brand = "Xiaomi"},
|
||
|
};
|
||
|
|
||
|
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 phones.Select(r => new PhoneEntityDto()
|
||
|
{
|
||
|
Uuid = r.Uuid,
|
||
|
Model = r.Model,
|
||
|
Brand = r.Brand,
|
||
|
});
|
||
|
})
|
||
|
.WithName("GetPhones")
|
||
|
.WithOpenApi();
|
||
|
|
||
|
app.MapGet("/{uuid}", (Guid uuid) =>
|
||
|
{
|
||
|
var phone = phones.FirstOrDefault(r => r.Uuid == uuid);
|
||
|
if (phone == null)
|
||
|
return Results.NotFound();
|
||
|
return Results.Json(new PhoneEntityDto()
|
||
|
{
|
||
|
Uuid = phone.Uuid,
|
||
|
Model = phone.Model,
|
||
|
Brand = phone.Brand,
|
||
|
});
|
||
|
})
|
||
|
.WithName("GetPhoneByGUID")
|
||
|
.WithOpenApi();
|
||
|
|
||
|
app.MapPost("/{model}/{brand}", (string Model, string Brand) =>
|
||
|
{
|
||
|
Guid NewGuid = Guid.NewGuid();
|
||
|
phones.Add(new Phones() { Uuid = NewGuid, Model = (string)Model, Brand = (string)Brand});
|
||
|
|
||
|
var phone = phones.FirstOrDefault(r => r.Uuid == NewGuid);
|
||
|
if (phone == null)
|
||
|
return Results.NotFound();
|
||
|
return Results.Json(new PhoneEntityDto()
|
||
|
{
|
||
|
Uuid = phone.Uuid,
|
||
|
Model = phone.Model,
|
||
|
Brand = phone.Brand,
|
||
|
});
|
||
|
})
|
||
|
.WithName("PostPhone")
|
||
|
.WithOpenApi();
|
||
|
|
||
|
app.MapPatch("/{uuid}/{model}/{brand}", (Guid uuid, string ?model, string ?brand) =>
|
||
|
{
|
||
|
var phone = phones.FirstOrDefault(r => r.Uuid == uuid);
|
||
|
if (phone == null)
|
||
|
return Results.NotFound();
|
||
|
if (model != null) phone.Model = model;
|
||
|
if (brand != null) phone.Brand = brand;
|
||
|
|
||
|
return Results.Json(new PhoneEntityDto()
|
||
|
{
|
||
|
Uuid = phone.Uuid,
|
||
|
Model = phone.Model,
|
||
|
Brand = phone.Brand,
|
||
|
});
|
||
|
})
|
||
|
.WithName("UpdatePhone")
|
||
|
.WithOpenApi();
|
||
|
|
||
|
app.MapDelete("/{uuid}", (Guid uuid) =>
|
||
|
{
|
||
|
var phone = phones.FirstOrDefault(r => r.Uuid == uuid);
|
||
|
if (phone == null)
|
||
|
return Results.NotFound();
|
||
|
phones.Remove(phone);
|
||
|
return Results.Json(new PhoneEntityDto()
|
||
|
{
|
||
|
Uuid = phone.Uuid,
|
||
|
Model = phone.Model,
|
||
|
Brand = phone.Brand,
|
||
|
});
|
||
|
})
|
||
|
.WithName("DeletePhoneByGUID")
|
||
|
.WithOpenApi();
|
||
|
|
||
|
app.Run();
|
||
|
|
||
|
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 { }
|