Add activities page.

This commit is contained in:
2025-06-13 19:32:25 +08:00
parent 0b3b54a0c4
commit 1f238c56f3
18 changed files with 424 additions and 70 deletions

53
server/dao/activity.go Normal file
View File

@@ -0,0 +1,53 @@
package dao
import "nysoure/server/model"
func AddNewResourceActivity(userID, resourceID uint) error {
activity := &model.Activity{
UserID: userID,
Type: model.ActivityTypeNewResource,
RefID: resourceID,
}
return db.Create(activity).Error
}
func AddUpdateResourceActivity(userID, resourceID uint) error {
activity := &model.Activity{
UserID: userID,
Type: model.ActivityTypeUpdateResource,
RefID: resourceID,
}
return db.Create(activity).Error
}
func AddNewCommentActivity(userID, commentID uint) error {
activity := &model.Activity{
UserID: userID,
Type: model.ActivityTypeNewComment,
RefID: commentID,
}
return db.Create(activity).Error
}
func DeleteResourceActivity(resourceID uint) error {
return db.Where("ref_id = ? AND (type = ? OR type = ?)", resourceID, model.ActivityTypeNewResource, model.ActivityTypeUpdateResource).Delete(&model.Activity{}).Error
}
func DeleteCommentActivity(commentID uint) error {
return db.Where("ref_id = ? AND type = ?", commentID, model.ActivityTypeNewComment).Delete(&model.Activity{}).Error
}
func GetActivityList(offset, limit int) ([]model.Activity, int, error) {
var activities []model.Activity
var total int64
if err := db.Model(&model.Activity{}).Count(&total).Error; err != nil {
return nil, 0, err
}
if err := db.Offset(offset).Limit(limit).Order("id DESC").Find(&activities).Error; err != nil {
return nil, 0, err
}
return activities, int(total), nil
}

View File

@@ -62,3 +62,11 @@ func GetCommentsWithUser(username string, page, pageSize int) ([]model.Comment,
totalPages := (int(total) + pageSize - 1) / pageSize
return comments, totalPages, nil
}
func GetCommentByID(commentID uint) (*model.Comment, error) {
var comment model.Comment
if err := db.Preload("User").Preload("Resource").First(&comment, commentID).Error; err != nil {
return nil, err
}
return &comment, nil
}

View File

@@ -34,7 +34,18 @@ func init() {
}
}
_ = db.AutoMigrate(&model.User{}, &model.Resource{}, &model.Image{}, &model.Tag{}, &model.Storage{}, &model.File{}, &model.UploadingFile{}, &model.Statistic{}, &model.Comment{})
_ = db.AutoMigrate(
&model.User{},
&model.Resource{},
&model.Image{},
&model.Tag{},
&model.Storage{},
&model.File{},
&model.UploadingFile{},
&model.Statistic{},
&model.Comment{},
&model.Activity{},
)
}
func GetDB() *gorm.DB {