@@ -2,22 +2,51 @@ package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
type Tag struct {
|
||||
gorm.Model
|
||||
Name string `json:"name" binding:"required" gorm:"not null"`
|
||||
Name string `json:"name" binding:"required" gorm:"not null"`
|
||||
Color string `json:"color" binding:"required" gorm:"default:pink"`
|
||||
ShopItems []ShopItem `gorm:"many2many:item_tags;"`
|
||||
}
|
||||
|
||||
func NewTag(ctx *gin.Context) (Tag, error) {
|
||||
colors := []string{
|
||||
"red",
|
||||
"orange",
|
||||
"amber",
|
||||
"yellow",
|
||||
"lime",
|
||||
"green",
|
||||
"emerald",
|
||||
"teal",
|
||||
"cyan",
|
||||
"sky",
|
||||
"blue",
|
||||
"indigo",
|
||||
"violet",
|
||||
"purple",
|
||||
"fuchsia",
|
||||
"pink",
|
||||
"rose",
|
||||
"slate",
|
||||
"gray",
|
||||
"zinc",
|
||||
"neutral",
|
||||
"stone",
|
||||
}
|
||||
n := rand.Int() % len(colors)
|
||||
|
||||
name := ctx.PostForm("name")
|
||||
|
||||
// Convert the price string to float64
|
||||
// Convert the price string to float64
|
||||
tag := Tag{
|
||||
Name: name,
|
||||
Name: name,
|
||||
Color: colors[n],
|
||||
}
|
||||
|
||||
if name == "" {
|
||||
@@ -27,14 +56,13 @@ func NewTag(ctx *gin.Context) (Tag, error) {
|
||||
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