distributed-computing/tasks/zakirova-ar/lab_3/worker-2/Program.cs

145 lines
3.9 KiB
C#
Raw Normal View History

2023-12-18 10:09:55 +04:00
List<Agreement> agrs = new()
{
new Agreement() { Uuid= Guid.NewGuid(), NumberOfField = "Пшеница_1", Date = new DateOnly(), Sum = 50000, IdRequest = Guid.Parse("7184fa5f-b786-4478-863a-99cc000eb752") }
};
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 agrs.Select(r => new AgreementEntityDto()
{
Uuid = r.Uuid,
NumberOfField = r.NumberOfField,
Sum = r.Sum,
Date = r.Date,
IdRequest = r.IdRequest,
});
})
.WithName("GetAgreements")
.WithOpenApi();
app.MapGet("/{uuid}", (Guid uuid) =>
{
var agr = agrs.FirstOrDefault(r => r.Uuid == uuid);
if (agr == null)
return Results.NotFound();
return Results.Json(new AgreementEntityDto()
{
Uuid = agr.Uuid,
NumberOfField = agr.NumberOfField,
Sum = agr.Sum,
Date = agr.Date,
IdRequest = agr.IdRequest,
});
})
.WithName("GetAgreementByGUID")
.WithOpenApi();
app.MapPost("/{numberOfField}/{date}/{sum}/{idRequest}", (string? numberOfField, DateOnly date, decimal sum, Guid idRequest) =>
{
Guid NewGuid = Guid.NewGuid();
agrs.Add(new Agreement() { Uuid = NewGuid, NumberOfField = (string)numberOfField, Date = (DateOnly)date, Sum = (decimal)sum, IdRequest = (Guid)idRequest });
var agr = agrs.FirstOrDefault(r => r.Uuid == NewGuid);
if (agr == null)
return Results.NotFound();
return Results.Json(new AgreementEntityDto()
{
Uuid = agr.Uuid,
NumberOfField = agr.NumberOfField,
Sum = agr.Sum,
Date = agr.Date,
IdRequest = agr.IdRequest,
});
})
.WithName("PostAgreement")
.WithOpenApi();
app.MapPatch("/{uuid}/{numberOfField}/{date}/{sum}/{idRequest}", (Guid uuid, string ?numberOfField, DateOnly date, decimal sum, Guid idRequest) =>
{
var agr = agrs.FirstOrDefault(r => r.Uuid == uuid);
if (agr == null)
return Results.NotFound();
if (numberOfField != ",") agr.NumberOfField = numberOfField;
if (date != null)agr.Date = date;
if (sum != agr.Sum && sum != 0) agr.Sum = sum;
if (idRequest != agr.IdRequest) agr.IdRequest = idRequest;
return Results.Json(new AgreementEntityDto()
{
Uuid = agr.Uuid,
NumberOfField = agr.NumberOfField,
Sum = agr.Sum,
Date = agr.Date,
IdRequest = agr.IdRequest,
});
})
.WithName("UpdateAgreement")
.WithOpenApi();
app.MapDelete("/{uuid}", (Guid uuid) =>
{
var agr = agrs.FirstOrDefault(r => r.Uuid == uuid);
if (agr == null)
return Results.NotFound();
agrs.Remove(agr);
return Results.Json(new AgreementEntityDto()
{
Uuid = agr.Uuid,
NumberOfField = agr.NumberOfField,
Sum = agr.Sum,
Date = agr.Date,
IdRequest = agr.IdRequest,
});
})
.WithName("DeleteAgreement")
.WithOpenApi();
app.MapGet("/Requests/", async () =>
{
var httpClient = new HttpClient();
var secondWorkerResponse = await httpClient.GetStringAsync("http://worker-1:8080/");
return secondWorkerResponse.ToArray();
})
.WithName("GetRequests")
.WithOpenApi();
app.Run();
public class Agreement
{
public Guid Uuid { get; set; }
public string NumberOfField { get; set; } = string.Empty;
public DateOnly Date { get; set; }
public Guid IdRequest { get; set; }
public decimal Sum { get; set; } = 0;
}
public class AgreementEntityDto : Agreement { }
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 { }