distributed-computing/tasks/ostrovskaya-sf/lab_3/worker-2/Program.cs

133 lines
3.2 KiB
C#
Raw Normal View History

2023-12-15 02:50:10 +04:00
using System.Diagnostics.Contracts;
List<Contract> agrs = new()
{
new Contract() { Uuid= Guid.NewGuid(), Number = "Договор №655/55 от 01.07.2023", IdRequest = Guid.Parse("a1b5c571-a859-4bb8-9fd2-7431ddd6c464") }
};
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 ContractEntityDto()
{
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 ContractEntityDto()
{
Uuid = agr.Uuid,
Number = agr.Number,
IdRequest = agr.IdRequest,
});
})
.WithName("GetAgreementByGUID")
.WithOpenApi();
2023-12-16 14:54:30 +04:00
app.MapPost("/{number}/{idRequest}", (string? number, Guid idRequest) =>
2023-12-15 02:50:10 +04:00
{
Guid NewGuid = Guid.NewGuid();
agrs.Add(new Contract() { 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 ContractEntityDto()
{
Uuid = agr.Uuid,
Number = agr.Number,
IdRequest = agr.IdRequest,
});
})
.WithName("PostAgreement")
.WithOpenApi();
2023-12-16 14:54:30 +04:00
app.MapPatch("/{uuid}/{number}/{idRequest}", (Guid uuid, string? number, Guid idRequest) =>
2023-12-15 02:50:10 +04:00
{
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 ContractEntityDto()
{
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 ContractEntityDto()
{
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 Contract
{
public Guid Uuid { get; set; }
public string Number { get; set; } = string.Empty;
public Guid IdRequest { get; set; }
}
public class ContractEntityDto : Contract { }
public class Request
{
public Guid Uuid { get; set; }
public string Title { get; set; } = string.Empty;
public string Student { get; set; } = string.Empty;
public DateTime DateAgreement { get; set; }
public bool AgreementValid { get; set; }
}
public class RequestEntityDto : Request { }