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

@@ -16,6 +16,8 @@ type Resource struct {
Files []File `gorm:"foreignKey:ResourceID"`
UserID uint
User User
Views uint
Downloads uint
}
type ResourceView struct {
@@ -37,6 +39,8 @@ type ResourceDetailView struct {
Images []ImageView `json:"images"`
Files []FileView `json:"files"`
Author UserView `json:"author"`
Views uint `json:"views"`
Downloads uint `json:"downloads"`
}
func (r *Resource) ToView() ResourceView {
@@ -85,5 +89,7 @@ func (r *Resource) ToDetailView() ResourceDetailView {
Images: images,
Files: files,
Author: r.User.ToView(),
Views: r.Views,
Downloads: r.Downloads,
}
}

View File

@@ -13,16 +13,20 @@ type User struct {
IsAdmin bool
CanUpload bool
AvatarVersion int
UploadsCount int
CommentsCount int
Resources []Resource `gorm:"foreignKey:UserID"`
}
type UserView struct {
ID uint `json:"id"`
Username string `json:"username"`
CreatedAt time.Time `json:"created_at"`
AvatarPath string `json:"avatar_path"`
IsAdmin bool `json:"is_admin"`
CanUpload bool `json:"can_upload"`
ID uint `json:"id"`
Username string `json:"username"`
CreatedAt time.Time `json:"created_at"`
AvatarPath string `json:"avatar_path"`
IsAdmin bool `json:"is_admin"`
CanUpload bool `json:"can_upload"`
UploadsCount int `json:"uploads_count"`
CommentsCount int `json:"comments_count"`
}
type UserViewWithToken struct {
@@ -32,12 +36,14 @@ type UserViewWithToken struct {
func (u User) ToView() UserView {
return UserView{
ID: u.ID,
Username: u.Username,
CreatedAt: u.CreatedAt,
AvatarPath: fmt.Sprintf("/api/user/avatar/%d?v=%d", u.ID, u.AvatarVersion),
IsAdmin: u.IsAdmin,
CanUpload: u.CanUpload || u.IsAdmin,
ID: u.ID,
Username: u.Username,
CreatedAt: u.CreatedAt,
AvatarPath: fmt.Sprintf("/api/user/avatar/%d?v=%d", u.ID, u.AvatarVersion),
IsAdmin: u.IsAdmin,
CanUpload: u.CanUpload || u.IsAdmin,
UploadsCount: u.UploadsCount,
CommentsCount: u.CommentsCount,
}
}