Skip to content

Commit

Permalink
fix: favorite_group id naming; column naming; tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JingYiJun committed Mar 2, 2024
1 parent f886982 commit a1be05f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 30 deletions.
2 changes: 1 addition & 1 deletion apis/favourite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("收藏夹不存在")
}
}

Expand Down
30 changes: 15 additions & 15 deletions models/favorite_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions models/user_favorite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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("帖子不存在")
Expand Down Expand Up @@ -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()}),
Expand All @@ -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
Expand All @@ -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{}).
Expand All @@ -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
})
}

Expand All @@ -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("帖子不存在")
Expand Down Expand Up @@ -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
})
}
7 changes: 6 additions & 1 deletion tests/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit a1be05f

Please sign in to comment.