Skip to content

Commit

Permalink
题解评论的创建与获取
Browse files Browse the repository at this point in the history
  • Loading branch information
Invariant64 committed Oct 24, 2022
1 parent 51ce75d commit 5a0691d
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 3 deletions.
60 changes: 60 additions & 0 deletions app/controller/solution_comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package controller

import (
"net/http"

"github.com/EduOJ/backend/app/request"
"github.com/EduOJ/backend/app/response"
"github.com/EduOJ/backend/app/response/resource"
"github.com/EduOJ/backend/base"
"github.com/EduOJ/backend/base/utils"
"github.com/EduOJ/backend/database/models"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"gorm.io/gorm"
)

func GetSolutionComments(c echo.Context) error {
sc, err := utils.FindSolutionComments(c.Param("id"))
if errors.Is(err, gorm.ErrRecordNotFound) {
return c.JSON(http.StatusNotFound, response.ErrorResp("NOT_FOUND", nil))
} else if err != nil {
panic(err)
}
return c.JSON(http.StatusOK, response.GetSolutionCommentsResponse{
Message: "SUCCESS",
Error: nil,
Data: struct {
SolutionComments []resource.SolutionComment `json:"solution_comments"`
}{
resource.GetSolutionComments(sc),
},
})
}

func CreateSolutions(c echo.Context) error {
req := request.CreateSolutionCommentRequest{}
err, ok := utils.BindAndValidate(&req, c)
if !ok {
return err
}

comment := models.SolutionComment{
SolutionID: req.SolutionID,
FatherNode: req.FatherNode,
Description: req.Description,
Speaker: req.Speaker,
}
utils.PanicIfDBError(base.DB.Create(&comment), "could not create comment")

return c.JSON(http.StatusOK, response.CreateSolutionCommentResponse{
Message: "SUCCESS",
Error: nil,
Data: struct {
*resource.SolutionComment `json:"solution_comment"`
}{
resource.GetSolutionComment(&comment),
},
})

}
36 changes: 36 additions & 0 deletions app/controller/solution_comment_tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package controller

import (
"net/http"

"github.com/EduOJ/backend/app/response"
"github.com/EduOJ/backend/app/response/resource"
"github.com/EduOJ/backend/base/utils"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"gorm.io/gorm"
)

func GetCommentTree(c echo.Context) error {
solutionComments, err := utils.FindSolutionComments(c.Param("solution_id"))
if errors.Is(err, gorm.ErrRecordNotFound) {
return c.JSON(http.StatusNotFound, response.ErrorResp("NOT_FOUND", nil))
} else if err != nil {
panic(err)
}

commentNodes := make([]resource.SolutionCommentNode, len(solutionComments))
for i, solutionComment := range solutionComments {
commentNodes[i].ConvertCommentToNode(&solutionComment)
}

return c.JSON(http.StatusOK, response.GetCommentTreeResponse{
Message: "SUCCESS",
Error: nil,
Data: struct {
*resource.SolutionCommentTree `json:"solution_comment_tree"`
}{
resource.GetSolutionCommentTree(commentNodes),
},
})
}
8 changes: 8 additions & 0 deletions app/response/resource/solution_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ func GetSolutionComment(solutionComment *models.SolutionComment) *SolutionCommen
sc.convert(solutionComment)
return &sc
}

func GetSolutionComments(solutionComments []models.SolutionComment) []SolutionComment {
scs := make([]SolutionComment, 0)
for _, sc := range solutionComments {
scs = append(scs, *GetSolutionComment(&sc))
}
return scs
}
34 changes: 34 additions & 0 deletions app/response/resource/solution_comment_node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package resource

import (
"github.com/EduOJ/backend/database/models"
)

type SolutionCommentNode struct {
ID uint `gorm:"primaryKey" json:"id"`

SolutionID uint `sql:"index" json:"solution_id" gorm:"not null"`
FatherNode uint `json:"father_node" gorm:"not null"`
Description string `json:"description"`
Speaker string `json:"speaker"`

Kids []SolutionCommentNode `json:"kids"`
}

func (cn *SolutionCommentNode) ConvertCommentToNode(solutionComment *models.SolutionComment) {
cn.ID = solutionComment.ID
cn.SolutionID = solutionComment.SolutionID
cn.FatherNode = solutionComment.FatherNode
cn.Description = solutionComment.Description
cn.Speaker = solutionComment.Speaker
cn.Kids = make([]SolutionCommentNode, 0)
}

