add ui configurable config options
This commit is contained in:
81
repositories/configRepository.go
Normal file
81
repositories/configRepository.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"git.dynamicdiscord.de/kalipso/zineshop/models"
|
||||
)
|
||||
|
||||
type ConfigRepository interface {
|
||||
Create(models.Config) (models.Config, error)
|
||||
GetAll() ([]models.Config, error)
|
||||
GetById(string) (models.Config, error)
|
||||
Update(models.Config) (models.Config, error)
|
||||
DeleteById(string) error
|
||||
}
|
||||
|
||||
type GORMConfigRepository struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func NewGORMConfigRepository(db *gorm.DB) ConfigRepository {
|
||||
return &GORMConfigRepository{
|
||||
DB: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *GORMConfigRepository) Create(config models.Config) (models.Config, error) {
|
||||
result := t.DB.Create(&config)
|
||||
|
||||
if result.Error != nil {
|
||||
return models.Config{}, result.Error
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func (t *GORMConfigRepository) GetAll() ([]models.Config, error) {
|
||||
var configs []models.Config
|
||||
result := t.DB.Find(&configs)
|
||||
|
||||
return configs, result.Error
|
||||
}
|
||||
|
||||
func (t *GORMConfigRepository) GetById(id string) (models.Config, error) {
|
||||
configId, err := strconv.Atoi(id)
|
||||
|
||||
if err != nil {
|
||||
return models.Config{}, err
|
||||
}
|
||||
|
||||
var config models.Config
|
||||
result := t.DB.First(&config, uint(configId))
|
||||
|
||||
if result.Error != nil {
|
||||
return models.Config{}, result.Error
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func (t *GORMConfigRepository) Update(config models.Config) (models.Config, error) {
|
||||
result := t.DB.Save(&config)
|
||||
if result.Error != nil {
|
||||
return models.Config{}, result.Error
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func (t *GORMConfigRepository) DeleteById(id string) error {
|
||||
configId, err := strconv.Atoi(id)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
result := t.DB.Delete(&models.Config{}, configId)
|
||||
return result.Error
|
||||
}
|
||||
@@ -9,12 +9,13 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ShopItems ShopItemRepository
|
||||
Users UserRepository
|
||||
Tags TagRepository
|
||||
CartItems CartItemRepository
|
||||
Orders OrderRepository
|
||||
Tokens RegisterTokenRepository
|
||||
ShopItems ShopItemRepository
|
||||
Users UserRepository
|
||||
Tags TagRepository
|
||||
CartItems CartItemRepository
|
||||
Orders OrderRepository
|
||||
Tokens RegisterTokenRepository
|
||||
ConfigOptions ConfigRepository
|
||||
)
|
||||
|
||||
func InitRepositories() {
|
||||
@@ -29,6 +30,8 @@ func InitRepositories() {
|
||||
&models.Tag{},
|
||||
&models.CartItem{},
|
||||
&models.Order{},
|
||||
&models.Config{},
|
||||
&models.Paper{},
|
||||
&models.RegisterToken{})
|
||||
|
||||
if err != nil {
|
||||
@@ -41,4 +44,5 @@ func InitRepositories() {
|
||||
CartItems = NewGORMCartItemRepository(db)
|
||||
Orders = NewGORMOrderRepository(db)
|
||||
Tokens = NewGORMRegisterTokenRepository(db)
|
||||
ConfigOptions = NewGORMConfigRepository(db)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user