distributed-computing/tasks/dunaev-oi/worker-1/Program.cs
2023-12-18 16:35:00 +04:00

112 lines
2.7 KiB
C#

List<Pets> pets = new()
{
new Pets() { Uuid= Guid.Parse("6a1b4a72-5669-41fe-8d5b-106dc86f58bd"), Animals = "Кот", Breed = "Бигль"},
new Pets() { Uuid= Guid.Parse("464bbdb8-39c0-4644-b9c0-3df1c484ea7e"), Animals = "Собака", Breed = "Мейн-Кун"},
new Pets() { Uuid= Guid.Parse("f8692bea-b7e6-4164-b564-a921f16c35c9"), Animals = "Хомяк", Breed = "Лемминг"},
};
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 pets.Select(r => new PetEntityDto()
{
Uuid = r.Uuid,
Animals = r.Animals,
Breed = r.Breed,
});
})
.WithName("GetPets")
.WithOpenApi();
app.MapGet("/{uuid}", (Guid uuid) =>
{
var pet = pets.FirstOrDefault(r => r.Uuid == uuid);
if (pet == null)
return Results.NotFound();
return Results.Json(new PetEntityDto()
{
Uuid = pet.Uuid,
Animals = pet.Animals,
Breed = pet.Breed,
});
})
.WithName("GetPetByGUID")
.WithOpenApi();
app.MapPost("/{animals}/{breed}", (string Animals, string Breed) =>
{
Guid NewGuid = Guid.NewGuid();
pets.Add(new Pets() { Uuid = NewGuid, Animals = (string)Animals, Breed = (string)Breed});
var pet = pets.FirstOrDefault(r => r.Uuid == NewGuid);
if (pet == null)
return Results.NotFound();
return Results.Json(new PetEntityDto()
{
Uuid = pet.Uuid,
Animals = pet.Animals,
Breed = pet.Breed,
});
})
.WithName("PostPet")
.WithOpenApi();
app.MapPatch("/{uuid}/{animals}/{breed}", (Guid uuid, string ?animals, string ?breed) =>
{
var pet = pets.FirstOrDefault(r => r.Uuid == uuid);
if (pet == null)
return Results.NotFound();
if (animals != null) pet.Animals = animals;
if (breed != null) pet.Breed = breed;
return Results.Json(new PetEntityDto()
{
Uuid = pet.Uuid,
Animals = pet.Animals,
Breed = pet.Breed,
});
})
.WithName("UpdatePet")
.WithOpenApi();
app.MapDelete("/{uuid}", (Guid uuid) =>
{
var pet = pets.FirstOrDefault(r => r.Uuid == uuid);
if (pet == null)
return Results.NotFound();
pets.Remove(pet);
return Results.Json(new PetEntityDto()
{
Uuid = pet.Uuid,
Animals = pet.Animals,
Breed = pet.Breed,
});
})
.WithName("DeletePetByGUID")
.WithOpenApi();
app.Run();
public class Pets
{
public Guid Uuid { get; set; }
public string Animals { get; set; } = string.Empty;
public string Breed { get; set; } = string.Empty;
}
public class PetEntityDto : Pets { }