142 lines
4.0 KiB
C#
142 lines
4.0 KiB
C#
|
using Microsoft.Extensions.DependencyInjection;
|
|||
|
using Microsoft.AspNetCore.Builder;
|
|||
|
using Microsoft.OpenApi.Models;
|
|||
|
|
|||
|
List<Сities> сities = new()
|
|||
|
{
|
|||
|
new Сities() { Uuid= Guid.NewGuid(), Population = "134r23", Name = "Moscow", IdCountrie = Guid.Parse("6a1b4a72-5669-41fe-8d5b-106dc86f58bd") },
|
|||
|
new Сities() { Uuid= Guid.NewGuid(), Population = "223322213", Name = "Paris", IdCountrie = Guid.Parse("f8692bea-b7e6-4164-b564-a921f16c35c9") },
|
|||
|
new Сities() { Uuid= Guid.NewGuid(), Population = "32123", Name = "New York", IdCountrie = Guid.Parse("464bbdb8-39c0-4644-b9c0-3df1c484ea7e") },
|
|||
|
new Сities() { Uuid= Guid.NewGuid(), Population = "312122", Name = "Sant Peterburg", IdCountrie = 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 сities.Select(r => new CityEntityDto()
|
|||
|
{
|
|||
|
Uuid = r.Uuid,
|
|||
|
Population = r.Population,
|
|||
|
Name = r.Name,
|
|||
|
IdCountrie = r.IdCountrie,
|
|||
|
});
|
|||
|
})
|
|||
|
.WithName("GetСities")
|
|||
|
.WithOpenApi();
|
|||
|
|
|||
|
app.MapGet("/{uuid}", (Guid uuid) =>
|
|||
|
{
|
|||
|
var city = сities.FirstOrDefault(r => r.Uuid == uuid);
|
|||
|
if (city == null)
|
|||
|
return Results.NotFound();
|
|||
|
return Results.Json(new CityEntityDto()
|
|||
|
{
|
|||
|
Uuid = city.Uuid,
|
|||
|
Population = city.Population,
|
|||
|
Name = city.Name,
|
|||
|
IdCountrie = city.IdCountrie,
|
|||
|
});
|
|||
|
})
|
|||
|
.WithName("GetCityByGUID")
|
|||
|
.WithOpenApi();
|
|||
|
|
|||
|
app.MapPost("/{population}/{name}/{IdCountrie}", (string? Population, string Name, Guid IdCountrie) =>
|
|||
|
{
|
|||
|
Guid NewGuid = Guid.NewGuid();
|
|||
|
сities.Add(new Сities() { Uuid = NewGuid, Population = (string)Population, Name = (string)Name, IdCountrie = (Guid)IdCountrie });
|
|||
|
|
|||
|
var city = сities.FirstOrDefault(r => r.Uuid == NewGuid);
|
|||
|
if (city == null)
|
|||
|
return Results.NotFound();
|
|||
|
return Results.Json(new CityEntityDto()
|
|||
|
{
|
|||
|
Uuid = city.Uuid,
|
|||
|
Population = city.Population,
|
|||
|
Name = city.Name,
|
|||
|
IdCountrie = city.IdCountrie,
|
|||
|
});
|
|||
|
})
|
|||
|
.WithName("PostCity")
|
|||
|
.WithOpenApi();
|
|||
|
|
|||
|
app.MapPatch("/{uuid}/{population}/{name}/{IdCountrie}", (Guid uuid, string ?population, string name, Guid IdCountrie) =>
|
|||
|
{
|
|||
|
var city = сities.FirstOrDefault(r => r.Uuid == uuid);
|
|||
|
if (city == null)
|
|||
|
return Results.NotFound();
|
|||
|
if (population != ",") city.Population = population;
|
|||
|
if (name != city.Name) city.Name = name;
|
|||
|
if (IdCountrie != city.IdCountrie) city.IdCountrie = IdCountrie;
|
|||
|
|
|||
|
return Results.Json(new CityEntityDto()
|
|||
|
{
|
|||
|
Uuid = city.Uuid,
|
|||
|
Population = city.Population,
|
|||
|
Name = city.Name,
|
|||
|
IdCountrie = city.IdCountrie,
|
|||
|
});
|
|||
|
})
|
|||
|
.WithName("UpdateCity")
|
|||
|
.WithOpenApi();
|
|||
|
|
|||
|
app.MapDelete("/{uuid}", (Guid uuid) =>
|
|||
|
{
|
|||
|
var city = сities.FirstOrDefault(r => r.Uuid == uuid);
|
|||
|
if (city == null)
|
|||
|
return Results.NotFound();
|
|||
|
сities.Remove(city);
|
|||
|
return Results.Json(new CityEntityDto()
|
|||
|
{
|
|||
|
Uuid = city.Uuid,
|
|||
|
Population = city.Population,
|
|||
|
Name = city.Name,
|
|||
|
IdCountrie = city.IdCountrie,
|
|||
|
});
|
|||
|
})
|
|||
|
.WithName("DeleteCity")
|
|||
|
.WithOpenApi();
|
|||
|
|
|||
|
app.MapGet("/Сities/", async () =>
|
|||
|
{
|
|||
|
var httpClient = new HttpClient();
|
|||
|
var secondWorkerResponse = await httpClient.GetStringAsync("http://worker-1:8080/");
|
|||
|
|
|||
|
return secondWorkerResponse.ToArray();
|
|||
|
})
|
|||
|
.WithName("GetCountries")
|
|||
|
.WithOpenApi();
|
|||
|
|
|||
|
app.Run();
|
|||
|
|
|||
|
public class Сities
|
|||
|
{
|
|||
|
public Guid Uuid { get; set; }
|
|||
|
public string Population { get; set; } = string.Empty;
|
|||
|
public string Name { get; set; } = string.Empty;
|
|||
|
public Guid IdCountrie { get; set; }
|
|||
|
}
|
|||
|
|
|||
|
public class CityEntityDto : Сities { }
|
|||
|
|
|||
|
public class Countries
|
|||
|
{
|
|||
|
public Guid Uuid { get; set; }
|
|||
|
public string Name { get; set; } = string.Empty;
|
|||
|
public string Area { get; set; } = string.Empty;
|
|||
|
}
|
|||
|
|
|||
|
public class CountrieEntityDto : Countries { }
|