Skip to content

Commit

Permalink
feat(api): add GET /comments/:id endpoint (#764)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode authored Feb 4, 2024
1 parent 5a4c06d commit 191977c
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 0 deletions.
92 changes: 92 additions & 0 deletions docs/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,73 @@ const docTemplate = `{
}
},
"/comments/{id}": {
"get": {
"description": "Get the detail of a comment by comment id",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Comment"
],
"summary": "Get a comment",
"operationId": "GetComment",
"parameters": [
{
"type": "integer",
"description": "The comment ID you want to get",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/handler.ResponseCommentGet"
}
},
"404": {
"description": "Not Found",
"schema": {
"allOf": [
{
"$ref": "#/definitions/handler.Map"
},
{
"type": "object",
"properties": {
"msg": {
"type": "string"
}
}
}
]
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"allOf": [
{
"$ref": "#/definitions/handler.Map"
},
{
"type": "object",
"properties": {
"msg": {
"type": "string"
}
}
}
]
}
}
}
},
"put": {
"security": [
{
Expand Down Expand Up @@ -3967,6 +4034,31 @@ const docTemplate = `{
}
}
},
"handler.ResponseCommentGet": {
"type": "object",
"required": [
"comment",
"reply_comment"
],
"properties": {
"comment": {
"description": "The comment detail",
"allOf": [
{
"$ref": "#/definitions/entity.CookedComment"
}
]
},
"reply_comment": {
"description": "The reply comment if exists (like reply)",
"allOf": [
{
"$ref": "#/definitions/entity.CookedComment"
}
]
}
}
},
"handler.ResponseCommentList": {
"type": "object",
"required": [
Expand Down
92 changes: 92 additions & 0 deletions docs/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,73 @@
}
},
"/comments/{id}": {
"get": {
"description": "Get the detail of a comment by comment id",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Comment"
],
"summary": "Get a comment",
"operationId": "GetComment",
"parameters": [
{
"type": "integer",
"description": "The comment ID you want to get",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/handler.ResponseCommentGet"
}
},
"404": {
"description": "Not Found",
"schema": {
"allOf": [
{
"$ref": "#/definitions/handler.Map"
},
{
"type": "object",
"properties": {
"msg": {
"type": "string"
}
}
}
]
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"allOf": [
{
"$ref": "#/definitions/handler.Map"
},
{
"type": "object",
"properties": {
"msg": {
"type": "string"
}
}
}
]
}
}
}
},
"put": {
"security": [
{
Expand Down Expand Up @@ -3960,6 +4027,31 @@
}
}
},
"handler.ResponseCommentGet": {
"type": "object",
"required": [
"comment",
"reply_comment"
],
"properties": {
"comment": {
"description": "The comment detail",
"allOf": [
{
"$ref": "#/definitions/entity.CookedComment"
}
]
},
"reply_comment": {
"description": "The reply comment if exists (like reply)",
"allOf": [
{
"$ref": "#/definitions/entity.CookedComment"
}
]
}
}
},
"handler.ResponseCommentList": {
"type": "object",
"required": [
Expand Down
53 changes: 53 additions & 0 deletions docs/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,20 @@ definitions:
- vote_down
- vote_up
type: object
handler.ResponseCommentGet:
properties:
comment:
allOf:
- $ref: '#/definitions/entity.CookedComment'
description: The comment detail
reply_comment:
allOf:
- $ref: '#/definitions/entity.CookedComment'
description: The reply comment if exists (like reply)
required:
- comment
- reply_comment
type: object
handler.ResponseCommentList:
properties:
comments:
Expand Down Expand Up @@ -1428,6 +1442,45 @@ paths:
summary: Delete Comment
tags:
- Comment
get:
consumes:
- application/json
description: Get the detail of a comment by comment id
operationId: GetComment
parameters:
- description: The comment ID you want to get
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/handler.ResponseCommentGet'
"404":
description: Not Found
schema:
allOf:
- $ref: '#/definitions/handler.Map'
- properties:
msg:
type: string
type: object
"500":
description: Internal Server Error
schema:
allOf:
- $ref: '#/definitions/handler.Map'
- properties:
msg:
type: string
type: object
summary: Get a comment
tags:
- Comment
put:
consumes:
- application/json
Expand Down
56 changes: 56 additions & 0 deletions server/handler/comment_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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 ResponseCommentGet struct {
Comment entity.CookedComment `json:"comment"` // The comment detail
ReplyComment *entity.CookedComment `json:"reply_comment"` // The reply comment if exists (like reply)
}

// @Id GetComment
// @Summary Get a comment
// @Description Get the detail of a comment by comment id
// @Tags Comment
// @Param id path int true "The comment ID you want to get"
// @Accept json
// @Produce json
// @Success 200 {object} ResponseCommentGet
// @Failure 404 {object} Map{msg=string}
// @Failure 500 {object} Map{msg=string}
// @Router /comments/{id} [get]
func CommentGet(app *core.App, router fiber.Router) {
router.Get("/comments/:id", func(c *fiber.Ctx) error {
id, _ := c.ParamsInt("id")

// Find comment by id
comment := app.Dao().FindComment(uint(id))
if comment.IsEmpty() {
return common.RespError(c, 404, i18n.T("{{name}} not found", Map{"name": i18n.T("Comment")}))
}

// Find linked comment by id
var replyComment *entity.CookedComment
if comment.Rid != 0 {
rComment := app.Dao().FindComment(uint(comment.Rid))
if !comment.IsEmpty() {
rComment := app.Dao().CookComment(&rComment)
rComment.Visible = false
replyComment = &rComment
}
}

cookedComment := app.Dao().CookComment(&comment)
cookedComment = fetchIPRegionForComment(app, cookedComment)

return common.RespData(c, ResponseCommentGet{
Comment: cookedComment,
ReplyComment: replyComment,
})
})
}
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func Serve(app *core.App) (*fiber.App, error) {
{
h.CommentCreate(app, api)
h.CommentList(app, api)
h.CommentGet(app, api)
h.Vote(app, api)
h.PagePV(app, api)
h.Stat(app, api)
Expand Down
38 changes: 38 additions & 0 deletions ui/artalk/src/api/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,13 @@ export interface HandlerResponseCommentCreate {
vote_up: number
}

export interface HandlerResponseCommentGet {
/** The comment detail */
comment: EntityCookedComment
/** The reply comment if exists (like reply) */
reply_comment: EntityCookedComment
}

export interface HandlerResponseCommentList {
comments: EntityCookedComment[]
count: number
Expand Down Expand Up @@ -972,6 +979,37 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
...params,
}),

/**
* @description Get the detail of a comment by comment id
*
* @tags Comment
* @name GetComment
* @summary Get a comment
* @request GET:/comments/{id}
* @response `200` `HandlerResponseCommentGet` OK
* @response `404` `(HandlerMap & {
msg?: string,
})` Not Found
* @response `500` `(HandlerMap & {
msg?: string,
})` Internal Server Error
*/
getComment: (id: number, params: RequestParams = {}) =>
this.request<
HandlerResponseCommentGet,
HandlerMap & {
msg?: string
}
>({
path: `/comments/${id}`,
method: 'GET',
type: ContentType.Json,
format: 'json',
...params,
}),

/**
* @description Update a specific comment
*
Expand Down

0 comments on commit 191977c

Please sign in to comment.