distributed-computing/tasks/dunaev-oi/lab3/worker-2/Program.cs

139 lines
4.2 KiB
C#
Raw Permalink Normal View History

2023-12-18 16:35:00 +04:00
List<Location> locations = new()
{
new Location() { Uuid= Guid.NewGuid(), Habitat = "Америка", IsSingleLocation = true, IdBreed = Guid.Parse("6a1b4a72-5669-41fe-8d5b-106dc86f58bd") },
new Location() { Uuid= Guid.NewGuid(), Habitat = "Россия", IsSingleLocation = false, IdBreed = Guid.Parse("f8692bea-b7e6-4164-b564-a921f16c35c9") },
new Location() { Uuid= Guid.NewGuid(), Habitat = "Китай", IsSingleLocation = false, IdBreed = Guid.Parse("464bbdb8-39c0-4644-b9c0-3df1c484ea7e") },
new Location() { Uuid= Guid.NewGuid(), Habitat = "Хорватия", IsSingleLocation = true, IdBreed = 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 locations.Select(r => new LocationEntityDto()
{
Uuid = r.Uuid,
Habitat = r.Habitat,
IsSingleLocation = r.IsSingleLocation,
IdBreed = r.IdBreed,
});
})
.WithName("GetLocations")
.WithOpenApi();
app.MapGet("/{uuid}", (Guid uuid) =>
{
var location = locations.FirstOrDefault(r => r.Uuid == uuid);
if (location == null)
return Results.NotFound();
return Results.Json(new LocationEntityDto()
{
Uuid = location.Uuid,
Habitat = location.Habitat,
IsSingleLocation = location.IsSingleLocation,
IdBreed = location.IdBreed,
});
})
.WithName("GetLocationByGUID")
.WithOpenApi();
app.MapPost("/{habitat}/{isSingleLocation}/{idBreed}", (string? Habitat, bool IsSingleLocation, Guid IdBreed) =>
{
Guid NewGuid = Guid.NewGuid();
locations.Add(new Location() { Uuid = NewGuid, Habitat = (string)Habitat, IsSingleLocation = (bool)IsSingleLocation, IdBreed = (Guid)IdBreed });
var location = locations.FirstOrDefault(r => r.Uuid == NewGuid);
if (location == null)
return Results.NotFound();
return Results.Json(new LocationEntityDto()
{
Uuid = location.Uuid,
Habitat = location.Habitat,
IsSingleLocation = location.IsSingleLocation,
IdBreed = location.IdBreed,
});
})
.WithName("PostLocation")
.WithOpenApi();
app.MapPatch("/{uuid}/{habitat}/{isSingleLocation}/{idBreed}", (Guid uuid, string ?habitat, bool isSingleLocation, Guid idBreed) =>
{
var location = locations.FirstOrDefault(r => r.Uuid == uuid);
if (location == null)
return Results.NotFound();
if (habitat != ",") location.Habitat = habitat;
if (isSingleLocation != location.IsSingleLocation) location.IsSingleLocation = isSingleLocation;
if (idBreed != location.IdBreed) location.IdBreed = idBreed;
return Results.Json(new LocationEntityDto()
{
Uuid = location.Uuid,
Habitat = location.Habitat,
IsSingleLocation = location.IsSingleLocation,
IdBreed = location.IdBreed,
});
})
.WithName("UpdateLocation")
.WithOpenApi();
app.MapDelete("/{uuid}", (Guid uuid) =>
{
var location = locations.FirstOrDefault(r => r.Uuid == uuid);
if (location == null)
return Results.NotFound();
locations.Remove(location);
return Results.Json(new LocationEntityDto()
{
Uuid = location.Uuid,
Habitat = location.Habitat,
IsSingleLocation = location.IsSingleLocation,
IdBreed = location.IdBreed,
});
})
.WithName("DeleteLocation")
.WithOpenApi();
app.MapGet("/Locations/", async () =>
{
var httpClient = new HttpClient();
var secondWorkerResponse = await httpClient.GetStringAsync("http://worker-1:8080/");
return secondWorkerResponse.ToArray();
})
.WithName("GetBreeds")
.WithOpenApi();
app.Run();
public class Location
{
public Guid Uuid { get; set; }
public string Habitat { get; set; } = string.Empty;
public bool IsSingleLocation { get; set; }
public Guid IdBreed { get; set; }
}
public class LocationEntityDto : Location { }
public class Breeds
{
public Guid Uuid { get; set; }
public string Animals { get; set; } = string.Empty;
public string Breed { get; set; } = string.Empty;
}
public class BreedEntityDto : Breeds { }