Compare commits

...

5 Commits

Author SHA1 Message Date
1dc9b1fb7c feat(database): favorites
Some checks failed
Gitea Build Check / Build (push) Failing after 27s
- Added functionality
2024-08-09 22:47:02 +02:00
7720110a0a refactor: Update validation error messages for clarity and consistency 2024-08-09 22:45:25 +02:00
b6c037cb22 refactor: Refactor UserFavorites model to UserFavorite for clarity and consistency
- Rename `UserFavorites` struct to `UserFavorite`
- Update the `TableName` method to reflect the new struct name
2024-08-09 22:45:12 +02:00
3e13046706 fix(query): added conditions
added the condition for the id
2024-08-09 22:44:55 +02:00
85d7905e8a refactor: renamed variables
- Update model references from `UserFavorites` to `UserFavorite` in multiple files.
- Adjust database queries and struct definitions accordingly to maintain consistency.
2024-08-09 22:43:58 +02:00
11 changed files with 153 additions and 26 deletions

View File

@ -62,7 +62,7 @@ func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthrov
return &otterError.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
}
userFavorite := models.UserFavorites{
userFavorite := models.UserFavorite{
UserID: string(anthroveUserID),
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"}
}
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 errors.Is(result.Error, gorm.ErrRecordNotFound) {
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}
}
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 errors.Is(result.Error, gorm.ErrRecordNotFound) {
return 0, &otterError.NoDataFound{}
@ -242,7 +242,7 @@ func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUse
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{
"anthrove_user_id": anthroveUserID,
@ -265,7 +265,7 @@ func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthrov
rows, err := db.WithContext(ctx).Raw(
`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()
if err != nil {

125
pkg/database/favorite.go Normal file
View File

@ -0,0 +1,125 @@
package database
import (
"context"
"errors"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
"gorm.io/gorm"
)
func CreateUserFavorite(ctx context.Context, userFav models.UserFavorite) (models.UserFavorite, error) {
if client == nil {
return models.UserFavorite{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
result := client.WithContext(ctx).Create(&userFav)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.UserFavorite{}, &otterError.Database{Reason: otterError.DuplicateKey}
}
return models.UserFavorite{}, result.Error
}
return userFav, nil
}
func CreateUserFavoriteInBatch(ctx context.Context, userFav []models.UserFavorite, batchSize int) error {
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if userFav == nil {
return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteListIsEmpty}
}
if len(userFav) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteListIsEmpty}
}
if batchSize == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}
}
result := client.WithContext(ctx).CreateInBatches(&userFav, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.Database{Reason: otterError.DuplicateKey}
}
return result.Error
}
return nil
}
func UpdateUserFavorite(ctx context.Context, userFav models.UserFavorite) error {
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(userFav.ID) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty}
}
if !userFav.DeletedAt.Valid {
return nil
}
result := client.WithContext(ctx).Model(&userFav).Update("deleted_at", gorm.DeletedAt{})
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.Database{Reason: otterError.NoDataFound}
}
return result.Error
}
return nil
}
func GetUserFavoritesByID(ctx context.Context, id models.UserFavoriteID) (models.UserFavorite, error) {
var userFavorites models.UserFavorite
if client == nil {
return models.UserFavorite{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(id) == 0 {
return models.UserFavorite{}, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty}
}
result := client.WithContext(ctx).First(&userFavorites, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.UserFavorite{}, &otterError.Database{Reason: otterError.NoDataFound}
}
return models.UserFavorite{}, result.Error
}
return userFavorites, nil
}
func DeleteUserFavorite(ctx context.Context, id models.UserFavoriteID) error {
var userFavorite models.UserFavorite
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(id) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty}
}
if len(id) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDToShort}
}
result := client.WithContext(ctx).Delete(&userFavorite, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.Database{Reason: otterError.NoDataFound}
}
return result.Error
}
return nil
}

View File

@ -70,7 +70,7 @@ func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.User
return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort}
}
result := client.WithContext(ctx).First(&user, id)
result := client.WithContext(ctx).First(&user, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.UserSource{}, &otterError.Database{Reason: otterError.NoDataFound}

View File

@ -3,13 +3,18 @@ package error
import "fmt"
const (
UserIDIsEmpty = "anthrovePostID cannot be empty"
UserIDToShort = "anthrovePostID needs to be 25 characters long"
SourceIDEmpty = "anthroveSourceID cannot be empty"
SourceIDToShort = "anthroveSourceID needs to be 25 characters long"
UserSourceIDEmpty = "anthroveUserSourceID cannot be empty"
UserSourceIDToShort = "anthroveUserSourceID needs to be 25 characters long"
TagIDEmpty = "tagID cannot be empty"
UserIDIsEmpty = "PostID cannot be empty"
UserIDToShort = "PostID needs to be 25 characters long"
SourceIDEmpty = "SourceID cannot be empty"
SourceIDToShort = "SourceID needs to be 25 characters long"
UserSourceIDEmpty = "UserSourceID cannot be empty"
UserSourceIDToShort = "UserSourceID needs to be 25 characters long"
TagIDEmpty = "tagID cannot be empty"
UserFavoriteListIsEmpty = "userFavoriteList cannot be empty"
UserFavoriteIDIsEmpty = "userFavoriteID cannot be empty"
UserFavoriteIDToShort = "UserFavoriteID needs to be 25 characters long"
BatchSizeIsEmpty = "batchSize cannot be empty"
)
type EntityValidationFailed struct {

View File

@ -5,7 +5,7 @@ type Post struct {
BaseModel[PostID]
Rating Rating `json:"rating" gorm:"type:enum('safe','questionable','explicit')"`
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"`
}

View File

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

View File

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

View File

@ -1,16 +1,13 @@
package models
import "time"
type UserFavorites struct {
type UserFavorite struct {
BaseModel[UserFavoriteID]
UserID string `json:"user_id"`
PostID string `json:"post_id"`
UserSourceID UserSourceID `json:"user_source_id"`
UserSource UserSource `json:"-" gorm:"foreignKey:ID;references:UserSourceID"`
CreatedAt time.Time `json:"created_at"`
}
func (UserFavorites) TableName() string {
func (UserFavorite) TableName() string {
return "UserFavorites"
}

View File

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

View File

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