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/IoT.Shared/Controllers/AjaxController.cs

112 lines
3.6 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using Application.Domain.Entities;
using Infrastructure.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace IoTShared.Controllers
{
[Area("Admin")]
public class AjaxController
{
private readonly ILogger<AjaxController> _logger;
private readonly IRepository<Node> _nodeRepo;
private readonly IRepository<Product> _productRepo;
private readonly IRepository<Scene> _sceneRepo;
private readonly IRepository<Device> _deviceRepo;
private readonly IRepository<Api> _apiRepo;
public AjaxController(ILogger<AjaxController> logger,
IRepository<Node> nodeRepo,
IRepository<Product> productRepo,
IRepository<Scene> sceneRepo,
IRepository<Device> deviceRepo,
IRepository<Api> apiRepo)
{
this._logger = logger;
this._nodeRepo = nodeRepo;
this._productRepo = productRepo;
this._sceneRepo = sceneRepo;
this._deviceRepo = deviceRepo;
this._apiRepo = apiRepo;
}
#region Node
public SelectList GetNodeSelectList(Guid? selected = null)
{
var list = this._nodeRepo.ReadOnlyTable()
.Select(o => new { o.Id, Name = $"{o.Name}/{o.Number}" })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
#endregion Node
#region Scene
public SelectList GetSceneSelectList(Guid parentId, Guid? selected = null)
{
var list = this._sceneRepo.ReadOnlyTable()
.Where(o => o.NodeId == parentId)
.Select(o => new { o.Id, o.Name })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
public JsonResult GetSceneJson(Guid parentId, Guid? selected)
{
return new JsonResult(this.GetSceneSelectList(parentId, selected));
}
#endregion Scene
#region Device
public SelectList GetDeviceSelectList(Guid parentId, Guid? selected = null)
{
var list = this._deviceRepo.ReadOnlyTable()
.Where(o => o.NodeId == parentId)
.Select(o => new { o.Id, Name = $"{o.DisplayName ?? o.Name}/{o.Number}" })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
public JsonResult GetDeviceJson(Guid parentId, Guid? selected)
{
return new JsonResult(this.GetDeviceSelectList(parentId, selected));
}
#endregion Device
#region Api
public SelectList GetProductSelectList(Guid? selected = null)
{
var list = this._productRepo.ReadOnlyTable()
.Select(o => new { o.Id, o.Name })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
public SelectList GetApiSelectList(Guid parentId, Guid? selected = null)
{
var list = this._deviceRepo.ReadOnlyTable()
.Where(o => o.Id == parentId).SelectMany(o => o.Product.Apis)
.Select(o => new { o.Id, o.Name })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
public JsonResult GetApiJson(Guid parentId, Guid? selected)
{
return new JsonResult(this.GetApiSelectList(parentId, selected));
}
#endregion Api
}
}