mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Add comment functionality.
This commit is contained in:
62
server/api/comment.go
Normal file
62
server/api/comment.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"nysoure/server/model"
|
||||
"nysoure/server/service"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func AddCommentRoutes(router fiber.Router) {
|
||||
api := router.Group("/comments")
|
||||
api.Post("/:resourceID", createComment)
|
||||
api.Get("/:resourceID", listComments)
|
||||
}
|
||||
|
||||
func createComment(c fiber.Ctx) error {
|
||||
userID, ok := c.Locals("uid").(uint)
|
||||
if !ok {
|
||||
return model.NewRequestError("You must be logged in to comment")
|
||||
}
|
||||
resourceIDStr := c.Params("resourceID")
|
||||
resourceID, err := strconv.Atoi(resourceIDStr)
|
||||
if err != nil {
|
||||
return model.NewRequestError("Invalid resource ID")
|
||||
}
|
||||
content := c.FormValue("content")
|
||||
if content == "" {
|
||||
return model.NewRequestError("Content cannot be empty")
|
||||
}
|
||||
comment, err := service.CreateComment(content, userID, uint(resourceID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Status(fiber.StatusCreated).JSON(model.Response[model.CommentView]{
|
||||
Success: true,
|
||||
Data: *comment,
|
||||
Message: "Comment created successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func listComments(c fiber.Ctx) error {
|
||||
resourceIDStr := c.Params("resourceID")
|
||||
resourceID, err := strconv.Atoi(resourceIDStr)
|
||||
if err != nil {
|
||||
return model.NewRequestError("Invalid resource ID")
|
||||
}
|
||||
pageStr := c.Query("page", "1")
|
||||
page, err := strconv.Atoi(pageStr)
|
||||
if err != nil {
|
||||
return model.NewRequestError("Invalid page number")
|
||||
}
|
||||
comments, totalPages, err := service.ListComments(uint(resourceID), page)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(model.PageResponse[model.CommentView]{
|
||||
Success: true,
|
||||
Data: comments,
|
||||
TotalPages: totalPages,
|
||||
Message: "Comments retrieved successfully",
|
||||
})
|
||||
}
|
30
server/dao/comment.go
Normal file
30
server/dao/comment.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package dao
|
||||
|
||||
import "nysoure/server/model"
|
||||
|
||||
func CreateComment(content string, userID uint, resourceID uint) (model.Comment, error) {
|
||||
c := model.Comment{
|
||||
Content: content,
|
||||
UserID: userID,
|
||||
ResourceID: resourceID,
|
||||
}
|
||||
err := db.Save(&c).Error
|
||||
return c, err
|
||||
}
|
||||
|
||||
func GetCommentByResourceID(resourceID uint, page, pageSize int) ([]model.Comment, int, error) {
|
||||
var comments []model.Comment
|
||||
var total int64
|
||||
|
||||
if err := db.Model(&model.Comment{}).Where("resource_id = ?", resourceID).Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if err := db.Where("resource_id = ?", resourceID).Offset((page - 1) * pageSize).Limit(pageSize).Preload("User").Order("created_at DESC").Find(&comments).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
totalPages := (int(total) + pageSize - 1) / pageSize
|
||||
|
||||
return comments, totalPages, nil
|
||||
}
|
@@ -15,7 +15,7 @@ func init() {
|
||||
panic("failed to connect database")
|
||||
}
|
||||
|
||||
_ = db.AutoMigrate(&model.User{}, &model.Resource{}, &model.Image{}, &model.Tag{}, &model.Storage{}, &model.File{}, &model.UploadingFile{}, &model.Statistic{})
|
||||
_ = db.AutoMigrate(&model.User{}, &model.Resource{}, &model.Image{}, &model.Tag{}, &model.Storage{}, &model.File{}, &model.UploadingFile{}, &model.Statistic{}, &model.Comment{})
|
||||
}
|
||||
|
||||
func GetDB() *gorm.DB {
|
||||
|
@@ -167,3 +167,14 @@ func GetResourceByTag(tagID uint, page int, pageSize int) ([]model.Resource, int
|
||||
|
||||
return tag.Resources, totalPages, nil
|
||||
}
|
||||
|
||||
func ExistsResource(id uint) (bool, error) {
|
||||
var r model.Resource
|
||||
if err := db.Model(&model.Resource{}).Where("id = ?", id).First(&r).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
@@ -39,6 +39,14 @@ func ExistsUser(username string) (bool, error) {
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func ExistsUserByID(id uint) (bool, error) {
|
||||
var count int64
|
||||
if err := db.Model(&model.User{}).Where("id = ?", id).Count(&count).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func GetUserByUsername(username string) (model.User, error) {
|
||||
var user model.User
|
||||
if err := db.Where("username = ?", username).First(&user).Error; err != nil {
|
||||
@@ -76,17 +84,14 @@ func IsUserDataBaseEmpty() (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// 获取分页用户列表
|
||||
func ListUsers(page, pageSize int) ([]model.User, int64, error) {
|
||||
var users []model.User
|
||||
var total int64
|
||||
|
||||
// 获取总数
|
||||
if err := db.Model(&model.User{}).Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 分页获取用户
|
||||
offset := (page - 1) * pageSize
|
||||
if err := db.Offset(offset).Limit(pageSize).Order("id desc").Find(&users).Error; err != nil {
|
||||
return nil, 0, err
|
||||
@@ -95,17 +100,14 @@ func ListUsers(page, pageSize int) ([]model.User, int64, error) {
|
||||
return users, total, nil
|
||||
}
|
||||
|
||||
// 根据用户名搜索用户
|
||||
func SearchUsersByUsername(username string, page, pageSize int) ([]model.User, int64, error) {
|
||||
var users []model.User
|
||||
var total int64
|
||||
|
||||
// 获取符合条件的总数
|
||||
if err := db.Model(&model.User{}).Where("username LIKE ?", "%"+username+"%").Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 分页获取符合条件的用户
|
||||
offset := (page - 1) * pageSize
|
||||
if err := db.Where("username LIKE ?", "%"+username+"%").Offset(offset).Limit(pageSize).Order("id desc").Find(&users).Error; err != nil {
|
||||
return nil, 0, err
|
||||
@@ -114,7 +116,6 @@ func SearchUsersByUsername(username string, page, pageSize int) ([]model.User, i
|
||||
return users, total, nil
|
||||
}
|
||||
|
||||
// 删除用户
|
||||
func DeleteUser(id uint) error {
|
||||
return db.Delete(&model.User{}, id).Error
|
||||
}
|
||||
|
31
server/model/comment.go
Normal file
31
server/model/comment.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Comment struct {
|
||||
gorm.Model
|
||||
Content string `gorm:"not null"`
|
||||
ResourceID uint `gorm:"not null"`
|
||||
UserID uint `gorm:"not null"`
|
||||
User User `gorm:"foreignKey:UserID"`
|
||||
Resource Resource `gorm:"foreignKey:ResourceID"`
|
||||
}
|
||||
|
||||
type CommentView struct {
|
||||
ID uint `json:"id"`
|
||||
Content string `json:"content"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
User UserView `json:"user"`
|
||||
}
|
||||
|
||||
func (c *Comment) ToView() *CommentView {
|
||||
return &CommentView{
|
||||
ID: c.ID,
|
||||
Content: c.Content,
|
||||
CreatedAt: c.CreatedAt,
|
||||
User: c.User.ToView(),
|
||||
}
|
||||
}
|
53
server/service/comment.go
Normal file
53
server/service/comment.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v3/log"
|
||||
"nysoure/server/dao"
|
||||
"nysoure/server/model"
|
||||
)
|
||||
|
||||
func CreateComment(content string, userID uint, resourceID uint) (*model.CommentView, error) {
|
||||
resourceExists, err := dao.ExistsResource(resourceID)
|
||||
if err != nil {
|
||||
log.Error("Error checking resource existence:", err)
|
||||
return nil, model.NewInternalServerError("Error checking resource existence")
|
||||
}
|
||||
if !resourceExists {
|
||||
return nil, model.NewNotFoundError("Resource not found")
|
||||
}
|
||||
userExists, err := dao.ExistsUserByID(userID)
|
||||
if err != nil {
|
||||
log.Error("Error checking user existence:", err)
|
||||
return nil, model.NewInternalServerError("Error checking user existence")
|
||||
}
|
||||
if !userExists {
|
||||
return nil, model.NewNotFoundError("User not found")
|
||||
}
|
||||
c, err := dao.CreateComment(content, userID, resourceID)
|
||||
if err != nil {
|
||||
log.Error("Error creating comment:", err)
|
||||
return nil, model.NewInternalServerError("Error creating comment")
|
||||
}
|
||||
return c.ToView(), nil
|
||||
}
|
||||
|
||||
func ListComments(resourceID uint, page int) ([]model.CommentView, int, error) {
|
||||
resourceExists, err := dao.ExistsResource(resourceID)
|
||||
if err != nil {
|
||||
log.Error("Error checking resource existence:", err)
|
||||
return nil, 0, model.NewInternalServerError("Error checking resource existence")
|
||||
}
|
||||
if !resourceExists {
|
||||
return nil, 0, model.NewNotFoundError("Resource not found")
|
||||
}
|
||||
comments, totalPages, err := dao.GetCommentByResourceID(resourceID, page, pageSize)
|
||||
if err != nil {
|
||||
log.Error("Error getting comments:", err)
|
||||
return nil, 0, model.NewInternalServerError("Error getting comments")
|
||||
}
|
||||
res := make([]model.CommentView, 0, len(comments))
|
||||
for _, c := range comments {
|
||||
res = append(res, *c.ToView())
|
||||
}
|
||||
return res, totalPages, nil
|
||||
}
|
5
server/service/consts.go
Normal file
5
server/service/consts.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package service
|
||||
|
||||
const (
|
||||
pageSize = 20
|
||||
)
|
@@ -7,10 +7,6 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
pageSize = 20
|
||||
)
|
||||
|
||||
type ResourceCreateParams struct {
|
||||
Title string `json:"title" binding:"required"`
|
||||
AlternativeTitles []string `json:"alternative_titles"`
|
||||
|
Reference in New Issue
Block a user