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/Services/DataService.cs

110 lines
3.9 KiB

using Application.Domain.Entities;
using Infrastructure.Application;
using Infrastructure.Data;
using Infrastructure.Domain;
using Infrastructure.Extensions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace IoT.Shared.Services
{
public class DataService
{
private readonly IConfiguration _cfg;
private readonly IServiceProvider _services;
private readonly IRepository<Product> _productRepo;
private readonly IRepository<Scene> _sceneRepo;
private readonly IRepository<SceneCommand> _sceneCommandRepo;
private readonly IRepository<IoTTimer> _iotTimerRepo;
private readonly IRepository<IoTTigger> _iotTiggerRepo;
public DataService(IConfiguration cfg,
IServiceProvider services,
IRepository<Node> nodeRepo,
IRepository<Device> deviceRepo,
IRepository<Data> dataRepo,
IRepository<Api> apiRepo,
IRepository<Command> commandRepo,
IRepository<Category> categoryRepo,
IRepository<Product> productRepo,
IRepository<Scene> sceneRepo,
IRepository<SceneCommand> sceneCommandRepo,
IRepository<IoTTimer> iotTimerRepo,
IRepository<IoTTigger> iotTiggerRepo)
{
this._cfg = cfg;
this._services = services;
this._productRepo = productRepo;
this._sceneRepo = sceneRepo;
this._sceneCommandRepo = sceneCommandRepo;
this._iotTimerRepo = iotTimerRepo;
this._iotTiggerRepo = iotTiggerRepo;
}
public void UpdateList<T>(string message, Expression<Func<T, bool>> predicate) where T : BaseEntity
{
using var scope = this._services.CreateScope();
var repo = scope.ServiceProvider.GetService<IRepository<T>>();
var list = message.FromJson<List<Guid>>();
var entities = repo.Table().Where(predicate).Where(o => !list.Contains(o.Id)).ToList();
foreach (var entity in entities)
{
repo.Delete(entity);
}
repo.SaveChanges();
}
#region private
public void Update<T>(string message) where T : BaseEntity
{
using var scope = this._services.CreateScope();
var repo = scope.ServiceProvider.GetService<IRepository<T>>();
var model = message.FromJson<T>();
var entity = repo.Table().FirstOrDefault(o => o.Id == model.Id);
if (entity == null)
{
entity = Activator.CreateInstance<T>();
repo.Add(entity);
}
entity.From(model);
repo.SaveChanges();
}
public void Edit<T, TEditModel>(TEditModel model)
where T : BaseEntity
where TEditModel : EditModel
{
using var scope = this._services.CreateScope();
var repo = scope.ServiceProvider.GetService<IRepository<T>>();
var entity = repo.Table().FirstOrDefault(o => o.Id == model.Id);
if (entity == null)
{
entity = Activator.CreateInstance<T>();
repo.Add(entity);
}
entity.From(model);
repo.SaveChanges();
}
public void Delete<T, TEditModel>(TEditModel model)
where T : BaseEntity
where TEditModel : EditModel
{
using var scope = this._services.CreateScope();
var repo = scope.ServiceProvider.GetService<IRepository<T>>();
var entity = repo.Table().FirstOrDefault(o => o.Id == model.Id);
if (entity != null)
{
repo.Delete(entity);
repo.SaveChanges();
}
}
#endregion private
}
}