mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 04:17:23 +00:00
Add activities page.
This commit is contained in:
53
server/dao/activity.go
Normal file
53
server/dao/activity.go
Normal 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
|
||||
}
|
@@ -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
|
||||
}
|
||||
|
@@ -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 {
|
||||
|
Reference in New Issue
Block a user