distributed-computing/tasks/savitskiy-al/lab_3/worker-1/Program.cs
2023-12-15 14:17:16 +04:00

131 lines
3.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.AspNetCore.Mvc;
List<AeroplaneDal> aeroplanes = new()
{
new AeroplaneDal() { AeroplaneId = Guid.NewGuid(), Name = "Boeing 747", Weght = 41140, Lenght = 40, ProductionDate = new DateTime(2010, 4, 15)},
new AeroplaneDal() { AeroplaneId = Guid.NewGuid(), Name = "Ту-154", Weght = 55300, Lenght = 48, ProductionDate = new DateTime(2005, 3, 20)},
new AeroplaneDal() { AeroplaneId = Guid.NewGuid(), Name = "Ил-86", Weght = 117500, Lenght = 60, ProductionDate = new DateTime(2005, 9, 7)},
};
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("/Aeroplanes/", () =>
{
var result = aeroplanes.Select(r => new worker_1.Models.Aeroplane.GetList.AeroplaneResult()
{
AeroplaneId = r.AeroplaneId,
Name = r.Name,
Lenght = r.Lenght,
ProductionDate = r.ProductionDate,
Weght = r.Weght,
}).ToArray();
return Results.Ok(result);
})
.WithName("List")
.WithOpenApi();
app.MapGet("/Aeroplanes/{aeroplaneId}", (Guid aeroplaneId) =>
{
var aeroplane = aeroplanes.FirstOrDefault(r => r.AeroplaneId == aeroplaneId);
if (aeroplane is null)
{
return Results.NotFound($"Не найдена самолет {aeroplaneId}");
}
return Results.Json(new worker_1.Models.Aeroplane.Get.AeroplaneResult()
{
AeroplaneId = aeroplane.AeroplaneId,
Name = aeroplane.Name,
Lenght = aeroplane.Lenght,
ProductionDate = aeroplane.ProductionDate,
Weght = aeroplane.Weght,
});
})
.WithName("Get")
.WithOpenApi();
app.MapPost("/Aeroplanes/", ([FromBody] worker_1.Models.Aeroplane.Create.AeroplaneForm request) =>
{
Guid aeroplaneId = Guid.NewGuid();
aeroplanes.Add(new AeroplaneDal()
{
AeroplaneId = aeroplaneId,
Lenght = request.Lenght,
ProductionDate = request.ProductionDate,
Weght = request.Weght,
Name = request.Name,
});
return Results.Ok(aeroplaneId);
})
.WithName("Create")
.WithOpenApi();
app.MapPatch("/Aeroplanes/{aeroplaneId}", (Guid aeroplaneId, [FromBody] worker_1.Models.Aeroplane.Create.AeroplaneForm request) =>
{
var aeroplane = aeroplanes.FirstOrDefault(r => r.AeroplaneId == aeroplaneId);
if (aeroplane is null)
{
return Results.NotFound($"Не найден самолет {aeroplaneId}");
}
if (!string.IsNullOrEmpty(request.Name))
{
aeroplane.Name = request.Name;
}
aeroplane.ProductionDate = request.ProductionDate;
aeroplane.Lenght = request.Lenght;
aeroplane.Weght = request.Weght;
return Results.Ok(aeroplane);
})
.WithName("Update")
.WithOpenApi();
app.MapDelete("/Aeroplanes/{aeroplaneId}", (Guid aeroplaneId) =>
{
var aeroplane = aeroplanes.FirstOrDefault(r => r.AeroplaneId == aeroplaneId);
if (aeroplane is null)
{
return Results.NotFound($"Не найден самолет {aeroplaneId}");
}
aeroplanes.Remove(aeroplane);
return Results.Ok(aeroplaneId);
})
.WithName("Delete")
.WithOpenApi();
app.Run();
public class AeroplaneDal
{
public Guid AeroplaneId { get; set; }
public string Name { get; set; }
public DateTime ProductionDate { get; set; }
public int Weght { get; set; }
public int Lenght { get; set; }
}