using adas_core.Application.Repositories.Interfaces; using adas_core.Domain.Models.AppSettings; using adas_core.Domain.Models.DTO; using adas_core.Domain.Models.MongoModels; using MongoDB.Bson; using MongoDB.Driver; using MongoUtils = adas_core.Infrastructure.Utils.MongoUtils; namespace adas_core.Infrastructure.Repositories; /// /// Repository for managing Device entities in MongoDB. Provides methods for finding devices by various attributes and updating device statistics. /// public class DeviceRepository : MongoRepository, IDeviceRepository { private readonly ApiSettings _apiSettings; /// /// Initializes a new instance of the DeviceRepository class with the specified API settings and MongoDB database. /// /// The API settings. /// The MongoDB database. public DeviceRepository(ApiSettings apiSettings, IMongoDatabase database) : base(database) { _apiSettings = apiSettings; } /// /// Gets the name of the MongoDB collection for devices. The collection name is determined by the API settings, with a default value of "devices" if not specified. /// /// The name of the MongoDB collection for devices. public override string GetCollectionName() { return _apiSettings.Devices ?? "devices"; } /// /// Creates indexes for the Device collection in MongoDB. /// This method ensures that indexes are created for the MacAddr, SerialNumber, Uuid, and Key fields to optimize query performance. /// The MacAddr index is unique, while the others are non-unique and created in the background. /// /// A task representing the asynchronous operation. public override async Task CreateIndexes() { var options = new CreateIndexOptions { Background = true, Unique = true }; var indexes = new List> { new(Builders.IndexKeys.Ascending(d => d.MacAddr), options), new(Builders.IndexKeys.Ascending(d => d.SerialNumber), new CreateIndexOptions { Background = true }), new(Builders.IndexKeys.Ascending(d => d.Uuid), new CreateIndexOptions { Background = true }), new(Builders.IndexKeys.Ascending(d => d.Key), new CreateIndexOptions { Background = true }) }; await MongoUtils.EnsureIndexes(Collection, indexes); } /// /// Finds a device by its MAC address. This method queries the MongoDB collection for a device with the specified MAC address and returns the first matching device, or null if no match is found. /// /// The MAC address of the device to find. /// The device with the specified MAC address, or null if not found. public async Task FindByMacAddr(string deviceDtoMacAddr) { return await Collection .Find(d => d.MacAddr == deviceDtoMacAddr) .FirstOrDefaultAsync(); } /// /// Finds a device by its serial number. This method queries the MongoDB collection for a device with the specified serial number and returns the first matching device, or null if no match is found. /// /// The serial number of the device to find. /// The device with the specified serial number, or null if not found. public async Task FindBySerialNumber(string deviceDtoSerialNumber) { return await Collection .Find(d => d.SerialNumber == deviceDtoSerialNumber) .FirstOrDefaultAsync(); } /// /// Finds a device by its UUID. This method queries the MongoDB collection for a device with the specified UUID and returns the first matching device, or null if no match is found. /// /// The UUID of the device to find. /// The device with the specified UUID, or null if not found. public async Task FindByUuid(string deviceDtoUuid) { return await Collection .Find(d => d.Uuid == deviceDtoUuid) .FirstOrDefaultAsync(); } /// /// Finds a device by its key. This method queries the MongoDB collection for a device with the specified key and returns the first matching device, or null if no match is found. /// /// The key of the device to find. /// The device with the specified key, or null if not found. public async Task FindByKey(string deviceDtoKey) { return await Collection .Find(d => d.Key == deviceDtoKey) .FirstOrDefaultAsync(); } /// /// Updates the statistics of a device. This method takes the device ID and an existing DeviceDto object, and updates the corresponding fields (Battery, Connected, Ready, Name) in the MongoDB collection for the device with the specified ID. /// The UpdatedAt field is also set to the current UTC time. /// /// The ID of the device to update. /// The existing DeviceDto object containing the updated statistics. /// A task representing the asynchronous operation. public async Task UpdateDeviceStats(ObjectId id, DeviceDto deviceExist) { var update = Builders.Update .Set(d => d.Battery, deviceExist.Battery) .Set(d => d.Connected, deviceExist.Connected) .Set(d => d.Ready, deviceExist.Ready) .Set(d => d.Name, deviceExist.Name) .Set(d => d.UpdatedAt, DateTime.UtcNow); await Collection.UpdateOneAsync( d => d.Id == id, update ); } }