mycorerepository/MyCore/Services/Devices/ScreenDeviceService.cs

54 lines
1.5 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 ScreenDeviceService
{
private readonly IMongoCollection<ScreenDevice> _screenDevices;
public ScreenDeviceService(IConfiguration config)
{
var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
var database = client.GetDatabase("MyCoreDb");
_screenDevices = database.GetCollection<ScreenDevice>("ScreenDevices");
}
public List<ScreenDevice> GetAll()
{
return _screenDevices.Find(m => true).ToList();
}
public ScreenDevice GetInfo(string id)
{
return _screenDevices.Find<ScreenDevice>(m => m.Id == id).FirstOrDefault();
}
public ScreenDevice Create(ScreenDevice device)
{
_screenDevices.InsertOne(device);
return device;
}
public void Update(string id, ScreenDevice screenDeviceIn)
{
_screenDevices.ReplaceOne(screenDevice => screenDevice.Id == id, screenDeviceIn);
}
public void Remove(ScreenDevice screenDeviceIn)
{
_screenDevices.DeleteOne(screenDevice => screenDevice.Id == screenDeviceIn.Id);
}
public void Remove(string id)
{
_screenDevices.DeleteOne(screenDevice => screenDevice.Id == id);
}
}
}