feat: add api
docker-publish / publish (push) Failing after 14m14s

This commit is contained in:
Nuxgrid
2026-07-27 16:06:19 +02:00
parent 8397a4470f
commit 6b4274d12b
46 changed files with 3922 additions and 0 deletions
+70
View File
@@ -0,0 +1,70 @@
package demos
import (
"errors"
"gorm.io/gorm"
)
// Store : persistance des démos.
type Store interface {
Create(d Demo) (Demo, error)
Get(id string) (Demo, bool)
List() ([]Demo, error)
ListByUsername(username string) ([]Demo, error)
Update(d Demo) (Demo, error)
CountActive() (int, error)
}
// GormStore : Store adossé à PostgreSQL via GORM.
type GormStore struct{ db *gorm.DB }
func NewGormStore(db *gorm.DB) *GormStore { return &GormStore{db: db} }
func (s *GormStore) Create(d Demo) (Demo, error) {
if err := s.db.Create(&d).Error; err != nil {
return Demo{}, err
}
return d, nil
}
func (s *GormStore) Get(id string) (Demo, bool) {
var d Demo
err := s.db.Where("id = ?", id).First(&d).Error
if errors.Is(err, gorm.ErrRecordNotFound) || err != nil {
return Demo{}, false
}
return d, true
}
func (s *GormStore) List() ([]Demo, error) {
var out []Demo
if err := s.db.Order("created_at DESC").Find(&out).Error; err != nil {
return nil, err
}
return out, nil
}
func (s *GormStore) ListByUsername(username string) ([]Demo, error) {
var out []Demo
if err := s.db.Where("username = ?", username).Order("created_at DESC").Find(&out).Error; err != nil {
return nil, err
}
return out, nil
}
func (s *GormStore) Update(d Demo) (Demo, error) {
if err := s.db.Save(&d).Error; err != nil {
return Demo{}, err
}
return d, nil
}
// CountActive compte les démos occupant de la capacité.
func (s *GormStore) CountActive() (int, error) {
var n int64
err := s.db.Model(&Demo{}).
Where("status IN ?", []Status{StatusPending, StatusProvisioning, StatusReady, StatusExpiring}).
Count(&n).Error
return int(n), err
}