/
c-friend-links.go
80 lines (71 loc) · 1.72 KB
/
c-friend-links.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
78
79
80
package site
import (
"net/http"
"github.com/gin-gonic/gin"
)
func (p *Plugin) indexFriendLinks(c *gin.Context) error {
var items []FriendLink
if err := p.Db.Order("updated_at DESC").Find(&items).Error; err != nil {
return err
}
c.JSON(http.StatusOK, items)
return nil
}
type fmFriendLink struct {
Title string `json:"title" binding:"required,max=255"`
Home string `json:"home" binding:"required,max=255"`
Logo string `json:"logo" binding:"required,max=255"`
SortOrder int `json:"sortOrder"`
}
func (p *Plugin) createFriendLink(c *gin.Context) error {
var fm fmFriendLink
if err := c.BindJSON(&fm); err != nil {
return err
}
item := FriendLink{
Title: fm.Title,
Logo: fm.Logo,
Home: fm.Home,
SortOrder: fm.SortOrder,
}
if err := p.Db.Create(&item).Error; err != nil {
return err
}
c.JSON(http.StatusOK, item)
return nil
}
func (p *Plugin) showFriendLink(c *gin.Context) error {
var item FriendLink
if err := p.Db.Where("id = ?", c.Param("id")).First(&item).Error; err != nil {
return err
}
c.JSON(http.StatusOK, item)
return nil
}
func (p *Plugin) updateFriendLink(c *gin.Context) error {
var fm fmFriendLink
if err := c.BindJSON(&fm); err != nil {
return err
}
if err := p.Db.Model(&FriendLink{}).
Where("id = ?", c.Param("id")).
Updates(map[string]interface{}{
"home": fm.Home,
"title": fm.Title,
"logo": fm.Logo,
"sort_order": fm.SortOrder,
}).Error; err != nil {
return err
}
c.JSON(http.StatusOK, gin.H{})
return nil
}
func (p *Plugin) destroyFriendLink(c *gin.Context) error {
if err := p.Db.
Where("id = ?", c.Param("id")).
Delete(FriendLink{}).Error; err != nil {
return err
}
c.JSON(http.StatusOK, gin.H{})
return nil
}