rama creada apartir de master en j
This commit is contained in:
@@ -13,19 +13,27 @@ using MongoUtils = adas_core.Infrastructure.Utils.MongoUtils;
|
||||
|
||||
namespace adas_core.Infrastructure.Repositories;
|
||||
|
||||
/// <summary>
|
||||
/// Repository implementation for managing Display entities in MongoDB.
|
||||
/// Provides CRUD operations and specialized queries for display devices.
|
||||
/// </summary>
|
||||
public class DisplayRepository : MongoRepository<Display>, IDisplayRepository
|
||||
{
|
||||
#region Properties
|
||||
|
||||
private readonly ApiSettings _apiSettings;
|
||||
|
||||
private readonly ILogger<DisplayRepository> _logger;
|
||||
// private readonly Idisplay<DisplayRepository> _logger;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the DisplayRepository.
|
||||
/// </summary>
|
||||
/// <param name="apiSettings">API settings containing collection names configuration.</param>
|
||||
/// <param name="database">The MongoDB database instance.</param>
|
||||
/// <param name="logger">Logger for repository operations.</param>
|
||||
public DisplayRepository(
|
||||
IOptions<ApiSettings> apiSettings,
|
||||
IMongoDatabase database,
|
||||
@@ -35,14 +43,16 @@ public class DisplayRepository : MongoRepository<Display>, IDisplayRepository
|
||||
_apiSettings = apiSettings.Value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
#region Create
|
||||
|
||||
/// <summary>
|
||||
/// Creates the necessary indexes for the Display collection.
|
||||
/// Creates indexes on displayConfigId and unitId fields for improved query performance.
|
||||
/// </summary>
|
||||
public override async Task CreateIndexes()
|
||||
{
|
||||
var options = new CreateIndexOptions<Display> { Background = true, Unique = false };
|
||||
@@ -60,17 +70,32 @@ public class DisplayRepository : MongoRepository<Display>, IDisplayRepository
|
||||
|
||||
#region Read
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the collection for displays.
|
||||
/// </summary>
|
||||
/// <returns>The collection name from API settings.</returns>
|
||||
public override string GetCollectionName()
|
||||
{
|
||||
return _apiSettings.Displays;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves all displays from the database.
|
||||
/// </summary>
|
||||
/// <returns>A list of all Display entities.</returns>
|
||||
public async Task<List<Display>> GetAll()
|
||||
{
|
||||
var result = await Collection.Find(Builders<Display>.Filter.Empty).ToListAsync();
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves paginated displays with optional filtering.
|
||||
/// Supports filtering by text, unit ID, unit name, and display type.
|
||||
/// </summary>
|
||||
/// <param name="filter">The pagination and filtering parameters.</param>
|
||||
/// <returns>A fluent queryable for Display results.</returns>
|
||||
/// <exception cref="BadRequestException">Thrown when the text filter exceeds 100 characters.</exception>
|
||||
public IFindFluent<Display, Display> GetPaginatedDisplays(PaginationFilter filter)
|
||||
{
|
||||
var filterBuilder = Builders<Display>.Filter;
|
||||
@@ -117,6 +142,12 @@ public class DisplayRepository : MongoRepository<Display>, IDisplayRepository
|
||||
return CreateFindFluent(filters, sort);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a fluent query for paginated displays with combined filters.
|
||||
/// </summary>
|
||||
/// <param name="filters">List of filter definitions to apply.</param>
|
||||
/// <param name="sort">Sort definition for the query results.</param>
|
||||
/// <returns>A fluent queryable for Display results.</returns>
|
||||
private IFindFluent<Display, Display> CreateFindFluent(List<FilterDefinition<Display>> filters,
|
||||
SortDefinition<Display> sort)
|
||||
{
|
||||
@@ -126,6 +157,11 @@ public class DisplayRepository : MongoRepository<Display>, IDisplayRepository
|
||||
return Collection.Find(combinedFilter).Sort(sort);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves all displays associated with a specific point of care.
|
||||
/// </summary>
|
||||
/// <param name="pointOfCare">The PointOfCare to filter by.</param>
|
||||
/// <returns>A list of Display entities associated with the point of care.</returns>
|
||||
public async Task<List<Display>> GetByPointOfCare(PointOfCare pointOfCare)
|
||||
{
|
||||
var filter = Builders<Display>.Filter.AnyEq(x => x.PointOfCareIdList, pointOfCare.Id);
|
||||
@@ -133,19 +169,34 @@ public class DisplayRepository : MongoRepository<Display>, IDisplayRepository
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves a display by its name.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the display to retrieve.</param>
|
||||
/// <returns>The Display if found; otherwise, null.</returns>
|
||||
public async Task<Display?> GetByName(string name)
|
||||
{
|
||||
var result = await Collection.FindAsync(Builders<Display>.Filter.Eq(p => p.Name, name));
|
||||
return await result.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves a display by its ID.
|
||||
/// </summary>
|
||||
/// <param name="id">The ObjectId of the display to retrieve.</param>
|
||||
/// <returns>The Display if found; otherwise, null.</returns>
|
||||
public async Task<Display?> GetById(ObjectId id)
|
||||
{
|
||||
var result = await Collection.FindAsync(Builders<Display>.Filter.Eq(p => p.Id, id));
|
||||
|
||||
return await result.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves a display by its ID with the associated display configuration.
|
||||
/// Performs a MongoDB lookup to join the display with its configuration.
|
||||
/// </summary>
|
||||
/// <param name="id">The ObjectId of the display to retrieve.</param>
|
||||
/// <returns>The Display with DisplayConfig populated if found; otherwise, null.</returns>
|
||||
public async Task<Display?> GetByIdWithConfigDisplay(ObjectId id)
|
||||
{
|
||||
var pipeline = new BsonDocument[]
|
||||
@@ -169,6 +220,11 @@ public class DisplayRepository : MongoRepository<Display>, IDisplayRepository
|
||||
return await result.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves all displays associated with a specific unit.
|
||||
/// </summary>
|
||||
/// <param name="id">The ObjectId of the unit.</param>
|
||||
/// <returns>A list of Display entities associated with the unit.</returns>
|
||||
public async Task<List<Display>> GetByUnitId(ObjectId id)
|
||||
{
|
||||
var filter = Builders<Display>.Filter.Eq(p => p.UnitId, id);
|
||||
@@ -176,6 +232,12 @@ public class DisplayRepository : MongoRepository<Display>, IDisplayRepository
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Counts the number of displays associated with a specific unit.
|
||||
/// </summary>
|
||||
/// <param name="unitId">The ObjectId of the unit.</param>
|
||||
/// <returns>The count of displays for the unit, or 0 if an error occurs.</returns>
|
||||
/// <exception cref="Exception">Logs errors and returns 0 on failure.</exception>
|
||||
public async Task<long> CountByUnitId(ObjectId unitId)
|
||||
{
|
||||
try
|
||||
@@ -189,12 +251,24 @@ public class DisplayRepository : MongoRepository<Display>, IDisplayRepository
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves all displays associated with a specific display configuration.
|
||||
/// </summary>
|
||||
/// <param name="id">The ObjectId of the display configuration.</param>
|
||||
/// <returns>A list of Display entities using the specified configuration.</returns>
|
||||
public async Task<List<Display>> GetByConfigId(ObjectId id)
|
||||
{
|
||||
var result = await Collection.FindAsync(Builders<Display>.Filter.Eq(p => p.DisplayConfigId, id));
|
||||
return result.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves all displays that use a specific card configuration through their display configuration.
|
||||
/// Performs an aggregation to join Display with DisplayConfig and filter by cardConfigId.
|
||||
/// </summary>
|
||||
/// <param name="configId">The ObjectId of the card configuration.</param>
|
||||
/// <returns>A list of Display entities using the specified card config, or empty list on error.</returns>
|
||||
/// <exception cref="Exception">Logs errors and returns empty list on failure.</exception>
|
||||
public async Task<List<Display>> GetByCardConfigId(ObjectId configId)
|
||||
{
|
||||
try
|
||||
@@ -228,6 +302,11 @@ public class DisplayRepository : MongoRepository<Display>, IDisplayRepository
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a display configuration is currently in use by any displays.
|
||||
/// </summary>
|
||||
/// <param name="displayConfigId">The ObjectId of the display configuration to check.</param>
|
||||
/// <returns>The count of displays using the configuration.</returns>
|
||||
public async Task<long> IsDisplayConfigInUse(ObjectId displayConfigId)
|
||||
{
|
||||
return await Collection.CountDocumentsAsync(
|
||||
@@ -238,6 +317,13 @@ public class DisplayRepository : MongoRepository<Display>, IDisplayRepository
|
||||
|
||||
#region Update
|
||||
|
||||
/// <summary>
|
||||
/// Updates the point of care list for a specific display.
|
||||
/// </summary>
|
||||
/// <param name="objectId">The ObjectId of the display to update.</param>
|
||||
/// <param name="listPocObId">The new list of point of care ObjectIds.</param>
|
||||
/// <returns>The updated Display if successful; otherwise, null.</returns>
|
||||
/// <exception cref="Exception">Logs errors and returns null on failure.</exception>
|
||||
public async Task<Display?> UpdatePointOfCareList(ObjectId objectId, List<ObjectId> listPocObId)
|
||||
{
|
||||
try
|
||||
@@ -256,6 +342,13 @@ public class DisplayRepository : MongoRepository<Display>, IDisplayRepository
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the display configuration for a specific display.
|
||||
/// </summary>
|
||||
/// <param name="objectId">The ObjectId of the display to update.</param>
|
||||
/// <param name="config">The new DisplayConfig to set.</param>
|
||||
/// <returns>The updated Display if successful; otherwise, null.</returns>
|
||||
/// <exception cref="Exception">Logs errors and returns null on failure.</exception>
|
||||
public async Task<Display?> UpdateConfig(ObjectId objectId, DisplayConfig config)
|
||||
{
|
||||
try
|
||||
@@ -274,6 +367,13 @@ public class DisplayRepository : MongoRepository<Display>, IDisplayRepository
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the display configuration ID reference for a specific display.
|
||||
/// </summary>
|
||||
/// <param name="objectId">The ObjectId of the display to update.</param>
|
||||
/// <param name="displayConfigId">The new display configuration ObjectId to set.</param>
|
||||
/// <returns>The updated Display if successful; otherwise, null.</returns>
|
||||
/// <exception cref="Exception">Logs errors and returns null on failure.</exception>
|
||||
public async Task<Display?> UpdateConfigId(ObjectId objectId, ObjectId displayConfigId)
|
||||
{
|
||||
try
|
||||
@@ -292,6 +392,13 @@ public class DisplayRepository : MongoRepository<Display>, IDisplayRepository
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the display configuration preset for a specific display.
|
||||
/// </summary>
|
||||
/// <param name="objectIdDisplay">The ObjectId of the display to update.</param>
|
||||
/// <param name="objectIdConfigDisplay">The ObjectId of the display configuration preset.</param>
|
||||
/// <returns>The updated Display if successful; otherwise, null.</returns>
|
||||
/// <exception cref="Exception">Logs errors and returns null on failure.</exception>
|
||||
public async Task<Display?> UpdateConfigPreset(ObjectId objectIdDisplay, ObjectId objectIdConfigDisplay)
|
||||
{
|
||||
try
|
||||
@@ -310,6 +417,13 @@ public class DisplayRepository : MongoRepository<Display>, IDisplayRepository
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the name of a specific display.
|
||||
/// </summary>
|
||||
/// <param name="display">The Display entity to update.</param>
|
||||
/// <param name="name">The new name for the display.</param>
|
||||
/// <returns>The updated Display if successful.</returns>
|
||||
/// <exception cref="Exception">Throws an exception if MongoDB update fails.</exception>
|
||||
public async Task<Display> UpdateName(Display display, string name)
|
||||
{
|
||||
try
|
||||
@@ -333,6 +447,12 @@ public class DisplayRepository : MongoRepository<Display>, IDisplayRepository
|
||||
|
||||
#region Delete
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all displays associated with a specific unit.
|
||||
/// </summary>
|
||||
/// <param name="unitId">The ObjectId of the unit whose displays should be deleted.</param>
|
||||
/// <returns>True if deletion was successful; otherwise, false.</returns>
|
||||
/// <exception cref="Exception">Logs errors and returns false on failure.</exception>
|
||||
public async Task<bool> DeleteManyByUnitId(ObjectId unitId)
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user