Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • bad1/assignment2
1 result
Show changes
Commits on Source (2)
......@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using assignment1.Models;
using assignment2.Models;
namespace assignment2.Interfaces
......
......@@ -6,9 +6,8 @@ namespace assignment2.Models
[BsonIgnoreExtraElements]
public class LogEntry
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public int Id { get; set; }
public string? Id { get; set; }
[BsonElement("Level")]
public int Level { get; set; }
......
......@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using assignment1.Models;
using assignment2.Interfaces;
using assignment2.Models;
using Microsoft.Extensions.Options;
......@@ -13,7 +14,7 @@ namespace assignment2.Services
{
public class LogsService : ILogService
{
private readonly IMongoCollection<Log> _logsCollection;
private readonly IMongoCollection<LogEntry> _logsCollection;
private readonly MongoClient _mongoClient;
private readonly IMongoDatabase _database;
......@@ -26,39 +27,29 @@ namespace assignment2.Services
_database = _mongoClient.GetDatabase(
logsDatabaseSettings.Value.DatabaseName);
//BsonClassMap.RegisterClassMap<Log>(cm =>
//{
// cm.AutoMap();
// cm.MapMember(c => c.User).SetElementName("Properties.Log.User");
// cm.MapMember(c => c.Operation).SetElementName("Properties.Log.Operation");
// cm.MapMember(c => c.Timestamp).SetElementName("Properties.Log.Timestamp");
// cm.SetIgnoreExtraElements(true);
//});
_logsCollection = _database.GetCollection<Log>(
_logsCollection = _database.GetCollection<LogEntry>(
logsDatabaseSettings.Value.LogsCollectionName);
}
public async Task<IEnumerable<Log>> GetAsync(string? user, string? operation, DateTime? startDate, DateTime? endTime)
public async Task<IEnumerable<LogEntry>> GetAsync(string? user, string? operation, DateTime? startDate, DateTime? endTime)
{
var builder = Builders<Log>.Filter;
var filters = new List<FilterDefinition<Log>>();
var builder = Builders<LogEntry>.Filter;
var filters = new List<FilterDefinition<LogEntry>>();
if (!string.IsNullOrEmpty(user))
filters.Add(builder.Eq(u => u.User, user));
filters.Add(builder.Eq(u => u.Properties.LogInfo.User, user));
if (!string.IsNullOrEmpty(operation))
filters.Add(builder.Eq(u => u.Operation, operation));
filters.Add(builder.Eq(u => u.Properties.LogInfo.Operation, operation));
if (startDate.HasValue && endTime.HasValue)
filters.Add(builder.And(builder.Gte(u => u.Timestamp, startDate), builder.Lte(u => u.Timestamp, endTime)));
filters.Add(builder.And(builder.Gte(u => u.Properties.LogInfo.Timestamp, startDate), builder.Lte(u => u.Properties.LogInfo.Timestamp, endTime)));
var filter = filters.Count > 0 ? builder.And(filters) : builder.Empty;
return await _logsCollection.Find(filter).ToListAsync();
}
}
}
\ No newline at end of file