distributed-computing/tasks/ostrovskaya-sf/lab_3/worker-1/Program.cs
Софья Островская 8badb568b7 check
2023-12-15 02:50:10 +04:00

124 lines
4.1 KiB
C#

List<Request> requests = new()
{
new Request() { Uuid= Guid.Parse("a1b5c571-a859-4bb8-9fd2-7431ddd6c464"), Title = "Дополнительное соглашение на расценку", Student = "Иванов Иван Иванович", DateAgreement = new DateTime(2023, 7, 01), AgreementValid = true },
new Request() { Uuid= Guid.Parse("53d08f8c-19f5-4028-8e92-520470c8f747"), Title = "Дополнительное соглашение на продление срока проживания", Student = "Петров Петр Петрович", DateAgreement = new DateTime(2023, 5, 01), AgreementValid = false },
};
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,
DateAgreement = r.DateAgreement,
Student = r.Student,
AgreementValid = r.AgreementValid,
});
})
.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,
DateAgreement = request.DateAgreement,
Student = request.Student,
AgreementValid = request.AgreementValid,
});
})
.WithName("GetRequestByGUID")
.WithOpenApi();
app.MapPost("/{title}/{Student}/{DateAgreement}/{AgreementValid}", (string title, string Student, DateTime DateAgreement, bool AgreementValid) =>
{
Guid NewGuid = Guid.NewGuid();
requests.Add(new Request() { Uuid = NewGuid, Title = (string)title, Student = (string)Student, DateAgreement = (DateTime)DateAgreement, AgreementValid = (bool)AgreementValid });
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,
DateAgreement = request.DateAgreement,
Student = request.Student,
AgreementValid = request.AgreementValid,
});
})
.WithName("PostRequest")
.WithOpenApi();
app.MapPatch("/{uuid}/{title}/{Student}/{DateAgreement}/{AgreementValid}", (Guid uuid, string? title, string? Student, DateTime DateAgreement, bool AgreementValid) =>
{
var request = requests.FirstOrDefault(r => r.Uuid == uuid);
if (request == null)
return Results.NotFound();
if (title != null) request.Title = title;
if (Student != ",") request.Student = Student;
if (DateAgreement != request.DateAgreement) request.DateAgreement = DateAgreement;
if (AgreementValid != request.AgreementValid) request.AgreementValid = AgreementValid;
return Results.Json(new RequestEntityDto()
{
Uuid = request.Uuid,
Title = request.Title,
DateAgreement = request.DateAgreement,
Student = request.Student,
AgreementValid = request.AgreementValid,
});
})
.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,
DateAgreement = request.DateAgreement,
Student = request.Student,
AgreementValid = request.AgreementValid,
});
})
.WithName("DeleteRequestByGUID")
.WithOpenApi();
app.Run();
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 { }