diff --git a/Marco.Pms.CacheHelper/UpdateLogHelper.cs b/Marco.Pms.CacheHelper/UpdateLogHelper.cs new file mode 100644 index 0000000..9bc520a --- /dev/null +++ b/Marco.Pms.CacheHelper/UpdateLogHelper.cs @@ -0,0 +1,91 @@ +using Marco.Pms.Model.MongoDBModels; +using Microsoft.Extensions.Configuration; +using MongoDB.Bson; +using MongoDB.Driver; +using System.Collections; + +namespace Marco.Pms.CacheHelper +{ + public class UpdateLogHelper + { + private readonly IMongoDatabase _mongoDatabase; + public UpdateLogHelper(IConfiguration configuration) + { + var connectionString = configuration["MongoDB:ConnectionString"]; + var mongoUrl = new MongoUrl(connectionString); + var client = new MongoClient(mongoUrl); // Your MongoDB connection string + _mongoDatabase = client.GetDatabase(mongoUrl.DatabaseName); // Your MongoDB Database name + } + public async Task PushToUpdateLogs(UpdateLogsObject oldObject, string collectionName) + { + var collection = _mongoDatabase.GetCollection(collectionName); + await collection.InsertOneAsync(oldObject); + } + + public async Task> GetFromUpdateLogsByEntityId(Guid entityId, string collectionName) + { + var collection = _mongoDatabase.GetCollection(collectionName); + var filter = Builders.Filter.Eq(p => p.EntityId, entityId.ToString()); + + List result = await collection + .Find(filter) + .ToListAsync(); + + return result; + } + + public async Task> GetFromUpdateLogsByUpdetedById(Guid updatedById, string collectionName) + { + var collection = _mongoDatabase.GetCollection(collectionName); + var filter = Builders.Filter.Eq(p => p.UpdatedById, updatedById.ToString()); + + List result = await collection + .Find(filter) + .ToListAsync(); + + return result; + } + + public BsonDocument NormalizeGuidsToStrings(object entity) + { + var bson = new BsonDocument(); + + var props = entity.GetType().GetProperties(); + foreach (var prop in props) + { + var value = prop.GetValue(entity); + if (value == null) + { + bson[prop.Name] = BsonNull.Value; + continue; + } + + if (value is Guid guidValue) + { + bson[prop.Name] = guidValue.ToString(); // store Guid as string + } + else if (value is string || value.GetType().IsPrimitive || value is DateTime) + { + bson[prop.Name] = BsonValue.Create(value); // simple types + } + else if (value is IEnumerable list && !(value is string)) + { + var array = new BsonArray(); + foreach (var item in list) + { + array.Add(NormalizeGuidsToStrings(item)); // recursive + } + bson[prop.Name] = array; + } + else + { + // nested object + continue; + } + } + + return bson; + } + + } +} diff --git a/Marco.Pms.Model/MongoDBModels/UpdateLogsObject.cs b/Marco.Pms.Model/MongoDBModels/UpdateLogsObject.cs new file mode 100644 index 0000000..3153c78 --- /dev/null +++ b/Marco.Pms.Model/MongoDBModels/UpdateLogsObject.cs @@ -0,0 +1,16 @@ +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; + +namespace Marco.Pms.Model.MongoDBModels +{ + public class UpdateLogsObject + { + [BsonId] + [BsonRepresentation(BsonType.String)] + public Guid Id { get; set; } = Guid.NewGuid(); + public string EntityId { get; set; } = string.Empty; + public BsonDocument? OldObject { get; set; } + public string UpdatedById { get; set; } = string.Empty; + public DateTime UpdatedAt { get; set; } = DateTime.UtcNow; + } +} diff --git a/Marco.Pms.Services/Program.cs b/Marco.Pms.Services/Program.cs index 5549702..e67ed7a 100644 --- a/Marco.Pms.Services/Program.cs +++ b/Marco.Pms.Services/Program.cs @@ -183,6 +183,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); #endregion #region Cache Services