Add statistics.

This commit is contained in:
2025-05-13 18:59:23 +08:00
parent f1345f9a0c
commit 12c1e0e413
8 changed files with 98 additions and 28 deletions

View File

@@ -1,15 +1,30 @@
package dao
import "nysoure/server/model"
import (
"gorm.io/gorm"
"nysoure/server/model"
)
func CreateComment(content string, userID uint, resourceID uint) (model.Comment, error) {
c := model.Comment{
Content: content,
UserID: userID,
ResourceID: resourceID,
var comment model.Comment
err := db.Transaction(func(tx *gorm.DB) error {
comment = model.Comment{
Content: content,
UserID: userID,
ResourceID: resourceID,
}
if err := tx.Create(&comment).Error; err != nil {
return err
}
if err := tx.Model(&model.User{}).Where("id = ?", userID).Update("comments_count", gorm.Expr("comments_count + 1")).Error; err != nil {
return err
}
return nil
})
if err != nil {
return model.Comment{}, err
}
err := db.Save(&c).Error
return c, err
return comment, nil
}
func GetCommentByResourceID(resourceID uint, page, pageSize int) ([]model.Comment, int, error) {