mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
54 lines
1.4 KiB
C#
54 lines
1.4 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;
|
|
|
|
namespace MyCore.Services
|
|
{
|
|
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> GetAll()
|
|
{
|
|
return _devices.Find(m => true).ToList();
|
|
}
|
|
|
|
public Device GetDeviceInfo(string id)
|
|
{
|
|
return _devices.Find<Device>(m => m.Id == id).FirstOrDefault();
|
|
}
|
|
|
|
public Device CreateDevice(Device device)
|
|
{
|
|
_devices.InsertOne(device);
|
|
return device;
|
|
}
|
|
|
|
public void Update(string id, Device deviceIn)
|
|
{
|
|
_devices.ReplaceOne(device => device.Id == id, deviceIn);
|
|
}
|
|
|
|
public void Remove(Device deviceIn)
|
|
{
|
|
_devices.DeleteOne(device => device.Id == deviceIn.Id);
|
|
}
|
|
|
|
public void Remove(string id)
|
|
{
|
|
_devices.DeleteOne(device => device.Id == id);
|
|
}
|
|
}
|
|
}
|