using adas_core.Domain.Enums; using adas_core.Domain.Models.MongoModels; using adas_core.Infrastructure.Utils.MongoMaps.Interfaz; using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; namespace adas_core.Infrastructure.Utils.MongoMaps; /// /// Provides an entity map contributor that relays or forwards mapping contributions from an underlying source. /// Implements the contract to participate in entity mapping workflows. /// public class RelayContributor : IEntityMapContributor { /// /// Registers the BSON class map for the type, defining how its properties are serialized and stored in MongoDB. The registration is performed only when no class map is already registered for the type, and it configures default values, string-based enum serialization, and null-ignoring behavior for optional members. /// public void RegisterMaps() { if (!BsonClassMap.IsClassMapRegistered(typeof(Relay))) BsonClassMap.RegisterClassMap(cm => { cm.AutoMap(); cm.MapIdMember(c => c.Id).SetElementName("_id"); cm.MapMember(c => c.Type) .SetDefaultValue(RelayEnum.Type.Door) .SetSerializer(new EnumSerializer(BsonType.String)); cm.MapMember(c => c.Driver).SetIgnoreIfNull(true); cm.MapMember(c => c.Ip).SetIgnoreIfNull(true); cm.MapMember(c => c.Port).SetDefaultValue(0); cm.MapMember(c => c.RelayNumber).SetDefaultValue(1); cm.MapMember(c => c.RelayName).SetIgnoreIfNull(true); cm.MapMember(c => c.Total).SetDefaultValue(1); cm.MapMember(c => c.RefreshTime).SetDefaultValue(60); cm.MapMember(c => c.Open).SetDefaultValue(false); cm.MapMember(c => c.Status) .SetDefaultValue(RelayEnum.Status.NotInitialized) .SetSerializer(new EnumSerializer(BsonType.String)); cm.MapMember(c => c.Username).SetIgnoreIfNull(true); cm.MapMember(c => c.Password).SetIgnoreIfNull(true); cm.MapMember(c => c.Mode) .SetDefaultValue(RelayEnum.Mode.OpenedOnClosedOff) .SetSerializer(new EnumSerializer(BsonType.String)); cm.MapMember(c => c.RebootDelay).SetIgnoreIfNull(true); cm.MapMember(c => c.Cache).SetDefaultValue(true); cm.MapMember(c => c.InUse).SetIgnoreIfNull(true).SetIsRequired(false); cm.MapMember(c => c.ManualRelayStatus) .SetIgnoreIfNull(true) .SetSerializer( new NullableSerializer( new EnumSerializer(BsonType.String))); }); } }