add tag rep
This commit is contained in:
40
models/tag.go
Normal file
40
models/tag.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Tag struct {
|
||||||
|
gorm.Model
|
||||||
|
Name string `json:"name" binding:"required" gorm:"unique;not null"`
|
||||||
|
ShopItems []ShopItem `gorm:"many2many:item_tags;"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTag(ctx *gin.Context) (Tag, error) {
|
||||||
|
name := ctx.PostForm("name")
|
||||||
|
|
||||||
|
// Convert the price string to float64
|
||||||
|
tag := Tag{
|
||||||
|
Name: name,
|
||||||
|
}
|
||||||
|
|
||||||
|
if name == "" {
|
||||||
|
return Tag{}, fmt.Errorf("Name or description empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
return tag, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func NewTagByJson(ctx *gin.Context) (Tag, error) {
|
||||||
|
var tag Tag
|
||||||
|
err := ctx.ShouldBindJSON(&tag)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return Tag{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return tag, nil
|
||||||
|
}
|
||||||
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