rama creada apartir de master en j
This commit is contained in:
@@ -9,10 +9,20 @@ using MongoUtils = adas_core.Infrastructure.Utils.MongoUtils;
|
||||
|
||||
namespace adas_core.Infrastructure.Repositories;
|
||||
|
||||
/// <summary>
|
||||
/// Repository implementation for managing Notice entities in MongoDB.
|
||||
/// Provides CRUD operations for notices/notifications that can be displayed on screens.
|
||||
/// </summary>
|
||||
public class NoticeRepository : MongoRepository<Notice>, INoticeRepository
|
||||
{
|
||||
private readonly ApiSettings _apiSettings;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the NoticeRepository.
|
||||
/// </summary>
|
||||
/// <param name="apiSettings">API settings containing collection names configuration.</param>
|
||||
/// <param name="database">The MongoDB database instance.</param>
|
||||
/// <exception cref="ArgumentNullException">Thrown when apiSettings is null.</exception>
|
||||
public NoticeRepository(IOptions<ApiSettings>? apiSettings, IMongoDatabase database) : base(database)
|
||||
{
|
||||
if (apiSettings != null)
|
||||
@@ -21,13 +31,21 @@ public class NoticeRepository : MongoRepository<Notice>, INoticeRepository
|
||||
throw new ArgumentNullException(nameof(apiSettings));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the collection for notices.
|
||||
/// </summary>
|
||||
/// <returns>The collection name from API settings.</returns>
|
||||
public override string GetCollectionName()
|
||||
{
|
||||
return _apiSettings.Notices;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts a new notice into the database.
|
||||
/// Automatically sets the NoticeDate to UTC current date and time before inserting.
|
||||
/// </summary>
|
||||
/// <param name="notice">The Notice entity to insert.</param>
|
||||
/// <exception cref="Exception">Logs warning and silently fails on insertion error.</exception>
|
||||
public override async Task InsertOneAsync(Notice notice)
|
||||
{
|
||||
try
|
||||
@@ -41,6 +59,11 @@ public class NoticeRepository : MongoRepository<Notice>, INoticeRepository
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a notice by its ID.
|
||||
/// </summary>
|
||||
/// <param name="id">The ObjectId of the notice to delete.</param>
|
||||
/// <exception cref="Exception">Throws and re-throws exceptions after logging.</exception>
|
||||
public async Task Delete(ObjectId id)
|
||||
{
|
||||
try
|
||||
@@ -55,6 +78,11 @@ public class NoticeRepository : MongoRepository<Notice>, INoticeRepository
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates an existing notice with new values.
|
||||
/// </summary>
|
||||
/// <param name="notice">The Notice entity with updated values.</param>
|
||||
/// <exception cref="Exception">Throws and re-throws exceptions after logging.</exception>
|
||||
public async Task Update(Notice notice)
|
||||
{
|
||||
try
|
||||
@@ -68,6 +96,11 @@ public class NoticeRepository : MongoRepository<Notice>, INoticeRepository
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves all notices from the database.
|
||||
/// </summary>
|
||||
/// <returns>An enumerable of all Notice entities.</returns>
|
||||
/// <exception cref="Exception">Logs errors and returns empty list on failure.</exception>
|
||||
public async Task<IEnumerable<Notice>> FindAll()
|
||||
{
|
||||
try
|
||||
@@ -82,6 +115,12 @@ public class NoticeRepository : MongoRepository<Notice>, INoticeRepository
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves a notice by its ID.
|
||||
/// </summary>
|
||||
/// <param name="id">The ObjectId of the notice to retrieve.</param>
|
||||
/// <returns>The Notice if found; otherwise, null.</returns>
|
||||
/// <exception cref="Exception">Logs errors and returns null on failure.</exception>
|
||||
public async Task<Notice?> FindById(ObjectId id)
|
||||
{
|
||||
try
|
||||
@@ -98,6 +137,12 @@ public class NoticeRepository : MongoRepository<Notice>, INoticeRepository
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves all notices for a specific date.
|
||||
/// </summary>
|
||||
/// <param name="date">The date to filter notices by.</param>
|
||||
/// <returns>An enumerable of Notice entities matching the date, or null on error.</returns>
|
||||
/// <exception cref="Exception">Logs errors and returns null on failure.</exception>
|
||||
public async Task<IEnumerable<Notice>?> FindByDate(DateTime date)
|
||||
{
|
||||
try
|
||||
@@ -114,6 +159,12 @@ public class NoticeRepository : MongoRepository<Notice>, INoticeRepository
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves all notices of a specific type.
|
||||
/// </summary>
|
||||
/// <param name="type">The notice type to filter by.</param>
|
||||
/// <returns>An enumerable of Notice entities matching the type, or null on error.</returns>
|
||||
/// <exception cref="Exception">Logs errors and returns null on failure.</exception>
|
||||
public async Task<IEnumerable<Notice>?> FindByType(string type)
|
||||
{
|
||||
try
|
||||
@@ -130,6 +181,12 @@ public class NoticeRepository : MongoRepository<Notice>, INoticeRepository
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves all notices associated with a specific display.
|
||||
/// </summary>
|
||||
/// <param name="displayId">The ObjectId of the display to filter by.</param>
|
||||
/// <returns>An enumerable of Notice entities for the display, or null on error.</returns>
|
||||
/// <exception cref="Exception">Logs errors and returns null on failure.</exception>
|
||||
public async Task<IEnumerable<Notice>?> FindByDisplayId(ObjectId displayId)
|
||||
{
|
||||
try
|
||||
@@ -146,6 +203,10 @@ public class NoticeRepository : MongoRepository<Notice>, INoticeRepository
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the necessary indexes for the Notice collection.
|
||||
/// Creates compound indexes on (noticeType, noticeDate) and (noticeDate) for improved query performance.
|
||||
/// </summary>
|
||||
public override async Task CreateIndexes()
|
||||
{
|
||||
var options = new CreateIndexOptions { Background = true, Unique = false };
|
||||
|
||||
Reference in New Issue
Block a user