DurnyklyYol/DurnyklyYol.Blazor.Server/Controllers/CargoController.cs

87 lines
3.0 KiB
C#

using DevExpress.ExpressApp.Core;
using DevExpress.ExpressApp.Security;
using DevExpress.ExpressApp.WebApi.Services;
using DurnyklyYol.Blazor.Server.DTO;
using DurnyklyYol.Module.BusinessObjects;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace DurnyklyYol.Blazor.Server.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class CargoController : ControllerBase
{
private readonly INonSecuredObjectSpaceFactory _nonSecuredObjectSpaceFactory;
public CargoController(INonSecuredObjectSpaceFactory nonSecuredObjectSpaceFactory) => _nonSecuredObjectSpaceFactory = nonSecuredObjectSpaceFactory;
[HttpGet("Route/{cargoId}")]
public IActionResult GetRoute(Guid cargoId)
{
try
{
using var objectSpace = _nonSecuredObjectSpaceFactory.CreateNonSecuredObjectSpace(typeof(Cargo));
var cargo = objectSpace.GetObjectByKey<Cargo>(cargoId);
if (cargo == null)
{
return NotFound();
}
var route = objectSpace.GetObjectsQuery<Module.BusinessObjects.Route>()
.Where(wh => wh.Oid == cargo.Route.Oid)
.Select(sl => new
{
StartName = sl.StartPoint.Name,
Startpoint = sl.StartPoint.Point,
DestName = sl.DestinationPoint.Name,
Destpoint = sl.DestinationPoint.Point,
Points = sl.Points.OrderBy(ob => ob.Order).Select(p => p.Point)
})
.FirstOrDefault();
if (route == null)
{
return NotFound();
}
var points = route.Points.Select(p => new PointDto
{
Name = p.Name,
Lat = p.Latitude,
Long = p.Longitude,
IsCurrent = cargo.CurrentPoint.Oid == p.Oid,
}).ToList();
points.Insert(0, new PointDto
{
Name = route.StartName,
Lat = route.Startpoint?.Latitude ?? 0,
Long = route.Startpoint?.Longitude ?? 0,
IsCurrent = cargo.CurrentPoint.Oid == route.Startpoint?.Oid,
DateAt = cargo.StartedAt
});
points.Add(new PointDto
{
Name = route.DestName,
Lat = route.Destpoint?.Latitude ?? 0,
Long = route.Destpoint?.Longitude ?? 0,
IsCurrent = cargo.CurrentPoint.Oid == route.Destpoint?.Oid,
DateAt = cargo.ArrivedAt
});
return Ok(points);
}
catch (Exception ex) {
return StatusCode(500, ex.Message);
}
}
}
}