94 lines
3.6 KiB
Go
94 lines
3.6 KiB
Go
package gormstore
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"git.itzana.me/StrafesNET/dev-service/pkg/datastore"
|
|
"git.itzana.me/StrafesNET/dev-service/pkg/model"
|
|
"gorm.io/gorm"
|
|
"time"
|
|
)
|
|
|
|
// CreateApplication creates a new Application in the database.
|
|
func (g *Gormstore) CreateApplication(ctx context.Context, app *model.Application) error {
|
|
result := g.db.WithContext(ctx).Create(app)
|
|
return result.Error
|
|
}
|
|
|
|
// GetApplication retrieves a single Application by ID.
|
|
func (g *Gormstore) GetApplication(ctx context.Context, id uint32) (*model.Application, error) {
|
|
var application model.Application
|
|
result := g.db.WithContext(ctx).Preload("User").Preload("Permissions").First(&application, id)
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, datastore.ErrNotExists
|
|
}
|
|
return &application, result.Error
|
|
}
|
|
|
|
// GetApplicationByAPIKey retrieves a single Application by its API key.
|
|
func (g *Gormstore) GetApplicationByAPIKey(ctx context.Context, apiKey string) (*model.Application, error) {
|
|
var application model.Application
|
|
result := g.db.WithContext(ctx).
|
|
Where("api_key = ?", apiKey).
|
|
Preload("User").
|
|
Preload("Permissions").
|
|
First(&application)
|
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, datastore.ErrNotExists
|
|
}
|
|
return &application, result.Error
|
|
}
|
|
|
|
// GetAllApplications retrieves all Applications from the database.
|
|
func (g *Gormstore) GetAllApplications(ctx context.Context) ([]model.Application, error) {
|
|
var applications []model.Application
|
|
result := g.db.WithContext(ctx).Preload("User").Preload("Permissions").Find(&applications)
|
|
return applications, result.Error
|
|
}
|
|
|
|
// GetApplicationsForUser retrieves all Applications for a specific user.
|
|
func (g *Gormstore) GetApplicationsForUser(ctx context.Context, userID uint64) ([]model.Application, error) {
|
|
var applications []model.Application
|
|
result := g.db.WithContext(ctx).
|
|
Where("user_id = ?", userID).
|
|
Preload("Permissions").
|
|
Find(&applications)
|
|
return applications, result.Error
|
|
}
|
|
|
|
// UpdateApplication updates an existing Application in the database.
|
|
func (g *Gormstore) UpdateApplication(ctx context.Context, app *model.Application) error {
|
|
app.UpdatedAt = time.Now()
|
|
result := g.db.WithContext(ctx).Save(app)
|
|
return result.Error
|
|
}
|
|
|
|
// DeleteApplication deletes an Application by ID from the database.
|
|
func (g *Gormstore) DeleteApplication(ctx context.Context, id uint32) error {
|
|
result := g.db.WithContext(ctx).Delete(&model.Application{}, id)
|
|
if result.RowsAffected == 0 {
|
|
return datastore.ErrNotExists
|
|
}
|
|
return result.Error
|
|
}
|
|
|
|
// AddPermissionToApplication associates a Permission with an Application by their IDs.
|
|
func (g *Gormstore) AddPermissionToApplication(ctx context.Context, appID, permissionID uint32) error {
|
|
result := g.db.WithContext(ctx).Model(&model.Application{ID: appID}).Association("Permissions").Append(&model.Permission{ID: permissionID})
|
|
return result
|
|
}
|
|
|
|
// RemovePermissionFromApplication removes the association between a Permission and an Application by their IDs.
|
|
func (g *Gormstore) RemovePermissionFromApplication(ctx context.Context, appID, permissionID uint32) error {
|
|
result := g.db.WithContext(ctx).Model(&model.Application{ID: appID}).Association("Permissions").Delete(&model.Permission{ID: permissionID})
|
|
return result
|
|
}
|
|
|
|
// GetApplicationCountForUser returns the number of applications associated with a specific user.
|
|
func (g *Gormstore) GetApplicationCountForUser(ctx context.Context, userID uint64) (uint32, error) {
|
|
var count int64
|
|
result := g.db.WithContext(ctx).Model(&model.Application{}).Where("user_id = ?", userID).Count(&count)
|
|
return uint32(count), result.Error
|
|
}
|