-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter.go
82 lines (74 loc) · 1.67 KB
/
chapter.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
package routers
import (
"net/http"
"strconv"
"github.com/fancxxy/gocomic/backend/config"
"github.com/fancxxy/gocomic/backend/models"
"github.com/fancxxy/gocomic/backend/rpc"
"github.com/gin-gonic/gin"
)
// @Summary Get the chapter
// @Produce json
// @Param id path int true "comic id"
// @Param cid path int true "chapter index"
// @Success 200 {object} routers.Response
// @Success 200 {object} routers.Response
// @Failure 400 {object} routers.Response
// @Failure 404 {object} routers.Response
// @Router /api/v1/comics/{id}/{cid} [get]
func chapter(c *gin.Context) {
idParam := c.Param("id")
id, err := strconv.Atoi(idParam)
if err != nil {
c.JSON(http.StatusBadRequest, Response{
Code: 400,
Msg: "invalid id parameter",
Data: nil,
})
return
}
cidParam := c.Param("cid")
cid, err := strconv.Atoi(cidParam)
if err != nil {
c.JSON(http.StatusBadRequest, Response{
Code: 400,
Msg: "invalid cid parameter",
Data: nil,
})
return
}
chapter := models.GetChapter(id, cid)
if chapter == nil {
c.JSON(http.StatusNotFound, Response{
Code: 404,
Msg: "cannot find the chapter",
Data: nil,
})
return
}
if chapter.Cached {
c.JSON(http.StatusOK, Response{
Code: 200,
Msg: "get the chapter succeed",
Data: struct {
*models.Chapter `json:"chapter"`
}{chapter},
})
return
}
response, err := rpc.DownloadChapter(chapter.URL, config.Config.Download.Path)
if err != nil {
c.JSON(http.StatusBadRequest, Response{
Code: 400,
Msg: err.Error(),
Data: nil,
})
return
}
chapter.Save(response)
c.JSON(http.StatusOK, Response{
Code: 200,
Msg: "get the chapter succeed",
Data: chapterData{Chapter: chapter},
})
}