package repository import ( "fmt" "time" "users_management/m/model/entity" "github.com/google/uuid" "gorm.io/gorm" ) type DevicesRepo interface { Post(device entity.Device) error GetAll() ([]entity.Device, error) Update(id uuid.UUID,updates map[string]interface{}) error GetByID(id uuid.UUID) (entity.Device, error) GetByType(deviceType string) ([]entity.Device, error) BulkUpdateImages(updates map[uuid.UUID]string) error BulkUpdateImagesMultiple(updates map[uuid.UUID][]string) error } type devicesRepo struct { db *gorm.DB } func NewDevicesRepo(db *gorm.DB) DevicesRepo { return &devicesRepo{ db: db, } } func (r *devicesRepo) BulkUpdateImages(updates map[uuid.UUID]string) error { multipleUpdates := make(map[uuid.UUID][]string) for deviceID, imageURL := range updates { multipleUpdates[deviceID] = []string{imageURL} } return r.BulkUpdateImagesMultiple(multipleUpdates) } func (r *devicesRepo) BulkUpdateImagesMultiple(updates map[uuid.UUID][]string) error { return r.db.Transaction(func(tx *gorm.DB) error { for deviceID, imageURLs := range updates { updateFields := map[string]interface{}{ "updated_at": time.Now(), } if len(imageURLs) > 0 { // Set primary image (first one) updateFields["image_url"] = imageURLs[0] // Set all images using StringSlice updateFields["image_urls"] = entity.StringSlice(imageURLs) } if err := tx.Model(&entity.Device{}). Where("id = ?", deviceID). Updates(updateFields).Error; err != nil { return fmt.Errorf("failed to update device %s: %w", deviceID, err) } } return nil }) } func (r *devicesRepo) Post(device entity.Device) error { return r.db.Transaction(func(tx *gorm.DB) error { // Create the device first if err := tx.Create(&device).Error; err != nil { return err } // Create the corresponding DevicePort record customerCount := 0 customerNames := make([]string, 0) // For ODP devices, initialize customer tracking if device.DeviceType == "ODP" { // Customer count starts at 0, will be updated when fishbones are connected customerCount = 0 } devicePort := entity.DevicePort{ ID: uuid.New(), DeviceID: device.ID, PortUsed: 0, PortAvailable: device.PortAmount, CustomerCount: customerCount, CustomerNames: customerNames, CreatedAt: time.Now(), UpdatedAt: time.Now(), } if err := tx.Create(&devicePort).Error; err != nil { return err } return nil }) } func (r *devicesRepo) GetAll() ([]entity.Device, error) { var devices []entity.Device err := r.db.Find(&devices).Error if err != nil { return devices, err } return devices, nil } func (r *devicesRepo) Update(id uuid.UUID,updates map[string]interface{}) error { err := r.db.Model(&entity.Device{}).Where("id = ?", id).Updates(updates).Error if err != nil { return err } return nil } func (r *devicesRepo) GetByID(id uuid.UUID) (entity.Device, error) { var device entity.Device err := r.db.Where("id = ?", id).First(&device).Error if err != nil { return device, err } return device, nil } func (r *devicesRepo) GetByType(deviceType string) ([]entity.Device, error) { var devices []entity.Device err := r.db.Where("device_type = ?", deviceType).Find(&devices).Error if err != nil { return devices, err } return devices, nil }