add tag rep

This commit is contained in:
2025-03-03 14:10:04 +01:00
parent ec4a3b047f
commit cdfd77bc21
2 changed files with 123 additions and 0 deletions

View File

@@ -0,0 +1,83 @@
package repositories
import(
"strconv"
"gorm.io/gorm"
"example.com/gin/test/models"
)
type TagRepository interface {
Create(models.Tag) (models.Tag, error)
GetAll() ([]models.Tag, error)
GetById(string) (models.Tag, error)
//GetByShopItemId(string) (models.Tag, error)
Update(models.Tag) (models.Tag, error)
DeleteById(string) error
}
type GORMTagRepository struct {
DB *gorm.DB
}
func NewGORMTagRepository(db *gorm.DB) TagRepository {
return &GORMTagRepository{
DB: db,
}
}
func (t *GORMTagRepository) Create(tag models.Tag) (models.Tag, error) {
result := t.DB.Create(&tag)
if result.Error != nil {
return models.Tag{}, result.Error
}
return tag, nil
}
func (t *GORMTagRepository) GetAll() ([]models.Tag, error) {
var tags []models.Tag
result := t.DB.Find(&tags)
return tags, result.Error
}
func (t *GORMTagRepository) GetById(id string) (models.Tag, error) {
tagId, err := strconv.Atoi(id)
if err != nil {
return models.Tag{}, err
}
var tag models.Tag
result := t.DB.First(&tag, uint(tagId))
if result.Error != nil {
return models.Tag{}, result.Error
}
return tag, nil
}
func (t *GORMTagRepository) Update(tag models.Tag) (models.Tag, error) {
result := t.DB.Save(&tag)
if result.Error != nil {
return models.Tag{}, result.Error
}
return tag, nil
}
func (t *GORMTagRepository) DeleteById(id string) error {
tagId, err := strconv.Atoi(id)
if err != nil {
return err
}
result := t.DB.Delete(&models.Tag{}, tagId)
return result.Error
}