EFBTracker/Mongo/Mongo.cs

46 lines
1.7 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using EFBTracker.Tracking;
namespace EFBTracker.Mongo
{
public class Mongo
{
public static async Task<bool> UploadSimPosition(string email, SimPosition position){
try
{
MongoClient client = new MongoClient(
Environment.GetEnvironmentVariable("MongoDBConnectionString", EnvironmentVariableTarget.User)
);
MongoDatabaseBase database = (MongoDatabaseBase)client.GetDatabase("EFB");
MongoCollectionBase<SimPositionModel> collection = (MongoCollectionBase<SimPositionModel>)database.GetCollection<SimPositionModel>("Simdata");
SimPositionModel data = new SimPositionModel(email, position);
FilterDefinition<SimPositionModel> userFilter = Builders<SimPositionModel>.Filter.Eq(x => x.EMail, email);
UpdateDefinition<SimPositionModel> updateDefinition = Builders<SimPositionModel>.Update
.Set(x => x.LatestPosition, position)
.Set(x => x.LatestPacketUpdate, DateTime.Now);
var confirmation = await collection.UpdateOneAsync(userFilter, updateDefinition);
if (confirmation.ModifiedCount == 0)
{
collection.InsertOne(data);
}
return true;
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
}
}