mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Add user file statistic.
This commit is contained in:
@@ -5,7 +5,8 @@ export interface User {
|
||||
avatar_path: string;
|
||||
is_admin: boolean;
|
||||
can_upload: boolean;
|
||||
uploads_count: number;
|
||||
resources_count: number;
|
||||
files_count: number;
|
||||
comments_count: number;
|
||||
bio: string;
|
||||
}
|
||||
|
@@ -89,7 +89,7 @@ function UserCard({ user }: { user: User }) {
|
||||
<p>
|
||||
<span className="text-sm font-bold mr-1">
|
||||
{" "}
|
||||
{user.uploads_count}
|
||||
{user.resources_count}
|
||||
</span>
|
||||
<span className="text-sm">Resources</span>
|
||||
<span className="mx-2"></span>
|
||||
|
@@ -77,6 +77,7 @@ func CreateFile(filename string, description string, resourceID uint, storageID
|
||||
if storageID == nil && redirectUrl == "" {
|
||||
return nil, errors.New("storageID and redirectUrl cannot be both empty")
|
||||
}
|
||||
|
||||
f := &model.File{
|
||||
UUID: uuid.NewString(),
|
||||
Filename: filename,
|
||||
@@ -88,9 +89,23 @@ func CreateFile(filename string, description string, resourceID uint, storageID
|
||||
Size: size,
|
||||
UserID: userID,
|
||||
}
|
||||
if err := db.Create(f).Error; err != nil {
|
||||
|
||||
err := db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Create(f).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
err := tx.Model(&model.User{}).Where("id = ?", userID).
|
||||
UpdateColumn("FilesCount", gorm.Expr("FilesCount + ?", 1)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return f, nil
|
||||
}
|
||||
|
||||
@@ -108,14 +123,19 @@ func GetFile(id string) (*model.File, error) {
|
||||
func DeleteFile(id string) error {
|
||||
f := &model.File{}
|
||||
if err := db.Where("uuid = ?", id).First(f).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return model.NewNotFoundError("file not found")
|
||||
}
|
||||
return err
|
||||
}
|
||||
if err := db.Delete(f).Error; err != nil {
|
||||
|
||||
if err := db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Delete(f).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Model(&model.User{}).Where("id = ?", f.UserID).
|
||||
UpdateColumn("FilesCount", gorm.Expr("FilesCount - ?", 1)).Error
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@@ -2,8 +2,9 @@ package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
@@ -13,7 +14,8 @@ type User struct {
|
||||
IsAdmin bool
|
||||
CanUpload bool
|
||||
AvatarVersion int
|
||||
UploadsCount int
|
||||
ResourcesCount int
|
||||
FilesCount int
|
||||
CommentsCount int
|
||||
Resources []Resource `gorm:"foreignKey:UserID"`
|
||||
Bio string
|
||||
@@ -26,7 +28,8 @@ type UserView struct {
|
||||
AvatarPath string `json:"avatar_path"`
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
CanUpload bool `json:"can_upload"`
|
||||
UploadsCount int `json:"uploads_count"`
|
||||
ResourcesCount int `json:"resources_count"`
|
||||
FilesCount int `json:"files_count"`
|
||||
CommentsCount int `json:"comments_count"`
|
||||
Bio string `json:"bio"`
|
||||
}
|
||||
@@ -44,7 +47,8 @@ func (u User) ToView() UserView {
|
||||
AvatarPath: fmt.Sprintf("/api/user/avatar/%d?v=%d", u.ID, u.AvatarVersion),
|
||||
IsAdmin: u.IsAdmin,
|
||||
CanUpload: u.CanUpload || u.IsAdmin,
|
||||
UploadsCount: u.UploadsCount,
|
||||
ResourcesCount: u.ResourcesCount,
|
||||
FilesCount: u.FilesCount,
|
||||
CommentsCount: u.CommentsCount,
|
||||
Bio: u.Bio,
|
||||
}
|
||||
|
Reference in New Issue
Block a user