using Infrastructure.Application; using Infrastructure.Data; using Infrastructure.Domain; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; using IoT.Shared.Services; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.DependencyInjection; using Platform.Services; using System; using System.Reflection; namespace IoT.Shared.Areas.IoTCenter.Controlls { public class SharedController : CrudController where TEntity : BaseEntity where TEditModel : EditModel { protected readonly IServiceProvider applicationServices; protected readonly bool isPlatform; public SharedController(IRepository repo, IServiceProvider applicationServices) : base(repo) { this.applicationServices = applicationServices; this.isPlatform = Assembly.GetEntryAssembly().GetName().Name.Contains("Platform"); } public override IActionResult Edit(TEditModel model) { if (ModelState.IsValid) { try { var method = $"Edit{typeof(TEntity).Name}"; using var scope = this.applicationServices.CreateScope(); if (this.isPlatform) { var group = this.GetNodeNumber(model); this.ToEntity(model, null); var hub = scope.ServiceProvider.GetService>(); hub.ServerToClient(method, model, group); } else { var dataService = scope.ServiceProvider.GetService(); dataService.Edit(model); } return Success(); } catch (Exception ex) { ex.PrintStack(); ModelState.AddModelError("", ex.Message); } } this.ToEditModel(null, model); return Result(model); } [ApiExplorerSettings(IgnoreApi = true)] public virtual string GetNodeNumber(TEditModel model) { throw new NotImplementedException(); } } }