Commit 75e98a73 authored by cann-alberto's avatar cann-alberto
Browse files

added generic MongoDbService class

parent 4533ac4f
using MMM_Server.Models;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
namespace MMM_Server.Services;
public class MongoDbService<T>
{
private readonly IMongoCollection<T> _collection;
public MongoDbService(
IOptions<MMMDatabaseSettings> databaseSettings,
string collectionName)
{
var mongoClient = new MongoClient(databaseSettings.Value.ConnectionString);
var mongoDatabase = mongoClient.GetDatabase(databaseSettings.Value.DatabaseName);
_collection = mongoDatabase.GetCollection<T>(collectionName);
}
public async Task<List<T>> GetAsync() =>
await _collection.Find(_ => true).ToListAsync();
public async Task CreateAsync(T newItem) =>
await _collection.InsertOneAsync(newItem);
//public async Task<T?> GetAsync(string id) =>
// await _collection.Find(x => x.Id == id).FirstOrDefaultAsync();
//public async Task UpdateAsync(string id, T updatedItem) =>
// await _collection.ReplaceOneAsync(x => x.Id == id, updatedItem);
//public async Task RemoveAsync(string id) =>
// await _collection.DeleteOneAsync(x => x.Id == id);
}
......@@ -4,6 +4,16 @@ using MongoDB.Driver;
namespace MMM_Server.Services;
public class PersonalProfileService : MongoDbService<PersonalProfile>
{
public PersonalProfileService(IOptions<MMMDatabaseSettings> profileDatabaseSettings)
: base(profileDatabaseSettings, profileDatabaseSettings.Value.ProfilesCollectionName)
{
}
}
/*
public class PersonalProfileService
{
private readonly IMongoCollection<PersonalProfile> _profilesCollection;
......@@ -26,17 +36,15 @@ public class PersonalProfileService
public async Task CreateAsync(PersonalProfile newProfile) =>
await _profilesCollection.InsertOneAsync(newProfile);
/*
public async Task<PersonalProfileData?> GetAsync(string id) =>
await _profilesCollection.Find(x => x.Id == id).FirstOrDefaultAsync();
//public async Task<PersonalProfileData?> GetAsync(string id) =>
// await _profilesCollection.Find(x => x.Id == id).FirstOrDefaultAsync();
public async Task UpdateAsync(string id, PersonalProfileData updatedProfile) =>
await _profilesCollection.ReplaceOneAsync(x => x.Id == id, updatedProfile);
//public async Task UpdateAsync(string id, PersonalProfileData updatedProfile) =>
// await _profilesCollection.ReplaceOneAsync(x => x.Id == id, updatedProfile);
public async Task RemoveAsync(string id) =>
await _profilesCollection.DeleteOneAsync(x => x.Id == id);
*/
//public async Task RemoveAsync(string id) =>
// await _profilesCollection.DeleteOneAsync(x => x.Id == id);
}
}*/
......@@ -2,7 +2,10 @@
"ProfileDatabase": {
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "MMM",
"ProfilesCollectionName": "PersonalProfiles"
"ProfilesCollectionName": "PersonalProfiles",
"UsersCollectionName": "Users",
"DevicesCollectionName": "Devices",
"PersonaeCollectionName": "Personae"
},
"Logging": {
"LogLevel": {
......
......@@ -2,7 +2,10 @@
"ProfileDatabase": {
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "MMM",
"ProfilesCollectionName": "PersonalProfiles"
"ProfilesCollectionName": "PersonalProfiles",
"UsersCollectionName": "Users",
"DevicesCollectionName": "Devices",
"PersonaeCollectionName": "Personae"
},
"Logging": {
"LogLevel": {
......
......@@ -14,7 +14,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("MMM-Server")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+245a85024c45de6e0375deaa5b6fec52f03abdf4")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+4533ac4fba6ed3ae5a653c9810331805a472adc4")]
[assembly: System.Reflection.AssemblyProductAttribute("MMM-Server")]
[assembly: System.Reflection.AssemblyTitleAttribute("MMM-Server")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
......
12a12690052f1eadc2d08f5bb3ea22f6b2fbc7baebfd2323a73cda3801d00bdd
b8555282181aca913b09b645a1e9970ad2c3362d226694cf8d50ee96f31c4fac
1e31261913ed492625f0b335bd9521ed7663f28f00dd74aba93b1c1d5fd09bd3
b87c4c295b5b8e9bb5da08a641a2db75a76f1d74707d38db40e8ab96b21f1286
{"documents":{"C:\\Users\\lab2a\\Documents\\Visual Studio 2022\\Projects\\MPAI-MMM\\MMM-Server\\*":"https://raw.githubusercontent.com/cann-alberto/MPAI-MMM/74d009de2e51803bbbf37fda6d6b43394606cea7/*"}}
\ No newline at end of file
{"documents":{"C:\\Users\\lab2a\\Documents\\Visual Studio 2022\\Projects\\MPAI-MMM\\MMM-Server\\*":"https://raw.githubusercontent.com/cann-alberto/MPAI-MMM/4533ac4fba6ed3ae5a653c9810331805a472adc4/*"}}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment