Mongo Class Maintainability improved

This commit is contained in:
Luke Else 2022-02-28 22:04:02 +00:00
parent 27bf158a7a
commit ed6e80fab3

View File

@ -10,18 +10,20 @@ namespace EFB.MongoData
//function that is responsible to getting the user's latest sim position from the MongoDB //function that is responsible to getting the user's latest sim position from the MongoDB
public static async Task<SimPositionModel> GetLatestData(string email) public static async Task<SimPositionModel> GetLatestData(string email)
{ {
//Create mongo client to allow connection to the database
MongoClient client = new MongoClient( MongoClient client = new MongoClient(
Environment.GetEnvironmentVariable("MongoDBConnectionString", EnvironmentVariableTarget.User) Environment.GetEnvironmentVariable("MongoDBConnectionString", EnvironmentVariableTarget.User)
); );
MongoDatabaseBase database = (MongoDatabaseBase)client.GetDatabase("EFB"); MongoDatabaseBase database = (MongoDatabaseBase)client.GetDatabase("EFB");
MongoCollectionBase<SimPositionModel> collection = (MongoCollectionBase<SimPositionModel>)database.GetCollection<SimPositionModel>("Simdata"); MongoCollectionBase<SimPositionModel> collection = (MongoCollectionBase<SimPositionModel>)database.GetCollection<SimPositionModel>("Simdata");
//Create filter to select specifically the records where the Email is the same as what we specified
FilterDefinition<SimPositionModel> filter = Builders<SimPositionModel>.Filter.Eq(x => x.EMail, email); FilterDefinition<SimPositionModel> filter = Builders<SimPositionModel>.Filter.Eq(x => x.EMail, email);
var data = await collection.FindAsync<SimPositionModel>(filter).Result.ToListAsync(); var data = await collection.FindAsync<SimPositionModel>(filter).Result.ToListAsync();
if (data.Count > 0) if (data.Count > 0)
{ {//Return the first item in the list (Should only be 1)
return data[0]; return data[0];
} }//Or return null if not present
return null; return null;
} }
} }