distributed-computing/tasks/zinoveva-ad/lab_3/worker-2/Program.cs

132 lines
3.1 KiB
C#
Raw Permalink Normal View History

2023-12-16 16:41:40 +04:00
List<Agreement> agrs = new()
{
new Agreement() { Uuid= Guid.NewGuid(), Number = "242697-03К", IdRequest = Guid.Parse("7777fa5f-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,
Number = r.Number,
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,
Number = agr.Number,
IdRequest = agr.IdRequest,
});
})
.WithName("GetAgreementByGUID")
.WithOpenApi();
app.MapPost("/{number}/{idRequest}", (string? number, Guid idRequest) =>
{
Guid NewGuid = Guid.NewGuid();
agrs.Add(new Agreement() { Uuid = NewGuid, Number = (string)number, 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,
Number = agr.Number,
IdRequest = agr.IdRequest,
});
})
.WithName("PostAgreement")
.WithOpenApi();
app.MapPatch("/{uuid}/{number}/{idRequest}", (Guid uuid, string ?number, Guid idRequest) =>
{
var agr = agrs.FirstOrDefault(r => r.Uuid == uuid);
if (agr == null)
return Results.NotFound();
if (number != ",") agr.Number = number;
if (idRequest != agr.IdRequest) agr.IdRequest = idRequest;
return Results.Json(new AgreementEntityDto()
{
Uuid = agr.Uuid,
Number = agr.Number,
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,
Number = agr.Number,
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 Number { get; set; } = string.Empty;
public Guid IdRequest { get; set; }
}
public class AgreementEntityDto : Agreement { }
public class Request
{
public Guid Uuid { get; set; }
public string Title { get; set; } = string.Empty;
public string Employee { get; set; } = string.Empty;
public DateTime DateDocument { get; set; }
public bool IsAgreed { get; set; }
}
public class RequestEntityDto : Request { }