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.
96 lines
3.5 KiB
96 lines
3.5 KiB
using Application.Domain.Entities;
|
|
using Application.Models;
|
|
using Application.Services;
|
|
using Infrastructure.Application;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using Infrastructure.Web.Mvc;
|
|
using IoTShared;
|
|
using IoTShared.Controllers;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace IoTCenter.Areas.Admin.Controllers
|
|
{
|
|
[Authorize]
|
|
[Area(nameof(Admin))]
|
|
public class SenceController : CrudController<Sence, PagedListModel<DisplaySenceModel>, DisplaySenceModel, EditSenceModel>
|
|
{
|
|
private readonly IRepository<Node> _nodeRepo;
|
|
private readonly IRepository<Sence> _repo;
|
|
private readonly AjaxController _ajax;
|
|
private readonly IHubContext<PageHub> _pageHubContext;
|
|
|
|
public SenceController(IRepository<Node> nodeRepo, IRepository<Sence> repo, AjaxController ajax, IHubContext<PageHub> pageHubContext) : base(repo)
|
|
{
|
|
this._repo = repo;
|
|
this._nodeRepo = nodeRepo;
|
|
this._ajax = ajax;
|
|
this._pageHubContext = pageHubContext;
|
|
}
|
|
|
|
public override IQueryable<Sence> Include(IQueryable<Sence> query)
|
|
{
|
|
return query.Include(o => o.Node);
|
|
}
|
|
|
|
public override void ToDisplayModel(Sence entity, DisplaySenceModel model)
|
|
{
|
|
model.NodeId = entity.Node.Name;
|
|
}
|
|
|
|
public override IActionResult Add(EditSenceModel model)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
var number = this._nodeRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == model.NodeId).Number;
|
|
this._pageHubContext.Clients.Group(number).SendAsync(nameof(INodeService.CreateSence), model);
|
|
return RedirectTo();
|
|
}
|
|
ModelState.AddModelError("", "服务器出现异常,请稍后重试");
|
|
return View(model);
|
|
}
|
|
|
|
public override IActionResult Edit(EditSenceModel model)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
var entity = this._repo.Table().FirstOrDefault(o => o.Id == model.Id);
|
|
var number = this._nodeRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == entity.NodeId).Number;
|
|
this._pageHubContext.Clients.Group(number).SendAsync(nameof(INodeService.EditSence), model);
|
|
return RedirectTo();
|
|
}
|
|
ModelState.AddModelError("", "服务器出现异常,请稍后重试");
|
|
return View(model);
|
|
}
|
|
|
|
public override IActionResult Delete(List<Guid> list)
|
|
{
|
|
foreach (var id in list)
|
|
{
|
|
try
|
|
{
|
|
var entity = this._repo.Table().FirstOrDefault(o => o.Id == id);
|
|
var number = this._nodeRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == entity.NodeId).Number;
|
|
this._pageHubContext.Clients.Group(number).SendAsync(nameof(INodeService.DeleteSence), id);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
}
|
|
}
|
|
return RedirectTo();
|
|
}
|
|
|
|
public override void ToModel(Sence entity, EditSenceModel model)
|
|
{
|
|
ViewData.SelectList(o => model.NodeId, () => this._ajax.GetNodeSelectList(model.NodeId));
|
|
base.ToModel(entity, model);
|
|
}
|
|
}
|
|
} |