forked from EduOJ/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51ce75d
commit 5a0691d
Showing
11 changed files
with
231 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}, | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |