distributed-computing/tasks/plaksina-av/lab_3/worker-1/Program.cs

119 lines
3.5 KiB
C#
Raw Normal View History

2023-12-15 22:14:31 +04:00
List<Request> requests = new()
{
new Request() { Uuid= Guid.Parse("81a130d2-502f-4cf1-a376-63edeb000e9f"), NameDepartament = "Информационные системы", Manager = "Романов Антон Алексеевич", Faculty = "ФИСТ"},
new Request() { Uuid= Guid.Parse("65a9aed2-218a-468e-92b9-99f6a6a34543"), NameDepartament = "Прикладная математика и информатика", Manager = "Кувайскова Юлия Евгеньевна", Faculty = "ФИСТ"},
};
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 requests.Select(r => new RequestEntityDto()
{
Uuid = r.Uuid,
NameDepartament = r.NameDepartament,
Manager = r.Manager,
Faculty = r.Faculty,
});
})
.WithName("GetRequests")
.WithOpenApi();
app.MapGet("/{uuid}", (Guid uuid) =>
{
var request = requests.FirstOrDefault(r => r.Uuid == uuid);
if (request == null)
return Results.NotFound();
return Results.Json(new RequestEntityDto()
{
Uuid = request.Uuid,
NameDepartament = request.NameDepartament,
Manager = request.Manager,
Faculty = request.Faculty,
});
})
.WithName("GetRequestByGUID")
.WithOpenApi();
app.MapPost("/{nameDepartament}/{manager}/{faculty}", (string nameDepartament, string manager, string faculty) =>
{
Guid NewGuid = Guid.NewGuid();
requests.Add(new Request() { Uuid = NewGuid, NameDepartament = (string)nameDepartament, Manager = (string)manager, Faculty = (string)faculty});
var request = requests.FirstOrDefault(r => r.Uuid == NewGuid);
if (request == null)
return Results.NotFound();
return Results.Json(new RequestEntityDto()
{
Uuid = request.Uuid,
NameDepartament = request.NameDepartament,
Manager = request.Manager,
Faculty = request.Faculty,
});
})
.WithName("PostRequest")
.WithOpenApi();
app.MapPatch("/{uuid}/{title}/{sourceOfFunds}/{sum}/{isCompleted}", (Guid uuid, string ?nameDepartament, string ?manager, string ?faculty) =>
{
var request = requests.FirstOrDefault(r => r.Uuid == uuid);
if (request == null)
return Results.NotFound();
if (nameDepartament != null) request.NameDepartament = nameDepartament;
if (manager != null) request.Manager = manager;
if (faculty != null) request.Faculty = faculty;
return Results.Json(new RequestEntityDto()
{
Uuid = request.Uuid,
NameDepartament = request.NameDepartament,
Manager = request.Manager,
Faculty = request.Faculty,
});
})
.WithName("UpdateRequest")
.WithOpenApi();
app.MapDelete("/{uuid}", (Guid uuid) =>
{
var request = requests.FirstOrDefault(r => r.Uuid == uuid);
if (request == null)
return Results.NotFound();
requests.Remove(request);
return Results.Json(new RequestEntityDto()
{
Uuid = request.Uuid,
NameDepartament = request.NameDepartament,
Manager = request.Manager,
Faculty = request.Faculty,
});
})
.WithName("DeleteRequestByGUID")
.WithOpenApi();
app.Run();
public class Request
{
public Guid Uuid { get; set; }
public string NameDepartament { get; set; } = string.Empty;
public string Manager { get; set; } = string.Empty;
public string Faculty { get; set; } = string.Empty;
}
public class RequestEntityDto : Request { }