distributed-computing/tasks/klementeva-ja/lab_3/worker-1/Program.cs
2023-12-14 13:53:47 +04:00

124 lines
3.8 KiB
C#

List<Request> requests = new()
{
new Request() { Uuid= Guid.Parse("7184fa5f-b786-4478-863a-99cc000eb752"), Title = "Расход на внедрение системы менеджмента качества", SourceOfFunds = "Шуреков", Sum = 100000, IsCompleted = false },
new Request() { Uuid= Guid.Parse("55a9aed1-218a-468e-92b9-99f6a6a34543"), Title = "Расход на сопровождение проекта", SourceOfFunds = "Клементьев", Sum = 25000, IsCompleted = true },
};
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,
Title = r.Title,
Sum = r.Sum,
SourceOfFunds = r.SourceOfFunds,
IsCompleted = r.IsCompleted,
});
})
.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,
Title = request.Title,
Sum = request.Sum,
SourceOfFunds = request.SourceOfFunds,
IsCompleted = request.IsCompleted,
});
})
.WithName("GetRequestByGUID")
.WithOpenApi();
app.MapPost("/{title}/{sourceOfFunds}/{sum}/{isCompleted}", (string title, string sourceOfFunds, decimal sum, bool isCompleted) =>
{
Guid NewGuid = Guid.NewGuid();
requests.Add(new Request() { Uuid = NewGuid, Title = (string)title, SourceOfFunds = (string)sourceOfFunds, Sum = (decimal)sum, IsCompleted = (bool)isCompleted});
var request = requests.FirstOrDefault(r => r.Uuid == NewGuid);
if (request == null)
return Results.NotFound();
return Results.Json(new RequestEntityDto()
{
Uuid = request.Uuid,
Title = request.Title,
Sum = request.Sum,
SourceOfFunds = request.SourceOfFunds,
IsCompleted = request.IsCompleted,
});
})
.WithName("PostRequest")
.WithOpenApi();
app.MapPatch("/{uuid}/{title}/{sourceOfFunds}/{sum}/{isCompleted}", (Guid uuid, string ?title, string ?sourceOfFunds, decimal sum, bool isCompleted) =>
{
var request = requests.FirstOrDefault(r => r.Uuid == uuid);
if (request == null)
return Results.NotFound();
if (title != null) request.Title = title;
if (sourceOfFunds != ",") request.SourceOfFunds = sourceOfFunds;
if (sum != request.Sum && sum != 0) request.Sum = sum;
if (isCompleted != request.IsCompleted) request.IsCompleted = isCompleted;
return Results.Json(new RequestEntityDto()
{
Uuid = request.Uuid,
Title = request.Title,
Sum = request.Sum,
SourceOfFunds = request.SourceOfFunds,
IsCompleted = request.IsCompleted,
});
})
.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,
Title = request.Title,
Sum = request.Sum,
SourceOfFunds = request.SourceOfFunds,
IsCompleted = request.IsCompleted,
});
})
.WithName("DeleteRequestByGUID")
.WithOpenApi();
app.Run();
public class Request
{
public Guid Uuid { get; set; }
public string Title { get; set; } = string.Empty;
public string SourceOfFunds { get; set; } = string.Empty;
public decimal Sum { get; set; } = 0;
public bool IsCompleted { get; set; }
}
public class RequestEntityDto : Request { }