Merge pull request 'Created an Utility function store logs in mogoDB' (#106) from Ashutosh_Task#767 into Expanses_Management_Feature

Reviewed-on: #106
This commit is contained in:
ashutosh.nehete 2025-07-18 11:05:02 +00:00
commit d84b01473a
3 changed files with 109 additions and 1 deletions

View File

@ -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<UpdateLogsObject>(collectionName);
await collection.InsertOneAsync(oldObject);
}
public async Task<List<UpdateLogsObject>> GetFromUpdateLogsByEntityId(Guid entityId, string collectionName)
{
var collection = _mongoDatabase.GetCollection<UpdateLogsObject>(collectionName);
var filter = Builders<UpdateLogsObject>.Filter.Eq(p => p.EntityId, entityId.ToString());
List<UpdateLogsObject> result = await collection
.Find(filter)
.ToListAsync();
return result;
}
public async Task<List<UpdateLogsObject>> GetFromUpdateLogsByUpdetedById(Guid updatedById, string collectionName)
{
var collection = _mongoDatabase.GetCollection<UpdateLogsObject>(collectionName);
var filter = Builders<UpdateLogsObject>.Filter.Eq(p => p.UpdatedById, updatedById.ToString());
List<UpdateLogsObject> 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;
}
}
}

View File

@ -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;
}
}

View File

@ -1,4 +1,3 @@
using System.Text;
using Marco.Pms.CacheHelper; using Marco.Pms.CacheHelper;
using Marco.Pms.DataAccess.Data; using Marco.Pms.DataAccess.Data;
using Marco.Pms.Model.Authentication; using Marco.Pms.Model.Authentication;
@ -16,6 +15,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using Serilog; using Serilog;
using System.Text;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -139,6 +139,7 @@ builder.Services.AddScoped<DirectoryHelper>();
builder.Services.AddScoped<MasterHelper>(); builder.Services.AddScoped<MasterHelper>();
builder.Services.AddScoped<ReportHelper>(); builder.Services.AddScoped<ReportHelper>();
builder.Services.AddScoped<CacheUpdateHelper>(); builder.Services.AddScoped<CacheUpdateHelper>();
builder.Services.AddScoped<UpdateLogHelper>();
builder.Services.AddScoped<ProjectCache>(); builder.Services.AddScoped<ProjectCache>();
builder.Services.AddScoped<EmployeeCache>(); builder.Services.AddScoped<EmployeeCache>();
builder.Services.AddSingleton<ILoggingService, LoggingService>(); builder.Services.AddSingleton<ILoggingService, LoggingService>();