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

144 lines
3.9 KiB
C#
Raw Normal View History

2023-12-15 22:14:31 +04:00
List<Agreement> agrs = new()
{
new Agreement() { Uuid= Guid.NewGuid(), Specialism = "Прикладная информатика", CountEGE = 3, DVI = false, IdRequest = Guid.Parse("81a130d2-502f-4cf1-a376-63edeb000e9f") }
};
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,
Specialism = r.Specialism,
CountEGE = r.CountEGE,
DVI = r.DVI,
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,
Specialism = agr.Specialism,
CountEGE = agr.CountEGE,
DVI = agr.DVI,
IdRequest = agr.IdRequest,
});
})
.WithName("GetAgreementByGUID")
.WithOpenApi();
app.MapPost("/{specialism}/{countEGE}/{DVI}/{idRequest}", (string? specialism, decimal countEGE, bool DVI, Guid idRequest) =>
{
Guid NewGuid = Guid.NewGuid();
agrs.Add(new Agreement() { Uuid = NewGuid, Specialism = (string)specialism, CountEGE = (decimal)countEGE, DVI = (bool)DVI, 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,
Specialism = agr.Specialism,
CountEGE = agr.CountEGE,
DVI = agr.DVI,
IdRequest = agr.IdRequest,
});
})
.WithName("PostAgreement")
.WithOpenApi();
app.MapPatch("/{uuid}/{specialism}/{countEGE}/{DVI}/{idRequest}", (Guid uuid, string? specialism, decimal countEGE, bool DVI, Guid idRequest) =>
{
var agr = agrs.FirstOrDefault(r => r.Uuid == uuid);
if (agr == null)
return Results.NotFound();
if (specialism != ",") agr.Specialism = specialism;
if (countEGE != agr.CountEGE && countEGE != 0) agr.CountEGE = countEGE;
if (DVI !=agr.DVI) agr.DVI = DVI;
if (idRequest != agr.IdRequest) agr.IdRequest = idRequest;
return Results.Json(new AgreementEntityDto()
{
Uuid = agr.Uuid,
Specialism = agr.Specialism,
CountEGE = agr.CountEGE,
DVI = agr.DVI,
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,
Specialism = agr.Specialism,
CountEGE = agr.CountEGE,
DVI = agr.DVI,
IdRequest = agr.IdRequest,
});
})
.WithName("DeleteAgreement")
.WithOpenApi();
app.MapGet("/Requests/", async () =>
{
var httpClient = new HttpClient();
var secondWorkerResponse = await httpClient.GetStringAsync("http://worker-1:8085/");
return secondWorkerResponse;
})
.WithName("GetRequests")
.WithOpenApi();
app.Run();
public class Agreement
{
public Guid Uuid { get; set; }
public string Specialism { get; set; } = string.Empty;
public decimal CountEGE { get; set; } = 0;
public Guid IdRequest { get; set; }
public bool DVI { get; set; }
}
public class AgreementEntityDto : Agreement { }
public class Request
{
public Guid Uuid { get; set; }
public string NameDepartament { get; set; } = string.Empty;
public string Manager { get; set; } = string.Empty;
public Guid IdRequest { get; set; }
public string Faculty { get; set; } = string.Empty;
}
public class RequestEntityDto : Request { }