Skip to content

Commit

Permalink
add new api to get all comments of discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
Lei Da authored and Rader committed Sep 10, 2024
1 parent 8b6da57 commit d705c6a
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 42 deletions.
55 changes: 44 additions & 11 deletions api/handler/discussion.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func NewDiscussionHandler() (*DiscussionHandler, error) {
// @Accept json
// @Produce json
// @Param current_user query string true "current user, the owner"
// @Param repo_type query string true "repository type" Enums(models,datasets,codes,spaces)
// @Param namespace query string true "namespace"
// @Param name query string true "name"
// @Param repo_type path string true "repository type" Enums(models,datasets,codes,spaces)
// @Param namespace path string true "namespace"
// @Param name path string true "name"
// @Param body body component.CreateRepoDiscussionRequest true "body"
// @Success 200 {object} types.Response{data=component.CreateDiscussionResponse} "OK"
// @Failure 400 {object} types.APIBadRequest "Bad request"
Expand Down Expand Up @@ -79,14 +79,19 @@ func (h *DiscussionHandler) CreateRepoDiscussion(ctx *gin.Context) {
// @Tags Discussion
// @Accept json
// @Produce json
// @Param id query string true "the discussion id"
// @Param id path string true "the discussion id"
// @Param current_user query string true "current user, the owner"
// @Param body body component.UpdateDiscussionRequest true "body"
// @Success 200 {object} types.Response "OK"
// @Failure 400 {object} types.APIBadRequest "Bad request"
// @Failure 500 {object} types.APIInternalServerError "Internal server error"
// @Router /discussions/{id} [put]
func (h *DiscussionHandler) UpdateDiscussion(ctx *gin.Context) {
currentUser := httpbase.GetCurrentUser(ctx)
if currentUser == "" {
httpbase.UnauthorizedError(ctx, component.ErrUserNotFound)
return
}
id := ctx.Param("id")
idInt, err := strconv.ParseInt(id, 10, 64)
if err != nil {
Expand All @@ -99,6 +104,7 @@ func (h *DiscussionHandler) UpdateDiscussion(ctx *gin.Context) {
return
}
req.ID = idInt
req.CurrentUser = currentUser
err = h.c.UpdateDiscussion(ctx, req)
if err != nil {
slog.Error("Failed to update discussion", "error", err, "request", req)
Expand All @@ -116,7 +122,7 @@ func (h *DiscussionHandler) UpdateDiscussion(ctx *gin.Context) {
// @Tags Discussion
// @Accept json
// @Produce json
// @Param id query string true "the discussion id"
// @Param id path string true "the discussion id"
// @Param current_user query string true "current user, the owner of the discussion"
// @Success 200 {object} types.Response "OK"
// @Failure 400 {object} types.APIBadRequest "Bad request"
Expand Down Expand Up @@ -151,7 +157,7 @@ func (h *DiscussionHandler) DeleteDiscussion(ctx *gin.Context) {
// @Tags Discussion
// @Accept json
// @Produce json
// @Param id query string true "the discussion id"
// @Param id path string true "the discussion id"
// @Success 200 {object} types.Response{data=component.ShowDiscussionResponse} "OK"
// @Failure 400 {object} types.APIBadRequest "Bad request"
// @Failure 500 {object} types.APIInternalServerError "Internal server error"
Expand Down Expand Up @@ -181,8 +187,8 @@ func (h *DiscussionHandler) ShowDiscussion(ctx *gin.Context) {
// @Accept json
// @Produce json
// @Param current_user query string false "current user"
// @Param repo_type query string true "repository type" Enums(models,datasets,codes,spaces)
// @Param namespace query string true "namespace"
// @Param repo_type path string true "repository type" Enums(models,datasets,codes,spaces)
// @Param namespace path string true "namespace"
// @Param name query string true "name"
// @Success 200 {object} types.Response{data=component.ListRepoDiscussionResponse} "OK"
// @Failure 400 {object} types.APIBadRequest "Bad request"
Expand Down Expand Up @@ -218,7 +224,7 @@ func (h *DiscussionHandler) ListRepoDiscussions(ctx *gin.Context) {
// @Tags Discussion
// @Accept json
// @Produce json
// @Param id query string true "the discussion id"
// @Param id path string true "the discussion id"
// @Param body body component.CreateCommentRequest true "body"
// @Success 200 {object} types.Response{data=component.CreateCommentResponse} "OK"
// @Failure 400 {object} types.APIBadRequest "Bad request"
Expand Down Expand Up @@ -261,7 +267,7 @@ func (h *DiscussionHandler) CreateDiscussionComment(ctx *gin.Context) {
// @Tags Discussion
// @Accept json
// @Produce json
// @Param id query string true "the comment id"
// @Param id path string true "the comment id"
// @Param current_user query string true "current user, the owner of the comment"
// @Param body body component.UpdateCommentRequest true "body"
// @Success 200 {object} types.Response "OK"
Expand Down Expand Up @@ -303,7 +309,7 @@ func (h *DiscussionHandler) UpdateComment(ctx *gin.Context) {
// @Tags Discussion
// @Accept json
// @Produce json
// @Param id query string true "the comment id"
// @Param id path string true "the comment id"
// @Param current_user query string true "current user, the owner of the comment"
// @Success 200 {object} types.Response "OK"
// @Failure 400 {object} types.APIBadRequest "Bad request"
Expand All @@ -330,6 +336,33 @@ func (h *DiscussionHandler) DeleteComment(ctx *gin.Context) {
httpbase.OK(ctx, nil)
}

// ListDiscussionComments godoc
// @Security ApiKey
// @Summary List discussion comments
// @Description list discussion comments
// @Tags Discussion
// @Accept json
// @Produce json
// @Param id path string true "the discussion id"
// @Success 200 {object} types.Response{data=[]component.DiscussionResponse_Comment} "OK"
// @Failure 400 {object} types.APIBadRequest "Bad request"
// @Failure 500 {object} types.APIInternalServerError "Internal server error"
// @Router /discussions/{id}/comments [get]
func (h *DiscussionHandler) ListDiscussionComments(ctx *gin.Context) {
id := ctx.Param("id")
idInt, err := strconv.ParseInt(id, 10, 64)
if err != nil {
httpbase.BadRequest(ctx, fmt.Errorf("invalid discussion id: %w", err).Error())
}
comments, err := h.c.ListDiscussionComments(ctx, idInt)
if err != nil {
slog.Error("Failed to list discussion comments", "error", err, "id", id)
httpbase.ServerError(ctx, fmt.Errorf("failed to list discussion comments: %w", err))
return
}
httpbase.OK(ctx, comments)
}

func (h *DiscussionHandler) getRepoType(ctx *gin.Context) types.RepositoryType {
repoType := ctx.Param("repo_type")
repoType = strings.TrimRight(repoType, "s")
Expand Down
1 change: 1 addition & 0 deletions api/router/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ func createDiscussionRoutes(apiGroup *gin.RouterGroup, needAPIKey gin.HandlerFun
apiGroup.PUT("/discussions/:id", discussionHandler.UpdateDiscussion)
apiGroup.DELETE("/discussions/:id", discussionHandler.DeleteDiscussion)
apiGroup.POST("/discussions/:id/comments", discussionHandler.CreateDiscussionComment)
apiGroup.GET("/discussions/:id/comments", discussionHandler.ListDiscussionComments)
apiGroup.PUT("/discussions/:id/comments/:comment_id", discussionHandler.UpdateComment)
apiGroup.DELETE("/discussions/:id/comments/:comment_id", discussionHandler.DeleteComment)
}
21 changes: 21 additions & 0 deletions component/discussion.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,27 @@ func (c *DiscussionComponent) DeleteComment(ctx context.Context, currentUser str
return nil
}

func (c *DiscussionComponent) ListDiscussionComments(ctx context.Context, discussionID int64) ([]*DiscussionResponse_Comment, error) {
comments, err := c.ds.FindDiscussionComments(ctx, discussionID)
if err != nil {
return nil, fmt.Errorf("failed to find discussion comments by discussion id '%d': %w", discussionID, err)
}
resp := make([]*DiscussionResponse_Comment, 0, len(comments))
for _, comment := range comments {
resp = append(resp, &DiscussionResponse_Comment{
ID: comment.ID,
Content: comment.Content,
User: &DiscussionResponse_User{
ID: comment.User.ID,
Username: comment.User.Username,
Avatar: comment.User.Avatar,
},
CreatedAt: comment.CreatedAt,
})
}
return resp, nil
}

//--- request and response ---//

type CreateRepoDiscussionRequest struct {
Expand Down
89 changes: 79 additions & 10 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ const docTemplate = `{
"type": "string",
"description": "the comment id",
"name": "id",
"in": "query",
"in": "path",
"required": true
},
{
Expand Down Expand Up @@ -1220,7 +1220,7 @@ const docTemplate = `{
"type": "string",
"description": "the comment id",
"name": "id",
"in": "query",
"in": "path",
"required": true
},
{
Expand Down Expand Up @@ -1895,7 +1895,7 @@ const docTemplate = `{
"type": "string",
"description": "the discussion id",
"name": "id",
"in": "query",
"in": "path",
"required": true
}
],
Expand Down Expand Up @@ -1954,7 +1954,7 @@ const docTemplate = `{
"type": "string",
"description": "the discussion id",
"name": "id",
"in": "query",
"in": "path",
"required": true
},
{
Expand Down Expand Up @@ -2017,6 +2017,13 @@ const docTemplate = `{
"type": "string",
"description": "the discussion id",
"name": "id",
"in": "path",
"required": true
},
{
"type": "string",
"description": "current user, the owner of the discussion",
"name": "current_user",
"in": "query",
"required": true
}
Expand Down Expand Up @@ -2044,6 +2051,68 @@ const docTemplate = `{
}
},
"/discussions/{id}/comments": {
"get": {
"security": [
{
"ApiKey": []
}
],
"description": "list discussion comments",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Discussion"
],
"summary": "List discussion comments",
"parameters": [
{
"type": "string",
"description": "the discussion id",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/types.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/component.DiscussionResponse_Comment"
}
}
}
}
]
}
},
"400": {
"description": "Bad request",
"schema": {
"$ref": "#/definitions/types.APIBadRequest"
}
},
"500": {
"description": "Internal server error",
"schema": {
"$ref": "#/definitions/types.APIInternalServerError"
}
}
}
},
"post": {
"security": [
{
Expand All @@ -2066,7 +2135,7 @@ const docTemplate = `{
"type": "string",
"description": "the discussion id",
"name": "id",
"in": "query",
"in": "path",
"required": true
},
{
Expand Down Expand Up @@ -10207,14 +10276,14 @@ const docTemplate = `{
"type": "string",
"description": "repository type",
"name": "repo_type",
"in": "query",
"in": "path",
"required": true
},
{
"type": "string",
"description": "namespace",
"name": "namespace",
"in": "query",
"in": "path",
"required": true
},
{
Expand Down Expand Up @@ -10293,21 +10362,21 @@ const docTemplate = `{
"type": "string",
"description": "repository type",
"name": "repo_type",
"in": "query",
"in": "path",
"required": true
},
{
"type": "string",
"description": "namespace",
"name": "namespace",
"in": "query",
"in": "path",
"required": true
},
{
"type": "string",
"description": "name",
"name": "name",
"in": "query",
"in": "path",
"required": true
},
{
Expand Down
Loading

0 comments on commit d705c6a

Please sign in to comment.