func (commentNode *SolutionCommentNode) GetKids(commentNodes []SolutionCommentNode) {
for _, cn := range commentNodes {
if cn.FatherNode == commentNode.FatherNode {
cn.GetKids(commentNodes)
commentNode.Kids = append(commentNode.Kids, cn)
}
}
}
20 changes: 20 additions & 0 deletions app/response/resource/solution_comment_tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package resource

type SolutionCommentTree struct {
Roots []SolutionCommentNode `json:"roots"`
}

func (t *SolutionCommentTree) BuildTree(commentNodes []SolutionCommentNode) {
for _, commentNode := range commentNodes {
if commentNode.FatherNode == 0 {
commentNode.GetKids(commentNodes)
t.Roots = append(t.Roots, commentNode)
}
}
}

func GetSolutionCommentTree(commentNodes []SolutionCommentNode) *SolutionCommentTree {
sct := SolutionCommentTree{}
sct.BuildTree(commentNodes)
return &sct
}
14 changes: 11 additions & 3 deletions app/response/solution_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,30 @@ type GetSolutionCommentResponse struct {
Message string `json:"message"`
Error interface{} `json:"error"`
Data struct {
*resource.SolutionComment `json:"solution"`
*resource.SolutionComment `json:"solution_comment"`
} `json:"data"`
}

type CreateSolutionCommentResponse struct {
Message string `json:"message"`
Error interface{} `json:"error"`
Data struct {
*resource.SolutionComment `json:"solution"`
*resource.SolutionComment `json:"solution_comment"`
} `json:"data"`
}

type UpdateSolutionCommentResponse struct {
Message string `json:"message"`
Error interface{} `json:"error"`
Data struct {
*resource.SolutionComment `json:"solutiong_comment"`
*resource.SolutionComment `json:"solution_comment"`
} `json:"data"`
}

type GetSolutionCommentsResponse struct {
Message string `json:"message"`
Error interface{} `json:"error"`
Data struct {
SolutionComments []resource.SolutionComment `json:"solution_comments"`
} `json:"data"`
}
11 changes: 11 additions & 0 deletions app/response/solution_comment_tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package response

import "github.com/EduOJ/backend/app/response/resource"

type GetCommentTreeResponse struct {
Message string `json:"message"`
Error interface{} `json:"error"`
Data struct {
*resource.SolutionCommentTree `json:"solution_comment_tree"`
} `json:"data"`
}
14 changes: 14 additions & 0 deletions base/utils/query_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ func FindSolution(id string) (*models.Solution, error) {
return &solution, nil
}

func FindSolutionComments(solutionID string) ([]models.SolutionComment, error) {
sc := []models.SolutionComment{}
query := base.DB
err := query.Where("SolutionID = ?", solutionID).Find(&sc).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
} else {
return nil, errors.Wrap(err, "could not query solution comment")
}
}
return sc, nil
}

// This function checks if the user has permission to get problems which are not public.
// nil user pointer is regarded as admin(skip the permission judgement).
func FindTestCase(problemId string, testCaseIdStr string, user *models.User) (*models.TestCase, *models.Problem, error) {
Expand Down
Binary file modified database.db
Binary file not shown.
22 changes: 22 additions & 0 deletions database/models/solution_comment_node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package models

import (
"time"

"gorm.io/gorm"
)

type SolutionCommentNode struct {
ID uint `gorm:"primaryKey" json:"id"`

SolutionID uint `sql:"index" json:"solution_id" gorm:"not null"`
FatherNode uint `json:"father_node" gorm:"not null"`
Description string `json:"description"`
Speaker string `json:"speaker"`

Kids []SolutionCommentNode `json:"kids"`

CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"-"`
DeletedAt gorm.DeletedAt `json:"deleted_at"`
}
15 changes: 15 additions & 0 deletions database/models/solution_comment_tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package models

import (
"time"

"gorm.io/gorm"
)

type SolutionCommentTree struct {
Roots []SolutionCommentNode `json:"roots"`

CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"-"`
DeletedAt gorm.DeletedAt `json:"deleted_at"`
}

0 comments on commit 5a0691d

Please sign in to comment.