package management import ( "fmt" d "playback-device-server/data" "slices" gonanoid "github.com/matoous/go-nanoid" ) type DeviceManager struct { deviceDatabase *d.DeviceDatabase registrationCodes []string } func (um *DeviceManager) Initialize(deviceDatabase *d.DeviceDatabase) error { um.deviceDatabase = deviceDatabase return nil } func (um *DeviceManager) CreateDevice(device *d.PlaybackDevice) (string, error) { id, error := um.deviceDatabase.CreateDevice(device.Name, device.Description) if error != nil { return "", error } return id, nil } func (um *DeviceManager) DeviceIdExists(id string) (bool, error) { exists, error := um.deviceDatabase.DeviceIdExists(id) if error != nil { return false, error } return exists, nil } //func (um *DeviceManager) Register(code string) (string, error) { // device, error := um.GetDeviceByCode(code) // if error != nil { // return "", error // } // // expiryDate := time.Now().AddDate(0, 0, 30) // token, error := um.deviceDatabase.CreateSession(device.ID, expiryDate) // if error != nil { // return "", error // } // // return token, nil //} func (um *DeviceManager) GetSession(sessionToken string) (*d.DeviceSession, error) { session, error := um.deviceDatabase.GetSession(sessionToken) if error != nil { return nil, error } return session, nil } func (um *DeviceManager) GetDeviceById(id string) (*d.PlaybackDevice, error) { device, error := um.deviceDatabase.GetDeviceById(id) if error != nil { return nil, error } return device, nil } func (um *DeviceManager) UpdateDevice(device *d.PlaybackDevice) error { error := um.deviceDatabase.UpdateDevice(device) return error } func (um *DeviceManager) DeleteSession(token string) error { error := um.deviceDatabase.DeleteSessionByToken(token) if error != nil { return error } return nil } func (um *DeviceManager) GetDevices() (*[]d.PlaybackDevice, error) { users, error := um.deviceDatabase.GetDevices() return users, error } func (um *DeviceManager) DeleteDevice(ID string) error { error := um.deviceDatabase.DeleteDevice(ID) return error } func (um *DeviceManager) GetRegistrationCode() (string, error) { code := um.createCode() return code, nil } func (um *DeviceManager) CreateIntegration(name, code string) (d.Integration, error) { exists, err := um.deviceDatabase.IntegrationNameExists(name) if err != nil { return d.Integration{}, err } if exists { return d.Integration{}, fmt.Errorf("Integration name already exists") } if err := um.redeemCode(code); err != nil { return d.Integration{}, err } token, err := gonanoid.Nanoid(16) if err != nil { return d.Integration{}, err } id, err := um.deviceDatabase.CreateIntegration(name, token) if err != nil { return d.Integration{}, err } integration := d.Integration{ ID: id, Name: name, Token: token, } return integration, nil } func (um *DeviceManager) GetIntegrations() ([]d.Integration, error) { integrations, err := um.deviceDatabase.GetIntegrations() if err != nil { return nil, err } return integrations, nil } func (um *DeviceManager) checkCode(code string) bool { for _, c := range um.registrationCodes { if c == code { return true } } return false } func (um *DeviceManager) createCode() string { code := gonanoid.MustGenerate("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 6) um.registrationCodes = append(um.registrationCodes, code) return code } func (um *DeviceManager) redeemCode(code string) error { if !um.checkCode(code) { return fmt.Errorf("Invalid registration code") } for i, c := range um.registrationCodes { if c == code { um.registrationCodes = slices.Delete(um.registrationCodes, i, i+1) break } } return nil }