add tag rep
This commit is contained in:
83
repositories/tagRepository.go
Normal file
83
repositories/tagRepository.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user