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