60 lines
1.6 KiB
C#
60 lines
1.6 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 RessourceDatabaseService
|
|
{
|
|
private readonly IMongoCollection<Ressource> _Ressources;
|
|
|
|
public RessourceDatabaseService(IConfiguration config)
|
|
{
|
|
var client = new MongoClient(config.GetConnectionString("TabletDb"));
|
|
var database = client.GetDatabase("TabletDb");
|
|
_Ressources = database.GetCollection<Ressource>("Ressources");
|
|
}
|
|
public List<Ressource> GetAll()
|
|
{
|
|
return _Ressources.Find(r => true).ToList();
|
|
}
|
|
|
|
public Ressource GetByType(RessourceType type)
|
|
{
|
|
return _Ressources.Find<Ressource>(r => r.Type == type).FirstOrDefault();
|
|
}
|
|
|
|
public Ressource GetById(string id)
|
|
{
|
|
return _Ressources.Find<Ressource>(r => r.Id == id).FirstOrDefault();
|
|
}
|
|
|
|
public bool IsExist(string id)
|
|
{
|
|
return _Ressources.Find<Ressource>(r => r.Id == id).FirstOrDefault() != null ? true : false;
|
|
}
|
|
|
|
public Ressource Create(Ressource ressource)
|
|
{
|
|
_Ressources.InsertOne(ressource);
|
|
return ressource;
|
|
}
|
|
|
|
public Ressource Update(string id, Ressource ressourceIn)
|
|
{
|
|
_Ressources.ReplaceOne(r => r.Id == id, ressourceIn);
|
|
return ressourceIn;
|
|
}
|
|
|
|
public void Remove(string id)
|
|
{
|
|
_Ressources.DeleteOne(r => r.Id == id);
|
|
}
|
|
|
|
}
|
|
}
|