distributed-computing/tasks/nikiforova-ts/Lab_3/worker-2/Program.cs
2023-12-18 17:56:15 +04:00

139 lines
3.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

List<Authors> authors = new()
{
new Authors() { Uuid= Guid.NewGuid(), Number = "1", Name = "Programming Basics", IdBook = Guid.Parse("6a1b4a72-5669-41fe-8d5b-106dc86f58bd") },
new Authors() { Uuid= Guid.NewGuid(), Number = "2", Name = "Advanced Algorithms", IdBook = Guid.Parse("f8692bea-b7e6-4164-b564-a921f16c35c9") },
new Authors() { Uuid= Guid.NewGuid(), Number = "3", Name = "Romantic Journey", IdBook = Guid.Parse("464bbdb8-39c0-4644-b9c0-3df1c484ea7e") },
new Authors() { Uuid= Guid.NewGuid(), Number = "3А", Name = "Mystery Island", IdBook = Guid.Parse("464bbdb8-39c0-4644-b9c0-3df1c484ea7e") },
};
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 authors.Select(r => new AuthorEntityDto()
{
Uuid = r.Uuid,
Number = r.Number,
Name = r.Name,
IdBook = r.IdBook,
});
})
.WithName("GetAuthors")
.WithOpenApi();
app.MapGet("/{uuid}", (Guid uuid) =>
{
var author = authors.FirstOrDefault(r => r.Uuid == uuid);
if (author == null)
return Results.NotFound();
return Results.Json(new AuthorEntityDto()
{
Uuid = author.Uuid,
Number = author.Number,
Name = author.Name,
IdBook = author.IdBook,
});
})
.WithName("GetAuthorByGUID")
.WithOpenApi();
app.MapPost("/{number}/{name}/{IdBook}", (string? Number, string Name, Guid IdBook) =>
{
Guid NewGuid = Guid.NewGuid();
authors.Add(new Authors() { Uuid = NewGuid, Number = (string)Number, Name = (string)Name, IdBook = (Guid)IdBook });
var author = authors.FirstOrDefault(r => r.Uuid == NewGuid);
if (author == null)
return Results.NotFound();
return Results.Json(new AuthorEntityDto()
{
Uuid = author.Uuid,
Number = author.Number,
Name = author.Name,
IdBook = author.IdBook,
});
})
.WithName("PostAuthor")
.WithOpenApi();
app.MapPatch("/{uuid}/{number}/{name}/{IdBook}", (Guid uuid, string ?number, string name, Guid IdBook) =>
{
var author = authors.FirstOrDefault(r => r.Uuid == uuid);
if (author == null)
return Results.NotFound();
if (number != ",") author.Number = number;
if (name != author.Name) author.Name = name;
if (IdBook != author.IdBook) author.IdBook = IdBook;
return Results.Json(new AuthorEntityDto()
{
Uuid = author.Uuid,
Number = author.Number,
Name = author.Name,
IdBook = author.IdBook,
});
})
.WithName("UpdateAuthor")
.WithOpenApi();
app.MapDelete("/{uuid}", (Guid uuid) =>
{
var author = authors.FirstOrDefault(r => r.Uuid == uuid);
if (author == null)
return Results.NotFound();
authors.Remove(author);
return Results.Json(new AuthorEntityDto()
{
Uuid = author.Uuid,
Number = author.Number,
Name = author.Name,
IdBook = author.IdBook,
});
})
.WithName("DeleteAuthor")
.WithOpenApi();
app.MapGet("/Authors/", async () =>
{
var httpClient = new HttpClient();
var secondWorkerResponse = await httpClient.GetStringAsync("http://worker-1:8080/");
return secondWorkerResponse.ToArray();
})
.WithName("GetBooks")
.WithOpenApi();
app.Run();
public class Authors
{
public Guid Uuid { get; set; }
public string Number { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public Guid IdBook { get; set; }
}
public class AuthorEntityDto : Authors { }
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 { }