Compare commits
No commits in common. "1dc9b1fb7c8e49eaaacfcbab239116ac8df4b139" and "f798869ef5fcb9722d5b210b4c09a437e7a47c06" have entirely different histories.
1dc9b1fb7c
...
f798869ef5
@ -62,7 +62,7 @@ func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthrov
|
||||
return &otterError.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
|
||||
}
|
||||
|
||||
userFavorite := models.UserFavorite{
|
||||
userFavorite := models.UserFavorites{
|
||||
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.UserFavorite{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count)
|
||||
result := db.WithContext(ctx).Model(&models.UserFavorites{}).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{}
|
||||
|
@ -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.UserFavorite{}).Where("user_id = ?", string(anthroveUserID)).Count(&count)
|
||||
result := db.WithContext(ctx).Model(&models.UserFavorites{}).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 \"UserFavorite\" 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 \"UserFavorites\" 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 "UserFavorite" WHERE user_id = $1
|
||||
SELECT post_id FROM "UserFavorites" 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 {
|
||||
|
@ -1,125 +0,0 @@
|
||||
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
|
||||
}
|
@ -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 = ?", id)
|
||||
result := client.WithContext(ctx).First(&user, id)
|
||||
if result.Error != nil {
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return models.UserSource{}, &otterError.Database{Reason: otterError.NoDataFound}
|
||||
|
@ -3,18 +3,13 @@ package error
|
||||
import "fmt"
|
||||
|
||||
const (
|
||||
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"
|
||||
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"
|
||||
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 {
|
||||
|
@ -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 []UserFavorite `json:"-" gorm:"foreignKey:PostID"`
|
||||
Favorites []UserFavorites `json:"-" gorm:"foreignKey:PostID"`
|
||||
References []PostReference `json:"references" gorm:"foreignKey:PostID"`
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,7 @@ func TestPost_TableName(t *testing.T) {
|
||||
BaseModel BaseModel[PostID]
|
||||
Rating Rating
|
||||
Tags []Tag
|
||||
Favorites []UserFavorite
|
||||
Favorites []UserFavorites
|
||||
References []PostReference
|
||||
}
|
||||
tests := []struct {
|
||||
|
@ -3,7 +3,7 @@ package models
|
||||
// User model
|
||||
type User struct {
|
||||
BaseModel[UserID]
|
||||
Favorites []UserFavorite `json:"-" gorm:"foreignKey:UserID"`
|
||||
Favorites []UserFavorites `json:"-" gorm:"foreignKey:UserID"`
|
||||
Sources []UserSource `json:"-" gorm:"foreignKey:UserID"`
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,16 @@
|
||||
package models
|
||||
|
||||
type UserFavorite struct {
|
||||
import "time"
|
||||
|
||||
type UserFavorites 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 (UserFavorite) TableName() string {
|
||||
func (UserFavorites) TableName() string {
|
||||
return "UserFavorites"
|
||||
}
|
||||
|
@ -17,14 +17,14 @@ func TestUserFavorite_TableName(t *testing.T) {
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "Test 1: Is name UserFavorite",
|
||||
name: "Test 1: Is name UserFavorites",
|
||||
fields: fields{},
|
||||
want: "UserFavorite",
|
||||
want: "UserFavorites",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
us := UserFavorite{
|
||||
us := UserFavorites{
|
||||
UserID: tt.fields.UserID,
|
||||
PostID: tt.fields.PostID,
|
||||
CreatedAt: tt.fields.CreatedAt,
|
||||
|
@ -5,7 +5,7 @@ import "testing"
|
||||
func TestUser_TableName(t *testing.T) {
|
||||
type fields struct {
|
||||
BaseModel BaseModel[UserID]
|
||||
Favorites []UserFavorite
|
||||
Favorites []UserFavorites
|
||||
Sources []UserSource
|
||||
}
|
||||
tests := []struct {
|
||||
|
Loading…
x
Reference in New Issue
Block a user