using adas_core.Application.Repositories.Interfaces; using adas_core.Domain.Models.AppSettings; using adas_core.Domain.Models.MongoModels; using adas_core.Domain.Models.Responses; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using MongoDB.Bson; using MongoDB.Driver; namespace adas_core.Infrastructure.Repositories; /// /// Repository for managing ChartConfig in MongoDB. Provides methods to retrieve and manipulate ChartConfig data. /// public class DisplayChartConfigRepository : MongoRepository, IDisplayChartConfigRepository { private readonly ApiSettings _apiSettings; private readonly ILogger _logger; /// /// Initializes a new instance of the DisplayChartConfigRepository class with the specified API settings, MongoDB database, and logger. /// /// The MongoDB database instance. /// The API settings instance. /// The logger instance. public DisplayChartConfigRepository(IMongoDatabase database, ApiSettings apiSettings, ILogger logger) : base(database) { _apiSettings = apiSettings; _logger = logger; } /// /// Gets the name of the MongoDB collection for ChartConfig. This method retrieves the collection name from the API settings. /// /// The name of the MongoDB collection for ChartConfig. public override string GetCollectionName() { return _apiSettings.DisplayChartConfig; } /// /// Retrieves all ChartConfig documents from the MongoDB collection. This method returns a list of ChartConfig objects representing all the configurations stored in the database. /// /// A list of ChartConfig objects representing all the configurations stored in the database. public async Task> GetAll() { var result = await Collection.Find(Builders.Filter.Empty).ToListAsync(); return result; } /// /// Retrieves a ChartConfig document from the MongoDB collection by its unique identifier. This method takes an ObjectId as a parameter and returns the corresponding ChartConfig object if found, or null if no matching document is found. /// /// The unique identifier of the ChartConfig document. /// The ChartConfig object if found, or null if no matching document is found. public async Task GetById(ObjectId configId) { try { var result = await Collection.FindAsync(Builders.Filter.Eq(p => p.Id, configId)); return await result.FirstOrDefaultAsync(); } catch (Exception ex) { _logger.LogError("Error on config card display repository on GetById Exception: {ex}", ex.Message); return null; } } /// /// Inserts a new ChartConfig document into the MongoDB collection and returns the inserted document. /// This method takes a ChartConfig object as a parameter, inserts it into the database, and returns the same object if the insertion is successful. If an error occurs during the insertion process, it logs the error and returns null. /// /// The ChartConfig object to be inserted into the MongoDB collection. /// The inserted ChartConfig object if successful, or null if an error occurs. public async Task InsertOneAsyncAndReturn(ChartConfig config) { try { await Collection.InsertOneAsync(config); return config; } catch (Exception ex) { _logger.LogError(ex.Message); return null; } } /// /// Updates an existing ChartConfig document in the MongoDB collection based on the provided ChartConfig object. /// This method takes a ChartConfig object as a parameter, identifies the document to be updated using the Id property, and updates the BaseConfig, AxesConfig, and SeriesConfig fields of the matching document. /// It returns an UpdateResponse object containing the number of changes made and the updated ChartConfig document. /// If an error occurs during the update process, it logs the error and returns an UpdateResponse with zero changes and null data. /// /// The ChartConfig object containing the updated data. /// An UpdateResponse object containing the number of changes made and the updated ChartConfig document. public async Task> UpdateOne(ChartConfig? config) { try { if (config == null) return new UpdateResponse(0, null); var filter = Builders.Filter.Eq(c => c.Id, config.Id); var update = Builders.Update .Set(c => c.BaseConfig, config.BaseConfig) .Set(c => c.AxesConfig, config.AxesConfig) .Set(c => c.SeriesConfig, config.SeriesConfig); // Realizamos la actualización var result = await Collection.UpdateOneAsync(filter, update); // Buscamos el documento actual (ya actualizado o el existente si no hubo cambios) var updatedDoc = await Collection.Find(filter).FirstOrDefaultAsync(); // result.ModifiedCount será 1 si cambió algo, o 0 si los datos eran idénticos return new UpdateResponse(result.ModifiedCount, updatedDoc); } catch (Exception e) { _logger.LogError(e.Message); return new UpdateResponse(0, null); } } /// /// Deletes a ChartConfig document from the MongoDB collection based on the provided unique identifier. /// /// ChartConfig Id to be deleted /// public async Task DeleteOne(ObjectId configId) { return await DeleteAsync(configId); } }