2023-09-08 16:57:44 +02:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-12-10 19:31:38 +01:00
|
|
|
"time"
|
2023-09-08 16:57:44 +02:00
|
|
|
)
|
|
|
|
|
2023-12-10 19:31:38 +01:00
|
|
|
type Device struct {
|
|
|
|
UserId string `json:"user_id"`
|
|
|
|
DeviceId string `json:"device_id"`
|
|
|
|
// The IP address that was used to register the device. Recorded so
|
|
|
|
// that I can count how many people are using hishtory and roughly
|
|
|
|
// from where. If you would like this deleted, please email me at
|
|
|
|
// david@daviddworken.com and I can clear it from your device entries.
|
|
|
|
RegistrationIp string `json:"registration_ip"`
|
|
|
|
RegistrationDate time.Time `json:"registration_date"`
|
|
|
|
// Test devices, that should be aggressively cleaned from the DB
|
|
|
|
IsIntegrationTestDevice bool `json:"is_integration_test_device"`
|
|
|
|
// Whether this device was uninstalled
|
|
|
|
UninstallDate time.Time `json:"uninstall_date"`
|
|
|
|
}
|
|
|
|
|
2023-09-12 17:30:43 +02:00
|
|
|
func (db *DB) CountAllDevices(ctx context.Context) (int64, error) {
|
|
|
|
var numDevices int64 = 0
|
2023-12-10 19:31:38 +01:00
|
|
|
tx := db.WithContext(ctx).Model(&Device{}).Count(&numDevices)
|
2023-09-08 16:57:44 +02:00
|
|
|
if tx.Error != nil {
|
|
|
|
return 0, fmt.Errorf("tx.Error: %w", tx.Error)
|
|
|
|
}
|
|
|
|
|
2023-09-12 17:30:43 +02:00
|
|
|
return numDevices, nil
|
2023-09-08 16:57:44 +02:00
|
|
|
}
|
|
|
|
|
2023-09-12 17:30:43 +02:00
|
|
|
func (db *DB) CountDevicesForUser(ctx context.Context, userID string) (int64, error) {
|
|
|
|
var existingDevicesCount int64
|
2023-12-10 19:31:38 +01:00
|
|
|
tx := db.WithContext(ctx).Model(&Device{}).Where("user_id = ?", userID).Count(&existingDevicesCount)
|
2023-09-08 16:57:44 +02:00
|
|
|
if tx.Error != nil {
|
|
|
|
return 0, fmt.Errorf("tx.Error: %w", tx.Error)
|
|
|
|
}
|
|
|
|
|
2023-09-12 17:30:43 +02:00
|
|
|
return existingDevicesCount, nil
|
2023-09-08 16:57:44 +02:00
|
|
|
}
|
|
|
|
|
2023-12-10 19:31:38 +01:00
|
|
|
func (db *DB) CreateDevice(ctx context.Context, device *Device) error {
|
2023-09-08 16:57:44 +02:00
|
|
|
tx := db.WithContext(ctx).Create(device)
|
|
|
|
if tx.Error != nil {
|
|
|
|
return fmt.Errorf("tx.Error: %w", tx.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-10 19:31:38 +01:00
|
|
|
func (db *DB) DevicesForUser(ctx context.Context, userID string) ([]*Device, error) {
|
|
|
|
var devices []*Device
|
2023-09-08 16:57:44 +02:00
|
|
|
tx := db.WithContext(ctx).Where("user_id = ?", userID).Find(&devices)
|
|
|
|
if tx.Error != nil {
|
|
|
|
return nil, fmt.Errorf("tx.Error: %w", tx.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return devices, nil
|
|
|
|
}
|