distributed-computing/tasks/lyamzina-ma/lab_3/worker-2/Program.cs

139 lines
4.0 KiB
C#
Raw Permalink Normal View History

2023-12-18 16:15:17 +04:00
List<Genres> genres = new()
{
new Genres() { Uuid= Guid.NewGuid(), Title = "фэнтези", IsSingleGenre = true, IdCategory = Guid.Parse("6a1b4a72-5669-41fe-8d5b-106dc86f58bd") },
new Genres() { Uuid= Guid.NewGuid(), Title = "драма", IsSingleGenre = false, IdCategory = Guid.Parse("f8692bea-b7e6-4164-b564-a921f16c35c9") },
new Genres() { Uuid= Guid.NewGuid(), Title = "комедия", IsSingleGenre = false, IdCategory = Guid.Parse("464bbdb8-39c0-4644-b9c0-3df1c484ea7e") },
new Genres() { Uuid= Guid.NewGuid(), Title = "боевик", IsSingleGenre = true, IdCategory = 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 genres.Select(r => new GenreEntityDto()
{
Uuid = r.Uuid,
Title = r.Title,
IsSingleGenre = r.IsSingleGenre,
IdCategory = r.IdCategory,
});
})
.WithName("GetGenres")
.WithOpenApi();
app.MapGet("/{uuid}", (Guid uuid) =>
{
var genre = genres.FirstOrDefault(r => r.Uuid == uuid);
if (genre == null)
return Results.NotFound();
return Results.Json(new GenreEntityDto()
{
Uuid = genre.Uuid,
Title = genre.Title,
IsSingleGenre = genre.IsSingleGenre,
IdCategory = genre.IdCategory,
});
})
.WithName("GetGenreByGUID")
.WithOpenApi();
app.MapPost("/{title}/{isSingleGenre}/{idCategory}", (string? Title, bool IsSingleGenre, Guid IdCategory) =>
{
Guid NewGuid = Guid.NewGuid();
genres.Add(new Genres() { Uuid = NewGuid, Title = (string)Title, IsSingleGenre = (bool)IsSingleGenre, IdCategory = (Guid)IdCategory });
var genre = genres.FirstOrDefault(r => r.Uuid == NewGuid);
if (genre == null)
return Results.NotFound();
return Results.Json(new GenreEntityDto()
{
Uuid = genre.Uuid,
Title = genre.Title,
IsSingleGenre = genre.IsSingleGenre,
IdCategory = genre.IdCategory,
});
})
.WithName("PostGenre")
.WithOpenApi();
app.MapPatch("/{uuid}/{title}/{isSingleGenre}/{idCategory}", (Guid uuid, string ?title, bool isSingleGenre, Guid idCategory) =>
{
var genre = genres.FirstOrDefault(r => r.Uuid == uuid);
if (genre == null)
return Results.NotFound();
if (title != ",") genre.Title = title;
if (isSingleGenre != genre.IsSingleGenre) genre.IsSingleGenre = isSingleGenre;
if (idCategory != genre.IdCategory) genre.IdCategory = idCategory;
return Results.Json(new GenreEntityDto()
{
Uuid = genre.Uuid,
Title = genre.Title,
IsSingleGenre = genre.IsSingleGenre,
IdCategory = genre.IdCategory,
});
})
.WithName("UpdateGenre")
.WithOpenApi();
app.MapDelete("/{uuid}", (Guid uuid) =>
{
var genre = genres.FirstOrDefault(r => r.Uuid == uuid);
if (genre == null)
return Results.NotFound();
genres.Remove(genre);
return Results.Json(new GenreEntityDto()
{
Uuid = genre.Uuid,
Title = genre.Title,
IsSingleGenre = genre.IsSingleGenre,
IdCategory = genre.IdCategory,
});
})
.WithName("DeleteGenre")
.WithOpenApi();
app.MapGet("/Genres/", async () =>
{
var httpClient = new HttpClient();
var secondWorkerResponse = await httpClient.GetStringAsync("http://worker-1:8080/");
return secondWorkerResponse.ToArray();
})
.WithName("GetCategories")
.WithOpenApi();
app.Run();
public class Genres
{
public Guid Uuid { get; set; }
public string Title { get; set; } = string.Empty;
public bool IsSingleGenre { get; set; }
public Guid IdCategory { get; set; }
}
public class GenreEntityDto : Genres { }
public class Categories
{
public Guid Uuid { get; set; }
public string Name { get; set; } = string.Empty;
public string Product { get; set; } = string.Empty;
}
public class CategoryEntityDto : Categories { }