-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
59 lines (51 loc) · 1.39 KB
/
api.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
package app
import (
"net/http"
"github.com/crispgm/read-track/internal/model"
"github.com/gin-gonic/gin"
)
// AddParams .
type AddParams struct {
Token string `form:"token" binding:"required"`
Title string `form:"title" binding:"required"`
URL string `form:"url" binding:"required,url"`
Type string `form:"type" binding:"required,oneof=read unread skip skim"`
Author string `form:"author"`
Description string `form:"description"`
Device string `form:"device"`
}
// Add implementation of adding an article
func (app Application) Add(c *gin.Context) {
resp := NewResponse()
var params AddParams
err := c.BindQuery(¶ms)
if err != nil {
c.JSON(http.StatusOK, NewError(ErrCodeParams, err).Response())
return
}
if params.Token != app.Conf().HTTP.Token {
c.JSON(http.StatusAccepted, ErrInvalidAccessToken.Response())
return
}
article := &model.Article{
Title: params.Title,
URL: params.URL,
Description: trimDefault(params.Description),
Author: trimDefault(params.Author),
Device: trimDefault(params.Device),
ReadType: params.Type,
}
err = model.CreateArticle(app.DB(), article)
if err != nil {
c.JSON(http.StatusOK, NewError(ErrCodeDBFailed, err).Response())
return
}
resp.Data = article
c.JSON(http.StatusOK, resp)
}
func trimDefault(input string) string {
if input == "-" {
return ""
}
return input
}