manager-service/ManagerService/Services/DeviceDatabaseService.cs

86 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Manager.Interfaces.Models;
using Microsoft.Extensions.Configuration;
using MongoDB.Driver;
namespace Manager.Services
{
public class DeviceDatabaseService
{
private readonly IMongoCollection<Device> _Devices;
public DeviceDatabaseService(IConfiguration config)
{
var client = new MongoClient(config.GetConnectionString("TabletDbOld"));
var database = client.GetDatabase("TabletDbOld");
_Devices = database.GetCollection<Device>("Devices");
}
public List<Device> GetAll()
{
return _Devices.Find(d => true).ToList();
}
public List<Device> GetAllConnected()
{
return _Devices.Find(d => d.Connected).ToList();
}
public List<Device> GetAllWithConfig(string configId)
{
return _Devices.Find(d => d.ConfigurationId == configId).ToList();
}
public Device GetById(string id)
{
return _Devices.Find<Device>(d => d.Id == id).FirstOrDefault();
}
public Device GetByIdentifier(string identifier)
{
return _Devices.Find<Device>(d => d.Identifier == identifier).FirstOrDefault();
}
public bool IsExistIdentifier(string identifier)
{
return _Devices.Find<Device>(d => d.Identifier == identifier).FirstOrDefault() != null ? true : false;
}
public bool IsExistIpWLAN(string ip)
{
return _Devices.Find<Device>(d => d.IpAddressWLAN == ip).FirstOrDefault() != null ? true : false;
}
public bool IsExistIpETH(string ip)
{
return _Devices.Find<Device>(d => d.IpAddressETH == ip).FirstOrDefault() != null ? true : false;
}
public bool IsExist(string id)
{
return _Devices.Find<Device>(d => d.Id == id).FirstOrDefault() != null ? true : false;
}
public Device Create(Device device)
{
_Devices.InsertOne(device);
return device;
}
public Device Update(string id, Device deviceIn)
{
_Devices.ReplaceOne(d => d.Id == id, deviceIn);
return deviceIn;
}
public void Remove(string id)
{
_Devices.DeleteOne(d => d.Id == id);
}
}
}