Skip to content

Commit

Permalink
feat: add Comment Deletion Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Redish101 committed Aug 14, 2023
1 parent fb1a5c5 commit 706e49f
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 26 deletions.
Empty file added README.en.md
Empty file.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Retalk

> 一个快速的, 便捷的自托管评论系统
这里没什么好看的, 是我用来测试ci水的提交()
42 changes: 42 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,40 @@ const docTemplate = `{
}
}
},
"/api/comment/delete": {
"delete": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "根据ID删除评论",
"tags": [
"评论"
],
"summary": "根据ID删除评论",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/common.Resp"
}
},
"403": {
"description": "Forbidden",
"schema": {
"$ref": "#/definitions/common.Resp"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/common.Resp"
}
}
}
}
},
"/api/comment/getAll": {
"get": {
"description": "获取所有评论",
Expand Down Expand Up @@ -340,6 +374,14 @@ const docTemplate = `{
}
}
}
},
"securityDefinitions": {
"ApiKeyAuth": {
"description": "\"将 \"Bearer TOKEN\" 设置为初始化时设置的 API 密钥\"",
"type": "apiKey",
"name": "Authorization",
"in": "header"
}
}
}`

Expand Down
42 changes: 42 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,40 @@
}
}
},
"/api/comment/delete": {
"delete": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "根据ID删除评论",
"tags": [
"评论"
],
"summary": "根据ID删除评论",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/common.Resp"
}
},
"403": {
"description": "Forbidden",
"schema": {
"$ref": "#/definitions/common.Resp"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/common.Resp"
}
}
}
}
},
"/api/comment/getAll": {
"get": {
"description": "获取所有评论",
Expand Down Expand Up @@ -333,5 +367,13 @@
}
}
}
},
"securityDefinitions": {
"ApiKeyAuth": {
"description": "\"\"Bearer TOKEN\" 设置为初始化时设置的 API 密钥\"",
"type": "apiKey",
"name": "Authorization",
"in": "header"
}
}
}
27 changes: 27 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,27 @@ paths:
summary: 新增评论
tags:
- 评论
/api/comment/delete:
delete:
description: 根据ID删除评论
responses:
"200":
description: OK
schema:
$ref: '#/definitions/common.Resp'
"403":
description: Forbidden
schema:
$ref: '#/definitions/common.Resp'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/common.Resp'
security:
- ApiKeyAuth: []
summary: 根据ID删除评论
tags:
- 评论
/api/comment/getAll:
get:
description: 获取所有评论
Expand Down Expand Up @@ -206,4 +227,10 @@ paths:
summary: 初始化服务端
tags:
- 服务端
securityDefinitions:
ApiKeyAuth:
description: '"将 "Bearer TOKEN" 设置为初始化时设置的 API 密钥"'
in: header
name: Authorization
type: apiKey
swagger: "2.0"
40 changes: 38 additions & 2 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
github.com/ClickHouse/ch-go v0.48.0/go.mod h1:KBY72ltlOlHelc4Jn4hlReP8Caek8d6RG4ZkoPsWxzc=
github.com/ClickHouse/clickhouse-go/v2 v2.3.0/go.mod h1:f2kb1LPopJdIyt0Y0vxNk9aiQCyhCmeVcyvOOaPCT4Q=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/denisenkom/go-mssqldb v0.12.0/go.mod h1:iiK0YP1ZeepvmBQk/QpLEhhTNJgfzrpArPY/aFvc9yU=
github.com/go-faster/city v1.0.1/go.mod h1:jKcUJId49qdW3L1qKHH/3wPeUstCVpVSXTM6vO3VcTw=
github.com/go-faster/errors v0.6.1/go.mod h1:5MGV2/2T9yvlrbhe9pD9LO5Z/2zCSq2T8j+Jpi2LAyY=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/gofiber/fiber/v2 v2.47.0 h1:EN5lHVCc+Pyqh5OEsk8fzRiifgwpbrP0rulQ4iNf3fs=
github.com/gofiber/fiber/v2 v2.47.0/go.mod h1:mbFMVN1lQuzziTkkakgtKKdjfsXSw9BKR5lmcNksUoU=
gorm.io/plugin/dbresolver v1.4.1 h1:Ug4LcoPhrvqq71UhxtF346f+skTYoCa/nEsdjvHwEzk=
gorm.io/plugin/dbresolver v1.4.1/go.mod h1:CTbCtMWhsjXSiJqiW2R8POvJ2cq18RVOl4WGyT5nhNc=
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/jackc/chunkreader/v2 v2.0.1/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
github.com/jackc/pgconn v1.13.0/go.mod h1:AnowpAqO4CMIIJNZl2VJp+KrkAZciAkhEl0W0JIobpI=
github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgproto3/v2 v2.3.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E=
github.com/jackc/pgtype v1.12.0/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4=
github.com/jackc/pgx/v4 v4.17.2/go.mod h1:lcxIZN44yMIrWI78a5CpucdD14hX0SBDbNRvjDBItsw=
github.com/microsoft/go-mssqldb v0.17.0/go.mod h1:OkoNGhGEs8EZqchVTtochlXruEhEOaO4S0d2sB5aeGQ=
github.com/paulmach/orb v0.7.1/go.mod h1:FWRlTgl88VI1RBx/MkrwWDRhQ96ctqMCh8boXhmqB/A=
github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0=
github.com/pierrec/lz4/v4 v4.1.17/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/tinylib/msgp v1.1.8/go.mod h1:qkpG+2ldGg4xRFmx+jfTvZPxfGFhi64BcnL9vkCm/Tw=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.opentelemetry.io/otel v1.10.0/go.mod h1:NbvWjCthWHKBEUMpf0/v8ZRZlni86PpGFEMA9pnQSnQ=
go.opentelemetry.io/otel/trace v1.10.0/go.mod h1:Sij3YYczqAdz+EhmGhE6TpTxUO5/F/AzrK+kxfGqySM=
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
gorm.io/driver/clickhouse v0.5.0/go.mod h1:cIKAlFw+IVK75g0bDcm0M9qRA4EAgsn23Si+zCXQ1Lc=
gorm.io/driver/postgres v1.4.5/go.mod h1:GKNQYSJ14qvWkvPwXljMGehpKrhlDNsqYRr5HnYGncg=
gorm.io/driver/sqlserver v1.4.1/go.mod h1:DJ4P+MeZbc5rvY58PnmN1Lnyvb5gw5NPzGshHDnJLig=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
2 changes: 1 addition & 1 deletion packages/client/src/i18n/languages/fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export default {
link: "lien",
send: "envoyer",
welcome: "Que voulez-vous dire ?",
admin: "administrateur",
admin: "Administrateur",
reply: "répondre",
} satisfies Translations;
10 changes: 5 additions & 5 deletions packages/client/src/i18n/languages/zh-wenyan.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Translations } from "../types";

export default {
name: "",
name: "",
email: "函箱",
link: "链接",
send: "发遣",
welcome: "述诸其言",
link: "連結",
send: "發遣",
welcome: "述諸其言",
admin: "有司",
reply: "覆书",
reply: "覆書",
} satisfies Translations;
5 changes: 1 addition & 4 deletions server/common/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,5 @@ func Auth(c *fiber.Ctx) bool {
return false
}
apikey := server.ApiKey
if md5.MD5(userToken) != apikey {
return false
}
return true
return md5.MD5(userToken) == apikey
}
2 changes: 1 addition & 1 deletion server/handler/comment_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func CommentAdd(router fiber.Router) {
author, _ = query.Author.Where(query.Author.Name.Eq(name)).First()
}
if author.IsAdmin {
if common.Auth(c) == false {
if !common.Auth(c) {
return common.RespError(c, "Token错误", nil, http.StatusForbidden)
}
}
Expand Down
40 changes: 40 additions & 0 deletions server/handler/comment_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package handler

import (
"strconv"

"github.com/gofiber/fiber/v2"
"github.com/retalkgo/retalk/internal/entity"
"github.com/retalkgo/retalk/internal/query"
"github.com/retalkgo/retalk/server/common"
)

// @Summary 根据ID删除评论
// @Description 根据ID删除评论
// @Tags 评论
// @Success 200 {object} common.Resp
// @Failure 403 {object} common.Resp
// @Failure 500 {object} common.Resp
// @Security ApiKeyAuth
// @Router /api/comment/delete [delete]
func CommentDelete(router fiber.Router) {
router.Delete("/delete", func(c *fiber.Ctx) error {
if !common.Auth(c) {
return common.RespError(c, "Token错误", nil, 403)
}
raw_id := c.Query("id")
if raw_id == "" {
return common.RespError(c, "请传递评论ID", nil, 400)
}
int_id, err := strconv.Atoi(raw_id)
if err != nil {
return common.RespServerError(c)
}
id := uint(int_id)
_, err = query.Comment.Delete(&entity.Comment{Base: entity.Base{ID: id}})
if err != nil {
return common.RespServerError(c)
}
return common.RespSuccess(c, "成功删除", nil)
})
}
5 changes: 4 additions & 1 deletion server/handler/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ type RespInit struct {
func Init(router fiber.Router) {
router.Post("/init", func(c *fiber.Ctx) error {
AllServers, err := query.Server.Find()
if err != nil {
return common.RespServerError(c)
}
if len(AllServers) != 0 {
return common.RespError(c, "已经被初始化", nil, http.StatusForbidden)
return common.RespError(c, "此服务端已经被初始化", nil, http.StatusForbidden)
}
apikey := c.FormValue("apikey")
if apikey == "" {
Expand Down
1 change: 1 addition & 0 deletions server/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func Init(app *fiber.App) {
h.CommentGetAll(comment)
h.CommentGetByPath(comment)
h.CommentAdd(comment)
h.CommentDelete(comment)

// 404路由
h.NotFound(app)
Expand Down
22 changes: 12 additions & 10 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@ import (
"github.com/gofiber/fiber/v2"
)

// @Title Retalk API
// @Version 1.0
// @Description Retalk 后端 API 文档
// @BasePath /

// @Contact.name API 支持
// @Contact.email retalk@redish101.top

// @License.name GPL-3.0
// @Title Retalk API
// @Version 1.0
// @Description Retalk 后端 API 文档
// @BasePath /
// @Contact.name API 支持
// @Contact.email retalk@redish101.top
// @License.name GPL-3.0
// @SecurityDefinitions.apikey ApiKeyAuth
// @In header
// @Name Authorization
// @Description "将 "Bearer TOKEN" 设置为初始化时设置的 API 密钥"

func Start() {
app := fiber.New(fiber.Config{
AppName: "Retalk " + version.Version + "-" + version.CommitHash,
ServerHeader: "retalk",
Prefork: true,
Prefork: false,
})
Init(app)
app.Listen(":3000")
Expand Down

0 comments on commit 706e49f

Please sign in to comment.