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.
66 lines
2.6 KiB
66 lines
2.6 KiB
using Application.Domain.Entities;
|
|
using Application.Models;
|
|
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 DeviceController : SharedController<Device, SearchDeviceMode, EditDeviceModel, EditDeviceModel>
|
|
{
|
|
private readonly AjaxController _ajax;
|
|
|
|
public DeviceController(IRepository<Device> repo, AjaxController ajax, IServiceProvider sp) : base(repo, sp)
|
|
{
|
|
this._ajax = ajax;
|
|
}
|
|
|
|
public override IQueryable<Device> Include(IQueryable<Device> query)
|
|
{
|
|
return query.Include(o => o.Product).Include(o => o.Node);
|
|
}
|
|
|
|
public override IQueryable<Device> Query(SearchDeviceMode model, IQueryable<Device> query)
|
|
{
|
|
if (model == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
ViewData.SelectList(o => model.NodeId, () => this._ajax.GetNodeSelectList(model.NodeId));
|
|
ViewData.SelectList(o => model.CategoryId, () => this._ajax.GetCategorySelectList(model.CategoryId));
|
|
ViewData.SelectList(o => model.ProductId, () => this._ajax.GetProductSelectList(model.ProductId));
|
|
return query
|
|
.WhereIf(model.NodeId.HasValue, o => o.NodeId == model.NodeId.Value)
|
|
.WhereIf(model.CategoryId.HasValue, o => o.Product.CategoryId == model.CategoryId.Value)
|
|
.WhereIf(model.ProductId.HasValue, o => o.ProductId == model.ProductId.Value)
|
|
.WhereIf(!string.IsNullOrEmpty(model.Keyword), o => o.Name.Contains(model.Keyword));
|
|
}
|
|
|
|
public override void ToDisplayModel(Device entity, EditDeviceModel model)
|
|
{
|
|
ViewData.Add(model.NodeId, entity.Node.Name);
|
|
ViewData.Add(model.ProductId, entity.Product.Name);
|
|
}
|
|
|
|
public override void ToEditModel(Device entity, EditDeviceModel model)
|
|
{
|
|
if (entity != null)
|
|
{
|
|
this.ToDisplayModel(entity, model);
|
|
}
|
|
ViewData.SelectList(o => model.NodeId, () => this._ajax.GetNodeSelectList(model.NodeId));
|
|
ViewData.SelectList(o => model.ProductId, () => this._ajax.GetProductSelectList(model.ProductId));
|
|
}
|
|
|
|
public override string GetNodeNumber(EditDeviceModel model)
|
|
{
|
|
return this._repo.ReadOnlyTable().Where(o => o.NodeId == model.NodeId).Select(o => o.Node.Number).FirstOrDefault();
|
|
}
|
|
}
|
|
} |