mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
50 lines
1.3 KiB
C#
50 lines
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using MyCore.Models;
|
|
using Microsoft.Extensions.Configuration;
|
|
using MongoDB.Driver;
|
|
using MyCore.Models.MyControlPanel;
|
|
|
|
namespace MyCore.Services.MyControlPanel
|
|
{
|
|
public class DeviceService
|
|
{
|
|
private readonly IMongoCollection<Device> _Devices;
|
|
|
|
public DeviceService(IConfiguration config)
|
|
{
|
|
var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
|
|
var database = client.GetDatabase("MyCoreDb");
|
|
_Devices = database.GetCollection<Device>("Devices");
|
|
}
|
|
public List<Device> GetDevices()
|
|
{
|
|
return _Devices.Find(d => true).ToList();
|
|
}
|
|
|
|
public Device GetDeviceById(string id)
|
|
{
|
|
return _Devices.Find<Device>(d => d.Id == id).FirstOrDefault();
|
|
}
|
|
|
|
public Device CreateDevice(Device device)
|
|
{
|
|
_Devices.InsertOne(device);
|
|
return device;
|
|
}
|
|
|
|
public Device Update(string id, Device deviceIn)
|
|
{
|
|
_Devices.ReplaceOne(device => device.Id == id, deviceIn);
|
|
return deviceIn;
|
|
}
|
|
|
|
public void Remove(string id)
|
|
{
|
|
_Devices.DeleteOne(device => device.Id == id);
|
|
}
|
|
}
|
|
}
|