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
|
||||
}
|
||||
Reference in New Issue
Block a user