You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/IoT.Shared/Areas/IoTCenter/Controlls/SceneCommandController.cs

63 lines
2.8 KiB

using Infrastructure.Application;
using Infrastructure.Data;
using Infrastructure.Extensions;
using IoT.Shared.Application.Domain.Entities;
using IoT.Shared.Application.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
namespace IoT.Shared.Areas.IoTCenter.Controlls
{
public class SceneCommandController : SharedController<IoTSceneIoTCommand, EditSceneCommandModel>
{
private readonly IRepository<IoTGateway> _nodeRepo;
private readonly AjaxBaseController _ajax;
public SceneCommandController(IRepository<IoTGateway> nodeRepo, IRepository<IoTSceneIoTCommand> repo, AjaxBaseController ajax, IServiceProvider sp) : base(repo, sp)
{
this._nodeRepo = nodeRepo;
this._ajax = ajax;
}
public override IQueryable<IoTSceneIoTCommand> Include(IQueryable<IoTSceneIoTCommand> query)
{
return query.Include(o => o.Scene).ThenInclude(o => o.Node)
.Include(o => o.Command).ThenInclude(o => o.Device);
}
public override IQueryable<IoTSceneIoTCommand> Query(PagedListModel<EditSceneCommandModel> model, IQueryable<IoTSceneIoTCommand> query)
{
return query.Where(o => o.Scene.NodeId != null)
.WhereIf(model.Query.NodeId.HasValue, o => o.Scene.NodeId == model.Query.NodeId.Value)
.WhereIf(model.Query.SceneId.HasValue, o => o.SceneId == model.Query.SceneId.Value)
.WhereIf(model.Query.CommandId.HasValue, o => o.CommandId == model.Query.CommandId.Value)
.OrderBy(o => o.SceneId);
}
public override void ToDisplayModel(IoTSceneIoTCommand entity, EditSceneCommandModel model)
{
if (entity != null)
{
model.NodeId = entity.Scene.NodeId;
ViewData.Add(model.SceneId, entity.Scene.Name);
ViewData.Add(model.NodeId, entity.Scene.Node.Name);
ViewData.Add(model.CommandId, entity.Command.Name);
}
}
public override void ToEditModel(IoTSceneIoTCommand entity, EditSceneCommandModel model)
{
this.ToDisplayModel(entity, model);
ViewData.SelectList(o => model.NodeId, () => this._ajax.GetNodeSelectList(model.SceneId));
ViewData.SelectList(o => model.SceneId, () => this._ajax.GetSceneSelectList(model.NodeId.Value, model.SceneId), model.NodeId.HasValue);
ViewData.SelectList(o => model.CommandId, () => this._ajax.GetCommandSelectList(model.NodeId.Value, model.CommandId), model.NodeId.HasValue);
}
public override string GetNodeNumber(EditSceneCommandModel model)
{
return this._nodeRepo.ReadOnlyTable().Where(o => o.Id == model.NodeId).Select(o => o.Number).FirstOrDefault();
}
}
}