Files
artalkjs/server/handler/site_create.go
qwqcode cf7725edba refactor(go): bump the module version and support go install (#979)
BREAKING CHANGE: The original Go module name was `github.com/ArtalkJS/Artalk`, which has now been modified to `github.com/artalkjs/artalk/v2` (all lowercase). This change may affect the CI pipeline or other areas if you are compiling the Artalk server and have specified the fixed Go module name. Regarding the original `-ldflags -X github.com/ArtalkJS/Artalk/internal/config.Version` and `CommitHash` definitions, please remove them, as they are now obsolete. The `Version` has been hardcoded into the Go code, and the `CommitHash` is now read from the Go build info via runtime/debug’s vcs.revision value. This change only affects the server developers and does not impact the frontend UI client.
2024-09-18 21:43:43 +08:00

68 lines
1.9 KiB
Go

package handler
import (
"strings"
"github.com/artalkjs/artalk/v2/internal/core"
"github.com/artalkjs/artalk/v2/internal/entity"
"github.com/artalkjs/artalk/v2/internal/i18n"
"github.com/artalkjs/artalk/v2/internal/utils"
"github.com/artalkjs/artalk/v2/server/common"
"github.com/gofiber/fiber/v2"
)
type ParamsSiteCreate struct {
Name string `json:"name" validate:"required"` // The site name
Urls []string `json:"urls" validate:"required"` // The site urls
}
type ResponseSiteCreate struct {
entity.CookedSite
}
// @Id CreateSite
// @Summary Create Site
// @Description Create a new site
// @Tags Site
// @Security ApiKeyAuth
// @Param site body ParamsSiteCreate true "The site data"
// @Accept json
// @Produce json
// @Success 200 {object} ResponseSiteCreate
// @Failure 400 {object} Map{msg=string}
// @Failure 403 {object} Map{msg=string}
// @Failure 500 {object} Map{msg=string}
// @Router /sites [post]
func SiteCreate(app *core.App, router fiber.Router) {
router.Post("/sites", common.AdminGuard(app, func(c *fiber.Ctx) error {
var p ParamsSiteCreate
if isOK, resp := common.ParamsDecode(c, &p); !isOK {
return resp
}
if len(p.Urls) > 0 {
for _, url := range p.Urls {
if !utils.ValidateURL(url) {
return common.RespError(c, 400, i18n.T("Contains invalid URL"))
}
}
}
if !app.Dao().FindSite(p.Name).IsEmpty() {
return common.RespError(c, 400, i18n.T("{{name}} already exists", Map{"name": i18n.T("Site")}))
}
site := entity.Site{}
site.Name = p.Name
site.Urls = strings.Join(p.Urls, ",")
err := app.Dao().CreateSite(&site)
if err != nil {
return common.RespError(c, 500, i18n.T("{{name}} creation failed", Map{"name": i18n.T("Site")}))
}
return common.RespData(c, ResponseSiteCreate{
CookedSite: app.Dao().CookSite(&site),
})
}))
}