2024-02-02 15:42:42 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-02-14 14:03:45 +00:00
|
|
|
"errors"
|
2024-07-07 04:35:46 +00:00
|
|
|
"strings"
|
2024-02-14 14:03:45 +00:00
|
|
|
|
2024-06-22 09:29:53 +00:00
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
2024-02-02 15:42:42 +00:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2024-02-14 14:03:45 +00:00
|
|
|
func ListCategory() ([]models.Category, error) {
|
|
|
|
var categories []models.Category
|
|
|
|
err := database.C.Find(&categories).Error
|
|
|
|
|
|
|
|
return categories, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetCategory(alias string) (models.Category, error) {
|
2024-02-02 15:42:42 +00:00
|
|
|
var category models.Category
|
|
|
|
if err := database.C.Where(models.Category{Alias: alias}).First(&category).Error; err != nil {
|
|
|
|
return category, err
|
|
|
|
}
|
|
|
|
return category, nil
|
|
|
|
}
|
|
|
|
|
2024-02-14 14:03:45 +00:00
|
|
|
func GetCategoryWithID(id uint) (models.Category, error) {
|
|
|
|
var category models.Category
|
|
|
|
if err := database.C.Where(models.Category{
|
|
|
|
BaseModel: models.BaseModel{ID: id},
|
|
|
|
}).First(&category).Error; err != nil {
|
|
|
|
return category, err
|
|
|
|
}
|
|
|
|
return category, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCategory(alias, name, description string) (models.Category, error) {
|
|
|
|
category := models.Category{
|
|
|
|
Alias: alias,
|
|
|
|
Name: name,
|
|
|
|
Description: description,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := database.C.Save(&category).Error
|
|
|
|
|
|
|
|
return category, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func EditCategory(category models.Category, alias, name, description string) (models.Category, error) {
|
|
|
|
category.Alias = alias
|
|
|
|
category.Name = name
|
|
|
|
category.Description = description
|
|
|
|
|
|
|
|
err := database.C.Save(&category).Error
|
|
|
|
|
|
|
|
return category, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteCategory(category models.Category) error {
|
|
|
|
return database.C.Delete(category).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetTagOrCreate(alias, name string) (models.Tag, error) {
|
2024-07-07 04:35:46 +00:00
|
|
|
alias = strings.ToLower(alias)
|
2024-02-02 15:42:42 +00:00
|
|
|
var tag models.Tag
|
|
|
|
if err := database.C.Where(models.Category{Alias: alias}).First(&tag).Error; err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
tag = models.Tag{
|
|
|
|
Alias: alias,
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
err := database.C.Save(&tag).Error
|
|
|
|
return tag, err
|
|
|
|
}
|
|
|
|
return tag, err
|
|
|
|
}
|
|
|
|
return tag, nil
|
|
|
|
}
|