distributed-computing/tasks/nikiforova-ts/Lab_3/worker-1/Program.cs

118 lines
2.8 KiB
C#
Raw Permalink Normal View History

2023-12-18 17:56:15 +04:00
List<Books > books = new()
{
new Books () { Uuid= Guid.Parse("6a1b4a72-5669-41fe-8d5b-106dc86f58bd"), Title = "Programming Basics", Genre = "Programming" },
new Books () { Uuid= Guid.Parse("464bbdb8-39c0-4644-b9c0-3df1c484ea7e"), Title = "Advanced Algorithms", Genre = "Programming" },
new Books () { Uuid= Guid.Parse("f8692bea-b7e6-4164-b564-a921f16c35c9"), Title = "Romantic Journey", Genre = "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 books.Select(r => new BookEntityDto()
{
Uuid = r.Uuid,
Title = r.Title,
Genre = r.Genre,
});
})
.WithName("GetBooks")
.WithOpenApi();
app.MapGet("/{uuid}", (Guid uuid) =>
{
var book = books.FirstOrDefault(r => r.Uuid == uuid);
if (book == null)
return Results.NotFound();
return Results.Json(new BookEntityDto()
{
Uuid = book.Uuid,
Title = book.Title,
Genre = book.Genre,
});
})
.WithName("GetBookByGUID")
.WithOpenApi();
app.MapPost("/{title}/{genre}", (string title, string genre) =>
{
Guid NewGuid = Guid.NewGuid();
books.Add(new Books() { Uuid = NewGuid, Title = (string)title, Genre = (string)genre});
var book = books.FirstOrDefault(r => r.Uuid == NewGuid);
if (book == null)
return Results.NotFound();
return Results.Json(new BookEntityDto()
{
Uuid = book.Uuid,
Title = book.Title,
Genre = book.Genre,
});
})
.WithName("PostBook")
.WithOpenApi();
app.MapPatch("/{uuid}/{title}/{genre}", (Guid uuid, string ?title, string ?genre) =>
{
var book = books.FirstOrDefault(r => r.Uuid == uuid);
if (book == null)
return Results.NotFound();
if (title != null) book.Title = title;
if (genre != ",") book.Genre = genre;
return Results.Json(new BookEntityDto()
{
Uuid = book.Uuid,
Title = book.Title,
Genre = book.Genre,
});
})
.WithName("UpdateBook")
.WithOpenApi();
app.MapDelete("/{uuid}", (Guid uuid) =>
{
var book = books.FirstOrDefault(r => r.Uuid == uuid);
if (book == null)
return Results.NotFound();
books.Remove(book);
return Results.Json(new BookEntityDto()
{
Uuid = book.Uuid,
Title = book.Title,
Genre = book.Genre,
});
})
.WithName("DeleteBookByGUID")
.WithOpenApi();
app.Run();
public class Books
{
public Guid Uuid { get; set; }
public string Title { get; set; } = string.Empty;
public string Genre { get; set; } = string.Empty;
}
public class BookEntityDto : Books { }