82 lines
1.6 KiB
Go
82 lines
1.6 KiB
Go
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
|
|
}
|