-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookmark.go
110 lines (86 loc) · 2.96 KB
/
bookmark.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package controllers
import (
"net/http"
"strconv"
// log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"twreporter.org/go-api/models"
)
// GetBookmarksOfAUser given userID this func will list all the bookmarks belongs to the user
func (mc *MembershipController) GetBookmarksOfAUser(c *gin.Context) (int, gin.H, error) {
var err error
var bookmarks []models.Bookmark
var bookmark models.Bookmark
var total int
// get userID according to the url param
userID := c.Param("userID")
// get bookmarkSlug in url param
bookmarkSlug := c.Param("bookmarkSlug")
// Get a specific bookmark from a user
if bookmarkSlug != "" {
host := c.Query("host")
if bookmark, err = mc.Storage.GetABookmarkOfAUser(userID, bookmarkSlug, host); err != nil {
return 0, gin.H{}, err
}
return http.StatusOK, gin.H{"status": "ok", "record": bookmark}, nil
}
_limit := c.Query("limit")
_offset := c.Query("offset")
limit, _ := strconv.Atoi(_limit)
offset, _ := strconv.Atoi(_offset)
if limit == 0 {
limit = 10
}
if bookmarks, total, err = mc.Storage.GetBookmarksOfAUser(userID, limit, offset); err != nil {
return 0, gin.H{}, err
}
// TODO The response JSON should be like
// {
// "status": "success",
// "data": {
// "meta": meta,
// "records": bookmarks
// }
// }
return http.StatusOK, gin.H{"status": "ok", "records": bookmarks, "meta": models.MetaOfResponse{
Total: total,
Offset: offset,
Limit: limit,
}}, nil
}
// DeleteABookmarkOfAUser given userID and bookmarkHref, this func will remove the relationship between user and bookmark
func (mc *MembershipController) DeleteABookmarkOfAUser(c *gin.Context) (int, gin.H, error) {
bookmarkID := c.Param("bookmarkID")
userID := c.Param("userID")
if err := mc.Storage.DeleteABookmarkOfAUser(userID, bookmarkID); err != nil {
return 0, gin.H{}, err
}
return http.StatusNoContent, gin.H{}, nil
}
// CreateABookmarkOfAUser given userID and bookmark POST body, this func will try to create bookmark record in the bookmarks table,
// and build the relationship between bookmark and user
func (mc *MembershipController) CreateABookmarkOfAUser(c *gin.Context) (int, gin.H, error) {
var bookmark models.Bookmark
var err error
userID := c.Param("userID")
if bookmark, err = mc.parseBookmarkPOSTBody(c); err != nil {
return 0, gin.H{}, err
}
if bookmark, err = mc.Storage.CreateABookmarkOfAUser(userID, bookmark); err != nil {
return 0, gin.H{}, err
}
// TODO The response JSON should be like
// {
// "status": "success",
// "data": bookmark
// }
return http.StatusCreated, gin.H{"status": "ok", "record": bookmark}, nil
}
func (mc *MembershipController) parseBookmarkPOSTBody(c *gin.Context) (models.Bookmark, error) {
var err error
var bm models.Bookmark
if err = c.Bind(&bm); err != nil {
return models.Bookmark{}, models.NewAppError("MembershipController.parseBookmarkPOSTBody", "POST body is neither JSON nor x-www-form-urlencoded", err.Error(), http.StatusBadRequest)
}
return bm, nil
}