From a6b6d139a03261a28dc96e45b8aa145a15f26006 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Fri, 18 Feb 2022 11:41:56 +0000 Subject: [PATCH] Added DB functionality to record user's latest sim position + alt --- EFBTracker.csproj | 1 + Mongo/Mongo.cs | 46 +++++++++++++++++++++++++++++++++++++++ Mongo/SimPositionModel.cs | 27 +++++++++++++++++++++++ Program.cs | 9 ++------ 4 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 Mongo/Mongo.cs create mode 100644 Mongo/SimPositionModel.cs diff --git a/EFBTracker.csproj b/EFBTracker.csproj index 3fd70ee..3e896b9 100644 --- a/EFBTracker.csproj +++ b/EFBTracker.csproj @@ -7,5 +7,6 @@ + \ No newline at end of file diff --git a/Mongo/Mongo.cs b/Mongo/Mongo.cs new file mode 100644 index 0000000..63edd3b --- /dev/null +++ b/Mongo/Mongo.cs @@ -0,0 +1,46 @@ +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 UploadSimPosition(string email, SimPosition position){ + try + { + + MongoClient client = new MongoClient( + Environment.GetEnvironmentVariable("MongoDBConnectionString", EnvironmentVariableTarget.User) + ); + MongoDatabaseBase database = (MongoDatabaseBase)client.GetDatabase("EFB"); + MongoCollectionBase collection = (MongoCollectionBase)database.GetCollection("Simdata"); + + SimPositionModel data = new SimPositionModel(email, position); + FilterDefinition userFilter = Builders.Filter.Eq(x => x.EMail, email); + UpdateDefinition updateDefinition = Builders.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; + } + + } + } + + +} \ No newline at end of file diff --git a/Mongo/SimPositionModel.cs b/Mongo/SimPositionModel.cs new file mode 100644 index 0000000..22d3e2b --- /dev/null +++ b/Mongo/SimPositionModel.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using EFBTracker.Tracking; +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; +using MongoDB.Driver; + +namespace EFBTracker.Mongo +{ + public class SimPositionModel + { + [BsonId] + public ObjectId Id { get; set; } + public string EMail { get; set; } = ""; + public DateTime LatestPacketUpdate { get; set; } + public SimPosition? LatestPosition { get; set; } = null; + + public SimPositionModel(string email, SimPosition position){ + EMail = email; + LatestPacketUpdate = DateTime.Now; + LatestPosition = position; + } + + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 13499c6..0960cd3 100644 --- a/Program.cs +++ b/Program.cs @@ -2,6 +2,7 @@ using System.Net.Sockets; using System.Text; using EFBTracker.Tracking; +using EFBTracker.Mongo; bool done = false; int listenPort = 49003; @@ -23,13 +24,7 @@ using(UdpClient listener = new UdpClient(listenPort)) { SimPosition position = new SimPosition(data); Console.WriteLine($"Latitude: {position.Latitude} Longitude: {position.Longitude} Altitude: {position.Altitude}"); + await Mongo.UploadSimPosition("example@example.com", position); } - - - - - - - } }