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/Admin/Controlls/SceneCommandController.cs

65 lines
2.6 KiB

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 SceneCommandController : SharedController<SceneCommand, PagedListModel<EditSceneCommandModel>, EditSceneCommandModel, EditSceneCommandModel>
{
private readonly IRepository<Node> _nodeRepo;
private readonly AjaxController _ajax;
public SceneCommandController(IRepository<Node> nodeRepo, IRepository<SceneCommand> repo, AjaxController ajax, IServiceProvider sp) : base(repo, sp)
{
this._nodeRepo = nodeRepo;
this._ajax = ajax;
}
public override IQueryable<SceneCommand> Include(IQueryable<SceneCommand> query)
{
return query.Include(o => o.Scene).ThenInclude(o => o.Node)
.Include(o => o.Command).ThenInclude(o => o.Device);
}
public override IQueryable<SceneCommand> Query(PagedListModel<EditSceneCommandModel> model, IQueryable<SceneCommand> query)
{
return query.Where(o => o.Scene.NodeId != null).OrderBy(o => o.SceneId);
}
public override void ToDisplayModel(SceneCommand 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(SceneCommand entity, EditSceneCommandModel model)
{
if (entity != null)
{
model.NodeId = entity.Scene.NodeId;
}
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();
}
}
}