- Kompletna Go implementacija licencnog servera (19 Go fajlova) - Klijentski API: activate, deactivate, validate - Admin API: CRUD licence, stats, audit log - Admin dashboard: htmx + Go templates - RSA-2048 potpisivanje licencnih podataka - Rate limiting i API key autentifikacija - MySQL migracije i seed podaci (ESIR, ARV, LIGHT_TICKET) - Unit testovi: keygen, crypto, model, middleware (24 testa) - Dokumentacija: SPEC.md, ARCHITECTURE.md, SETUP.md, API.md, TESTING.md, README.md Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
86 lines
3.3 KiB
Go
86 lines
3.3 KiB
Go
package repository
|
|
|
|
import (
|
|
"dal-license-server/internal/model"
|
|
"database/sql"
|
|
"fmt"
|
|
)
|
|
|
|
type ActivationRepo struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewActivationRepo(db *sql.DB) *ActivationRepo {
|
|
return &ActivationRepo{db: db}
|
|
}
|
|
|
|
func (r *ActivationRepo) Create(a *model.Activation) (int64, error) {
|
|
res, err := r.db.Exec(`INSERT INTO activations (license_id, machine_fingerprint, hostname, os_info, app_version, ip_address)
|
|
VALUES (?, ?, ?, ?, ?, ?)`,
|
|
a.LicenseID, a.MachineFingerprint, a.Hostname, a.OSInfo, a.AppVersion, a.IPAddress)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("create activation: %w", err)
|
|
}
|
|
return res.LastInsertId()
|
|
}
|
|
|
|
func (r *ActivationRepo) GetActiveByLicense(licenseID int64) (*model.Activation, error) {
|
|
a := &model.Activation{}
|
|
err := r.db.QueryRow(`SELECT id, license_id, machine_fingerprint, hostname, os_info, app_version, ip_address, activated_at, deactivated_at, is_active, last_seen_at
|
|
FROM activations WHERE license_id = ? AND is_active = TRUE LIMIT 1`, licenseID).
|
|
Scan(&a.ID, &a.LicenseID, &a.MachineFingerprint, &a.Hostname, &a.OSInfo, &a.AppVersion, &a.IPAddress, &a.ActivatedAt, &a.DeactivatedAt, &a.IsActive, &a.LastSeenAt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
func (r *ActivationRepo) GetByLicenseAndFingerprint(licenseID int64, fingerprint string) (*model.Activation, error) {
|
|
a := &model.Activation{}
|
|
err := r.db.QueryRow(`SELECT id, license_id, machine_fingerprint, hostname, os_info, app_version, ip_address, activated_at, deactivated_at, is_active, last_seen_at
|
|
FROM activations WHERE license_id = ? AND machine_fingerprint = ? AND is_active = TRUE`, licenseID, fingerprint).
|
|
Scan(&a.ID, &a.LicenseID, &a.MachineFingerprint, &a.Hostname, &a.OSInfo, &a.AppVersion, &a.IPAddress, &a.ActivatedAt, &a.DeactivatedAt, &a.IsActive, &a.LastSeenAt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
func (r *ActivationRepo) ListByLicense(licenseID int64) ([]model.Activation, error) {
|
|
rows, err := r.db.Query(`SELECT id, license_id, machine_fingerprint, hostname, os_info, app_version, ip_address, activated_at, deactivated_at, is_active, last_seen_at
|
|
FROM activations WHERE license_id = ? ORDER BY activated_at DESC`, licenseID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var acts []model.Activation
|
|
for rows.Next() {
|
|
var a model.Activation
|
|
rows.Scan(&a.ID, &a.LicenseID, &a.MachineFingerprint, &a.Hostname, &a.OSInfo, &a.AppVersion, &a.IPAddress, &a.ActivatedAt, &a.DeactivatedAt, &a.IsActive, &a.LastSeenAt)
|
|
acts = append(acts, a)
|
|
}
|
|
return acts, nil
|
|
}
|
|
|
|
func (r *ActivationRepo) Deactivate(licenseID int64, fingerprint string) error {
|
|
_, err := r.db.Exec(`UPDATE activations SET is_active = FALSE, deactivated_at = NOW() WHERE license_id = ? AND machine_fingerprint = ? AND is_active = TRUE`,
|
|
licenseID, fingerprint)
|
|
return err
|
|
}
|
|
|
|
func (r *ActivationRepo) ForceRelease(licenseID int64) error {
|
|
_, err := r.db.Exec(`UPDATE activations SET is_active = FALSE, deactivated_at = NOW() WHERE license_id = ? AND is_active = TRUE`, licenseID)
|
|
return err
|
|
}
|
|
|
|
func (r *ActivationRepo) UpdateLastSeen(id int64) {
|
|
r.db.Exec("UPDATE activations SET last_seen_at = NOW() WHERE id = ?", id)
|
|
}
|
|
|
|
func (r *ActivationRepo) CountActive() (int, error) {
|
|
var count int
|
|
err := r.db.QueryRow("SELECT COUNT(*) FROM activations WHERE is_active = TRUE").Scan(&count)
|
|
return count, err
|
|
}
|