refactor: renamed variables

- Update model references from `UserFavorites` to `UserFavorite` in multiple files.
- Adjust database queries and struct definitions accordingly to maintain consistency.
This commit is contained in:
SoXX 2024-08-09 22:43:58 +02:00
parent f798869ef5
commit 85d7905e8a
7 changed files with 13 additions and 13 deletions

View File

@ -62,7 +62,7 @@ func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthrov
return &otterError.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"} return &otterError.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
} }
userFavorite := models.UserFavorites{ userFavorite := models.UserFavorite{
UserID: string(anthroveUserID), UserID: string(anthroveUserID),
PostID: string(anthrovePostID), PostID: string(anthrovePostID),
} }
@ -106,7 +106,7 @@ func CheckReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthrove
return false, &otterError.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"} return false, &otterError.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"}
} }
result := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count) result := db.WithContext(ctx).Model(&models.UserFavorite{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return false, &otterError.NoDataFound{} return false, &otterError.NoDataFound{}

View File

@ -113,7 +113,7 @@ func GetUserFavoritesCount(ctx context.Context, db *gorm.DB, anthroveUserID mode
return 0, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} return 0, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
} }
result := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ?", string(anthroveUserID)).Count(&count) result := db.WithContext(ctx).Model(&models.UserFavorite{}).Where("user_id = ?", string(anthroveUserID)).Count(&count)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return 0, &otterError.NoDataFound{} return 0, &otterError.NoDataFound{}
@ -242,7 +242,7 @@ func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUse
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
} }
db.WithContext(ctx).Joins("RIGHT JOIN \"UserFavorites\" AS of ON \"Post\".id = of.post_id AND of.user_id = ?", anthroveUserID).Preload("References").Offset(skip).Limit(limit).Find(&favoritePosts) db.WithContext(ctx).Joins("RIGHT JOIN \"UserFavorite\" AS of ON \"Post\".id = of.post_id AND of.user_id = ?", anthroveUserID).Preload("References").Offset(skip).Limit(limit).Find(&favoritePosts)
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID, "anthrove_user_id": anthroveUserID,
@ -265,7 +265,7 @@ func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthrov
rows, err := db.WithContext(ctx).Raw( rows, err := db.WithContext(ctx).Raw(
`WITH user_posts AS ( `WITH user_posts AS (
SELECT post_id FROM "UserFavorites" WHERE user_id = $1 SELECT post_id FROM "UserFavorite" WHERE user_id = $1
) )
SELECT post_tags.tag_name AS tag_name, count(*) AS count, (SELECT tag_type FROM "Tag" WHERE "Tag".name = post_tags.tag_name LIMIT 1) AS tag_type FROM post_tags, user_posts WHERE post_tags.post_id IN (user_posts.post_id) GROUP BY post_tags.tag_name ORDER BY tag_type DESC, tag_name DESC`, anthroveUserID).Rows() SELECT post_tags.tag_name AS tag_name, count(*) AS count, (SELECT tag_type FROM "Tag" WHERE "Tag".name = post_tags.tag_name LIMIT 1) AS tag_type FROM post_tags, user_posts WHERE post_tags.post_id IN (user_posts.post_id) GROUP BY post_tags.tag_name ORDER BY tag_type DESC, tag_name DESC`, anthroveUserID).Rows()
if err != nil { if err != nil {

View File

@ -5,7 +5,7 @@ type Post struct {
BaseModel[PostID] BaseModel[PostID]
Rating Rating `json:"rating" gorm:"type:enum('safe','questionable','explicit')"` Rating Rating `json:"rating" gorm:"type:enum('safe','questionable','explicit')"`
Tags []Tag `json:"-" gorm:"many2many:post_tags;"` Tags []Tag `json:"-" gorm:"many2many:post_tags;"`
Favorites []UserFavorites `json:"-" gorm:"foreignKey:PostID"` Favorites []UserFavorite `json:"-" gorm:"foreignKey:PostID"`
References []PostReference `json:"references" gorm:"foreignKey:PostID"` References []PostReference `json:"references" gorm:"foreignKey:PostID"`
} }

View File

@ -7,7 +7,7 @@ func TestPost_TableName(t *testing.T) {
BaseModel BaseModel[PostID] BaseModel BaseModel[PostID]
Rating Rating Rating Rating
Tags []Tag Tags []Tag
Favorites []UserFavorites Favorites []UserFavorite
References []PostReference References []PostReference
} }
tests := []struct { tests := []struct {

View File

@ -3,8 +3,8 @@ package models
// User model // User model
type User struct { type User struct {
BaseModel[UserID] BaseModel[UserID]
Favorites []UserFavorites `json:"-" gorm:"foreignKey:UserID"` Favorites []UserFavorite `json:"-" gorm:"foreignKey:UserID"`
Sources []UserSource `json:"-" gorm:"foreignKey:UserID"` Sources []UserSource `json:"-" gorm:"foreignKey:UserID"`
} }
func (User) TableName() string { func (User) TableName() string {

View File

@ -17,14 +17,14 @@ func TestUserFavorite_TableName(t *testing.T) {
want string want string
}{ }{
{ {
name: "Test 1: Is name UserFavorites", name: "Test 1: Is name UserFavorite",
fields: fields{}, fields: fields{},
want: "UserFavorites", want: "UserFavorite",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
us := UserFavorites{ us := UserFavorite{
UserID: tt.fields.UserID, UserID: tt.fields.UserID,
PostID: tt.fields.PostID, PostID: tt.fields.PostID,
CreatedAt: tt.fields.CreatedAt, CreatedAt: tt.fields.CreatedAt,

View File

@ -5,7 +5,7 @@ import "testing"
func TestUser_TableName(t *testing.T) { func TestUser_TableName(t *testing.T) {
type fields struct { type fields struct {
BaseModel BaseModel[UserID] BaseModel BaseModel[UserID]
Favorites []UserFavorites Favorites []UserFavorite
Sources []UserSource Sources []UserSource
} }
tests := []struct { tests := []struct {