using Application.Domain.Entities; using Application.Models; using Infrastructure.Application; using Infrastructure.Data; using Infrastructure.Extensions; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System; using System.Linq; namespace IoT.Shared.Areas.Admin.Controlls { [Authorize] [Area(nameof(Admin))] public class IoTTimerCommandController : SharedController, EditIoTTimerCommandModel, EditIoTTimerCommandModel> { private readonly IRepository _nodeRepo; private readonly AjaxController _ajax; public IoTTimerCommandController(IRepository nodeRepo, IRepository repo, AjaxController ajax, IServiceProvider sp) : base(repo, sp) { this._nodeRepo = nodeRepo; this._ajax = ajax; } public override IQueryable Include(IQueryable query) { return query.Include(o => o.IoTTimer).ThenInclude(o => o.Node) .Include(o => o.Command).ThenInclude(o => o.Device); } public override IQueryable Query(PagedListModel model, IQueryable query) { return query.Where(o => o.IoTTimer.NodeId != null); } public override void ToEditModel(IoTTimerCommand entity, EditIoTTimerCommandModel model) { this.ToDisplayModel(entity, model); ViewData.SelectList(o => model.NodeId, () => this._ajax.GetNodeSelectList(model.IoTTimerId)); ViewData.SelectList(o => model.IoTTimerId, () => this._ajax.GetIoTTimerSelectList(model.NodeId.Value, model.IoTTimerId), model.NodeId.HasValue); ViewData.SelectList(o => model.CommandId, () => this._ajax.GetCommandSelectList(model.NodeId.Value, model.CommandId), model.NodeId.HasValue); } public override void ToDisplayModel(IoTTimerCommand entity, EditIoTTimerCommandModel model) { if (entity != null) { model.NodeId = entity.IoTTimer.NodeId; ViewData.Add(model.NodeId, entity.IoTTimer.Node.Name); ViewData.Add(model.IoTTimerId, entity.IoTTimer.Name); ViewData.Add(model.CommandId, entity.Command.Name); } } public override string GetNodeNumber(EditIoTTimerCommandModel model) { return this._nodeRepo.ReadOnlyTable().Where(o => o.Id == model.NodeId).Select(o => o.Number).FirstOrDefault(); } } }