-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCatalogProductLink.go
77 lines (68 loc) · 2.24 KB
/
CatalogProductLink.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
// CatalogProductLink :
type CatalogProductLink struct {
LinkId uint16 `gorm:"column:link_id;primary_key" form:"link_id;primary_key" json:"link_id;primary_key"`
ProductId uint16 `gorm:"column:product_id" form:"product_id" json:"product_id"`
LinkedProductId uint16 `gorm:"column:linked_product_id" form:"linked_product_id" json:"linked_product_id"`
LinkTypeId uint16 `gorm:"column:link_type_id" form:"link_type_id" json:"link_type_id"`
}
// TableName :
func (*CatalogProductLink) TableName() string {
return "catalog_product_link"
}
// handler create
func initRoutersCatalogProductLink(r *gin.Engine, catalogproductlink string) {
route := r.Group("catalogproductlink")
route.GET("/", getCatalogProductLinks)
route.GET("/:id", getCatalogProductLink)
route.POST("/", createCatalogProductLink)
route.PUT("/:id", updateCatalogProductLink)
route.DELETE("/:id", deleteCatalogProductLink)
}
func getCatalogProductLinks(c *gin.Context) {
var catalogProductLinks []CatalogProductLink
if err := g.Find(&catalogProductLinks).Error; err != nil {
c.AbortWithStatus(404)
fmt.Println(err)
} else {
c.JSON(200, catalogProductLinks)
}
}
func getCatalogProductLink(c *gin.Context) {
id := c.Params.ByName("id")
var catalogProductLink CatalogProductLink
if err := g.Where("id = ?", id).First(&catalogProductLink).Error; err != nil {
c.AbortWithStatus(404)
fmt.Println(err)
} else {
c.JSON(200, catalogProductLink)
}
}
func createCatalogProductLink(c *gin.Context) {
var catalogProductLink CatalogProductLink
c.BindJSON(&catalogProductLink)
g.Create(&catalogProductLink)
c.JSON(200, catalogProductLink)
}
func updateCatalogProductLink(c *gin.Context) {
var catalogProductLink CatalogProductLink
id := c.Params.ByName("id")
if err := g.Where("id = ?", id).First(&catalogProductLink).Error; err != nil {
c.AbortWithStatus(404)
fmt.Println(err)
}
c.BindJSON(&catalogProductLink)
g.Save(&catalogProductLink)
c.JSON(200, catalogProductLink)
}
func deleteCatalogProductLink(c *gin.Context) {
id := c.Params.ByName("id")
var catalogProductLink CatalogProductLink
d := g.Where("id = ?", id).Delete(&catalogProductLink)
fmt.Println(d)
c.JSON(200, gin.H{"id #" + id: "deleted"})
}