48 lines
1.5 KiB
Go
48 lines
1.5 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"
|
|
)
|
|
|
|
// CreateRateLimit creates a new RateLimit in the database.
|
|
func (g *Gormstore) CreateRateLimit(ctx context.Context, rl *model.RateLimit) error {
|
|
result := g.db.WithContext(ctx).Create(rl)
|
|
return result.Error
|
|
}
|
|
|
|
// GetRateLimit retrieves a single RateLimit by ID.
|
|
func (g *Gormstore) GetRateLimit(ctx context.Context, id uint32) (*model.RateLimit, error) {
|
|
var rateLimit model.RateLimit
|
|
result := g.db.WithContext(ctx).First(&rateLimit, id)
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, datastore.ErrNotExists
|
|
}
|
|
return &rateLimit, result.Error
|
|
}
|
|
|
|
// GetAllRateLimits retrieves all RateLimits from the database.
|
|
func (g *Gormstore) GetAllRateLimits(ctx context.Context) ([]model.RateLimit, error) {
|
|
var rateLimits []model.RateLimit
|
|
result := g.db.WithContext(ctx).Find(&rateLimits)
|
|
return rateLimits, result.Error
|
|
}
|
|
|
|
// UpdateRateLimit updates an existing RateLimit in the database.
|
|
func (g *Gormstore) UpdateRateLimit(ctx context.Context, rl *model.RateLimit) error {
|
|
result := g.db.WithContext(ctx).Save(rl)
|
|
return result.Error
|
|
}
|
|
|
|
// DeleteRateLimit deletes a RateLimit by ID from the database.
|
|
func (g *Gormstore) DeleteRateLimit(ctx context.Context, id uint32) error {
|
|
result := g.db.WithContext(ctx).Delete(&model.RateLimit{}, id)
|
|
if result.RowsAffected == 0 {
|
|
return datastore.ErrNotExists
|
|
}
|
|
return result.Error
|
|
}
|