Files
artalkjs/server/handler/site_update.go
T
qwqcode 589850d311 refactor(api): refactor HTTP API to version 2 (#735)
* refactor(api): refactor HTTP API to version 2

* refactor: remove forced site_origin check

* refactor: update http client

* fix

* add more api description and error code

* reduce nestings in response structures

* separate unread

* refactor(api/comments): get comments

* refactor: frontend api module

* chore: remove vite temporary file

* chore: restore swagger file

* update

* 移除为管理员分配站点名的站点隔离功能,简化实现

* refactor pagesAllFetch

* fix ci

* remove useless codes

* limiter protected paths

* add test for version endpoint

* rename handler code files

* code file separate

* fix some bug of interacting with backend

* add fetch error class for handling

* fix and rename

* refactor: checker support both `Promise` and callback fn

* fix new checker captcha iframe url

* rename and add swagger id field

* replace tab with spaces in go comment

* chore: refine

* complete the validate tag for request fields and enable `--defaultRequired`

90% of the request fields of our API are required,100% of response fields in our API are required.
Each field add validate:"required" is easy to miss.

* replace original api client with auto-generated codes

* artalk pkg apply new api client

* artalk-sidebar pkg apply new api client

* fix issues

* fix sidebar locale conf and refactor user login

* fix and refactor captcha and limiter

* fix ui testing

* add script to build http client by swagger.json
2024-01-24 21:33:30 +08:00

105 lines
2.9 KiB
Go

package handler
import (
"strings"
"github.com/ArtalkJS/Artalk/internal/core"
"github.com/ArtalkJS/Artalk/internal/dao"
"github.com/ArtalkJS/Artalk/internal/entity"
"github.com/ArtalkJS/Artalk/internal/i18n"
"github.com/ArtalkJS/Artalk/internal/utils"
"github.com/ArtalkJS/Artalk/server/common"
"github.com/gofiber/fiber/v2"
)
type ParamsSiteUpdate struct {
Name string `json:"name" validate:"required"` // Updated site name
Urls []string `json:"urls" validate:"required"` // Updated site urls
}
type ResponseSiteUpdate struct {
entity.CookedSite
}
// @Id UpdateSite
// @Summary Update Site
// @Description Update a specific site
// @Tags Site
// @Security ApiKeyAuth
// @Param id path int true "The site ID you want to update"
// @Param site body ParamsSiteUpdate true "The site data"
// @Accept json
// @Produce json
// @Success 200 {object} ResponseSiteUpdate
// @Router /sites/{id} [put]
func SiteUpdate(app *core.App, router fiber.Router) {
router.Put("/sites/:id", common.AdminGuard(app, func(c *fiber.Ctx) error {
id, _ := c.ParamsInt("id")
var p ParamsSiteUpdate
if isOK, resp := common.ParamsDecode(c, &p); !isOK {
return resp
}
site := app.Dao().FindSiteByID(uint(id))
if site.IsEmpty() {
return common.RespError(c, 404, i18n.T("{{name}} not found", Map{"name": i18n.T("Site")}))
}
if strings.TrimSpace(p.Name) == "" {
return common.RespError(c, 400, i18n.T("{{name}} cannot be empty", Map{"name": "name"}))
}
// 重命名合法性检测
modifyName := p.Name != site.Name
if modifyName && !app.Dao().FindSite(p.Name).IsEmpty() {
return common.RespError(c, 400, i18n.T("{{name}} already exists", Map{"name": i18n.T("Site")}))
}
// urls 合法性检测
if len(p.Urls) > 0 {
for _, url := range p.Urls {
if !utils.ValidateURL(url) {
return common.RespError(c, 400, i18n.T("Contains invalid URL"))
}
}
}
// 预先删除缓存,防止修改主键原有 site_name 占用问题
app.Dao().CacheAction(func(cache *dao.DaoCache) {
cache.SiteCacheDel(&site)
})
// 同步变更 site_name
if modifyName {
var comments []entity.Comment
var pages []entity.Page
app.Dao().DB().Where("site_name = ?", site.Name).Find(&comments)
app.Dao().DB().Where("site_name = ?", site.Name).Find(&pages)
for _, comment := range comments {
comment.SiteName = p.Name
app.Dao().UpdateComment(&comment)
}
for _, page := range pages {
page.SiteName = p.Name
app.Dao().UpdatePage(&page)
}
}
// 修改 site
site.Name = p.Name
site.Urls = strings.Join(p.Urls, ",")
err := app.Dao().UpdateSite(&site)
if err != nil {
return common.RespError(c, 500, i18n.T("{{name}} save failed", Map{"name": i18n.T("Site")}))
}
return common.RespData(c, ResponseSiteUpdate{
CookedSite: app.Dao().CookSite(&site),
})
}))
}