-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
page_fetch.go
44 lines (38 loc) · 1.32 KB
/
page_fetch.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
package handler
import (
"github.com/ArtalkJS/Artalk/internal/core"
"github.com/ArtalkJS/Artalk/internal/entity"
"github.com/ArtalkJS/Artalk/internal/i18n"
"github.com/ArtalkJS/Artalk/server/common"
"github.com/gofiber/fiber/v2"
)
type ResponsePageFetch struct {
entity.CookedPage
}
// @Id FetchPage
// @Summary Fetch Page Data
// @Description Fetch the data of a specific page
// @Tags Page
// @Security ApiKeyAuth
// @Param id path int true "The page ID you want to fetch"
// @Accept json
// @Produce json
// @Success 200 {object} ResponsePageFetch
// @Failure 404 {object} Map{msg=string}
// @Failure 500 {object} Map{msg=string}
// @Router /pages/{id}/fetch [post]
func PageFetch(app *core.App, router fiber.Router) {
router.Post("/pages/:id/fetch", common.AdminGuard(app, func(c *fiber.Ctx) error {
id, _ := c.ParamsInt("id")
page := app.Dao().FindPageByID(uint(id))
if page.IsEmpty() {
return common.RespError(c, 404, i18n.T("{{name}} not found", Map{"name": i18n.T("Page")}))
}
if err := app.Dao().FetchPageFromURL(&page); err != nil {
return common.RespError(c, 500, i18n.T("Page fetch failed")+": "+err.Error())
}
return common.RespData(c, ResponsePageFetch{
CookedPage: app.Dao().CookPage(&page),
})
}))
}