Files
raven/server/AyaNova/Controllers/ScheduleController.cs
2021-09-14 19:54:57 +00:00

126 lines
4.8 KiB
C#

using System.Threading.Tasks;
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging;
using AyaNova.Models;
using AyaNova.Api.ControllerHelpers;
using AyaNova.Biz;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Collections.Generic;
namespace AyaNova.Api.Controllers
{
[ApiController]
[ApiVersion("8.0")]
[Route("api/v{version:apiVersion}/schedule")]
[Produces("application/json")]
[Authorize]
public class ScheduleController : ControllerBase
{
private readonly AyContext ct;
private readonly ILogger<ScheduleController> log;
private readonly ApiServerState serverState;
/// <summary>
/// ctor
/// </summary>
/// <param name="dbcontext"></param>
/// <param name="logger"></param>
/// <param name="apiServerState"></param>
public ScheduleController(AyContext dbcontext, ILogger<ScheduleController> logger, ApiServerState apiServerState)
{
ct = dbcontext;
log = logger;
serverState = apiServerState;
}
/// <summary>
/// Get personal schedule for parameters specified
/// </summary>
/// <param name="p">Personal schedule parameters</param>
/// <param name="apiVersion">From route path</param>
/// <returns></returns>
[HttpPost("personal")]
public async Task<IActionResult> PostPersonalSchedule([FromBody] PersonalScheduleParams p, ApiVersion apiVersion)
{
if (!serverState.IsOpen)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
if (!ModelState.IsValid)
return BadRequest(new ApiErrorResponse(ModelState));
List<PersonalScheduleListItem> r = new List<PersonalScheduleListItem>();
var UserId = UserIdFromContext.Id(HttpContext.Items);
var UType = UserTypeFromContext.Type(HttpContext.Items);
if (UType == UserType.Service || UType == UserType.ServiceContractor)
{
//trying ad-hoc join https://entityframeworkcore.com/querying-data-joining
var w=await ct.WorkOrderItemScheduledUser.Join(ct.WorkOrderItem,x=>x.WorkOrderItemId==)
// var w = await ct.WorkOrder.AsSplitQuery().AsNoTracking()
// .Include(s => s.States)
// .Include(w => w.Items.OrderBy(item => item.Sequence))
// .ThenInclude(wi => wi.Expenses)
// .Include(w => w.Items)
// .ThenInclude(wi => wi.Labors)
// .Include(w => w.Items)
// .ThenInclude(wi => wi.Loans)
// .Include(w => w.Items)
// .ThenInclude(wi => wi.Parts)
// .Include(w => w.Items)
// .ThenInclude(wi => wi.PartRequests)
// .Include(w => w.Items)
// .ThenInclude(wi => wi.ScheduledUsers)
// .Include(w => w.Items)
// .ThenInclude(wi => wi.Tasks.OrderBy(t => t.Sequence))
// .Include(w => w.Items)
// .ThenInclude(wi => wi.Travels)
// .Include(w => w.Items)
// .ThenInclude(wi => wi.Units)
// .Include(w => w.Items)
// .ThenInclude(wi => wi.OutsideServices)
// .SingleOrDefaultAsync(z => z.Id == UserId);
}
return Ok(r);
}
public enum PersonalScheduleWorkOrderColorSource : int
{
None = 0,
WorkOrderStatus = 2,
WorkOrderItemStatus = 3,
WorkOrderItemPriority = 4
}
public class PersonalScheduleParams
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
public PersonalScheduleWorkOrderColorSource ColorSource { get; set; }
}
public class PersonalScheduleListItem
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
public string Name { get; set; }
public string Color { get; set; }
public AyaType Type { get; set; }
public long Id { get; set; }
}
//------------
}//eoc
}//eons