Interactive/pkg/internal/services/categories.go

90 lines
2.1 KiB
Go
Raw Permalink Normal View History

2024-02-02 15:42:42 +00:00
package services
import (
2024-02-14 14:03:45 +00:00
"errors"
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
"strings"
2024-02-14 14:03:45 +00:00
2024-11-02 05:41:51 +00:00
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
"git.solsynth.dev/hypernet/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: cruda.BaseModel{ID: id},
2024-02-14 14:03:45 +00:00
}).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
}
2024-09-16 16:12:09 +00:00
func GetTagWithID(id uint) (models.Tag, error) {
var tag models.Tag
if err := database.C.Where(models.Tag{
BaseModel: cruda.BaseModel{ID: id},
2024-09-16 16:12:09 +00:00
}).First(&tag).Error; err != nil {
return tag, err
}
return tag, nil
}
2024-02-14 14:03:45 +00:00
func GetTagOrCreate(alias, name string) (models.Tag, error) {
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
}