From fb21254805df830c39127690d446dc425acabdd5 Mon Sep 17 00:00:00 2001 From: Thomas Fransolet Date: Wed, 25 Mar 2020 18:48:59 +0100 Subject: [PATCH] MC Add location service --- .../MyControlPanel/LocationService.cs | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/MyCore/Services/MyControlPanel/LocationService.cs b/MyCore/Services/MyControlPanel/LocationService.cs index fc6a81c..d75c8d6 100644 --- a/MyCore/Services/MyControlPanel/LocationService.cs +++ b/MyCore/Services/MyControlPanel/LocationService.cs @@ -2,10 +2,48 @@ 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 LocationService { + private readonly IMongoCollection _Locations; + + public LocationService(IConfiguration config) + { + var client = new MongoClient(config.GetConnectionString("MyCoreDb")); + var database = client.GetDatabase("MyCoreDb"); + _Locations = database.GetCollection("Locations"); + } + public List GetLocations() + { + return _Locations.Find(l => true).ToList(); + } + + public Location GetLocationById(string id) + { + return _Locations.Find(l => l.Id == id).FirstOrDefault(); + } + + public Location CreateLocation(Location location) + { + _Locations.InsertOne(location); + return location; + } + + public Location Update(string id, Location locationIn) + { + _Locations.ReplaceOne(location => location.Id == id, locationIn); + return locationIn; + } + + public void Remove(string id) + { + _Locations.DeleteOne(location => location.Id == id); + } } }