From a1be05f5aabe3e6befab9d90f87fcdda1b865a5c Mon Sep 17 00:00:00 2001 From: Ke Chen Date: Sat, 2 Mar 2024 23:02:30 +0800 Subject: [PATCH] fix: favorite_group id naming; column naming; tests --- apis/favourite/api.go | 2 +- models/favorite_group.go | 30 +++++++++++++++--------------- models/user_favorite.go | 26 +++++++++++++------------- tests/init.go | 7 ++++++- 4 files changed, 35 insertions(+), 30 deletions(-) diff --git a/apis/favourite/api.go b/apis/favourite/api.go index 8cbadb2..ba03669 100644 --- a/apis/favourite/api.go +++ b/apis/favourite/api.go @@ -32,7 +32,7 @@ func ListFavorites(c *fiber.Ctx) error { } if query.FavoriteGroupID != nil { if !IsFavoriteGroupExist(DB, userID, *query.FavoriteGroupID) { - return common.Forbidden("收藏夹不存在") + return common.NotFound("收藏夹不存在") } } diff --git a/models/favorite_group.go b/models/favorite_group.go index 298274a..6971b47 100644 --- a/models/favorite_group.go +++ b/models/favorite_group.go @@ -9,13 +9,13 @@ import ( ) type FavoriteGroup struct { - ID int `json:"id" gorm:"primaryKey"` - UserID int `json:"user_id" gorm:"primaryKey"` - Name string `json:"name" gorm:"not null;size:64" default:"默认"` - CreatedAt time.Time `json:"time_created"` - UpdatedAt time.Time `json:"time_updated"` - Deleted bool `json:"deleted" gorm:"default:false"` - Count int `json:"count" gorm:"default:0"` + FavoriteGroupID int `json:"favorite_group_id" gorm:"primaryKey"` + UserID int `json:"user_id" gorm:"primaryKey"` + Name string `json:"name" gorm:"not null;size:64" default:"默认"` + CreatedAt time.Time `json:"time_created"` + UpdatedAt time.Time `json:"time_updated"` + Deleted bool `json:"deleted" gorm:"default:false"` + Count int `json:"count" gorm:"default:0"` } const MaxGroupPerUser = 10 @@ -49,10 +49,10 @@ func DeleteUserFavoriteGroup(tx *gorm.DB, userID int, groupID int) (err error) { func CreateDefaultFavoriteGroup(tx *gorm.DB, userID int) (err error) { return tx.Clauses(dbresolver.Write).Transaction(func(tx *gorm.DB) error { err = tx.Create(&FavoriteGroup{ - UserID: userID, - Name: "默认收藏夹", - ID: 0, - CreatedAt: time.Now(), + UserID: userID, + Name: "默认收藏夹", + FavoriteGroupID: 0, + CreatedAt: time.Now(), }).Error if err != nil { return err @@ -82,10 +82,10 @@ func AddUserFavoriteGroup(tx *gorm.DB, userID int, name string) (err error) { } err = tx.Create(&FavoriteGroup{ - UserID: userID, - Name: name, - ID: groupID, - CreatedAt: time.Now(), + UserID: userID, + Name: name, + FavoriteGroupID: groupID, + CreatedAt: time.Now(), }).Error if err != nil { return err diff --git a/models/user_favorite.go b/models/user_favorite.go index 13443cf..460c179 100644 --- a/models/user_favorite.go +++ b/models/user_favorite.go @@ -26,7 +26,7 @@ func (UserFavorite) TableName() string { func IsFavoriteGroupExist(tx *gorm.DB, userID int, favoriteGroupID int) bool { var num int64 - tx.Model(&FavoriteGroup{}).Where("user_id = ? AND id = ? AND deleted = false", userID, favoriteGroupID).Count(&num) + tx.Model(&FavoriteGroup{}).Where("user_id = ? AND favorite_group_id = ? AND deleted = false", userID, favoriteGroupID).Count(&num) return num > 0 } @@ -36,7 +36,7 @@ func ModifyUserFavorite(tx *gorm.DB, userID int, holeIDs []int, favoriteGroupID return nil } if !IsFavoriteGroupExist(tx, userID, favoriteGroupID) { - return common.Forbidden("收藏夹不存在") + return common.NotFound("收藏夹不存在") } if !IsHolesExist(tx, holeIDs) { return common.Forbidden("帖子不存在") @@ -93,16 +93,16 @@ func ModifyUserFavorite(tx *gorm.DB, userID int, holeIDs []int, favoriteGroupID return err } } - return tx.Model(&FavoriteGroup{}).Where("user_id = ? AND id = ?", userID, favoriteGroupID).Update("number", len(holeIDs)).Error + return tx.Model(&FavoriteGroup{}).Where("user_id = ? AND favorite_group_id = ?", userID, favoriteGroupID).Update("count", len(holeIDs)).Error }) } func AddUserFavorite(tx *gorm.DB, userID int, holeID int, favoriteGroupID int) error { if !IsFavoriteGroupExist(tx, userID, favoriteGroupID) { - return common.Forbidden("收藏夹不存在") + return common.NotFound("收藏夹不存在") } if !IsHolesExist(tx, []int{holeID}) { - return common.Forbidden("帖子不存在") + return common.NotFound("帖子不存在") } var err = tx.Clauses(clause.OnConflict{ DoUpdates: clause.Assignments(Map{"created_at": time.Now()}), @@ -115,7 +115,7 @@ func AddUserFavorite(tx *gorm.DB, userID int, holeID int, favoriteGroupID int) e return err } return tx.Clauses(dbresolver.Write).Model(&FavoriteGroup{}). - Where("user_id = ? AND id = ?", userID, favoriteGroupID).Update("number", gorm.Expr("number + 1")).Error + Where("user_id = ? AND favorite_group_id = ?", userID, favoriteGroupID).Update("count", gorm.Expr("count + 1")).Error } // UserGetFavoriteData get all favorite data of a user @@ -129,7 +129,7 @@ func UserGetFavoriteData(tx *gorm.DB, userID int) ([]int, error) { // UserGetFavoriteDataByFavoriteGroup get favorite data in specific favorite group func UserGetFavoriteDataByFavoriteGroup(tx *gorm.DB, userID int, favoriteGroupID int) ([]int, error) { if !IsFavoriteGroupExist(tx, userID, favoriteGroupID) { - return nil, common.Forbidden("收藏夹不存在") + return nil, common.NotFound("收藏夹不存在") } data := make([]int, 0, 10) err := tx.Clauses(dbresolver.Write).Model(&UserFavorite{}). @@ -142,17 +142,17 @@ func UserGetFavoriteDataByFavoriteGroup(tx *gorm.DB, userID int, favoriteGroupID // otherwise, delete the favorite in the specific favorite group func DeleteUserFavorite(tx *gorm.DB, userID int, holeID int, favoriteGroupID int) error { if !IsFavoriteGroupExist(tx, userID, favoriteGroupID) { - return common.Forbidden("收藏夹不存在") + return common.NotFound("收藏夹不存在") } if !IsHolesExist(tx, []int{holeID}) { - return common.Forbidden("帖子不存在") + return common.NotFound("帖子不存在") } return tx.Clauses(dbresolver.Write).Transaction(func(tx *gorm.DB) error { err := tx.Delete(&UserFavorite{UserID: userID, HoleID: holeID, FavoriteGroupID: favoriteGroupID}).Error if err != nil { return err } - return tx.Clauses(dbresolver.Write).Model(&FavoriteGroup{}).Where("user_id = ? AND id = ?", userID, favoriteGroupID).Update("number", gorm.Expr("number - 1")).Error + return tx.Clauses(dbresolver.Write).Model(&FavoriteGroup{}).Where("user_id = ? AND favorite_group_id = ?", userID, favoriteGroupID).Update("count", gorm.Expr("count - 1")).Error }) } @@ -165,7 +165,7 @@ func MoveUserFavorite(tx *gorm.DB, userID int, holeIDs []int, fromFavoriteGroupI return nil } if !IsFavoriteGroupExist(tx, userID, fromFavoriteGroupID) || !IsFavoriteGroupExist(tx, userID, toFavoriteGroupID) { - return common.Forbidden("收藏夹不存在") + return common.NotFound("收藏夹不存在") } if !IsHolesExist(tx, holeIDs) { return common.Forbidden("帖子不存在") @@ -198,10 +198,10 @@ func MoveUserFavorite(tx *gorm.DB, userID int, holeIDs []int, fromFavoriteGroupI return err } } - err = tx.Model(&FavoriteGroup{}).Where("user_id = ? AND id = ?", userID, fromFavoriteGroupID).Update("number", gorm.Expr("number - ?", len(removingHoleIDs))).Error + err = tx.Model(&FavoriteGroup{}).Where("user_id = ? AND favorite_group_id = ?", userID, fromFavoriteGroupID).Update("count", gorm.Expr("count - ?", len(removingHoleIDs))).Error if err != nil { return err } - return tx.Model(&FavoriteGroup{}).Where("user_id = ? AND id = ?", userID, toFavoriteGroupID).Update("number", gorm.Expr("number + ?", len(removingHoleIDs))).Error + return tx.Model(&FavoriteGroup{}).Where("user_id = ? AND favorite_group_id = ?", userID, toFavoriteGroupID).Update("count", gorm.Expr("count + ?", len(removingHoleIDs))).Error }) } diff --git a/tests/init.go b/tests/init.go index 4c645c8..99d86e5 100644 --- a/tests/init.go +++ b/tests/init.go @@ -131,12 +131,17 @@ func initTestTags() { } func initTestFavorites() { + favoriteGroup := FavoriteGroup{Name: "test", UserID: 1, FavoriteGroupID: 0} + err := DB.Create(&favoriteGroup).Error + if err != nil { + log.Fatal().Err(err).Send() + } userFavorites := make([]UserFavorite, 10) for i := range userFavorites { userFavorites[i].HoleID = i + 1 userFavorites[i].UserID = 1 } - err := DB.Create(&userFavorites).Error + err = DB.Create(&userFavorites).Error if err != nil { log.Fatal().Err(err).Send() }