diff --git a/internal/postgres/post.go b/internal/postgres/post.go index bf0fea4..32ee5af 100644 --- a/internal/postgres/post.go +++ b/internal/postgres/post.go @@ -32,60 +32,42 @@ func CreateAnthrovePostNode(ctx context.Context, db *gorm.DB, anthrovePost *mode func GetPostByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID string) (*models.Post, error) { if anthrovePostID == "" { - return false, fmt.Errorf("anthrovePostID is required") + return nil, fmt.Errorf("anthrovePostID is required") } - return executeCheckQuery(ctx, db, "id = ?", string(anthrovePostID)) + var post models.Post + err := db.WithContext(ctx).First(&post, "id = ?", anthrovePostID).Error + if err != nil { + return nil, err + } + + return &post, nil } -func GetPostBySourceURL(ctx context.Context, db *gorm.DB, sourceURL string) (*models.Post, bool, error) { +func GetPostBySourceURL(ctx context.Context, db *gorm.DB, sourceURL string) (*models.Post, error) { var post models.Post err := db.WithContext(ctx).Raw(`SELECT p.id AS id, p.rating as rating FROM "Post" AS p INNER JOIN "PostReference" AS pr ON p.id = pr.post_id AND pr.url = $1 LIMIT 1`, sourceURL).First(&post).Error if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { - return nil, false, nil + return nil, nil } - return nil, false, err + return nil, err } - var post models.Post - err = db.WithContext(ctx).First(&post, "id = ?", postRef.PostID).Error - - if err != nil { - return nil, false, err - } - - pgPost := graphModels.AnthrovePost{ - PostID: models.AnthrovePostID(post.ID), - Rating: post.Rating, - } - - //TODO Now test it! :D - // Naaa - // D: - return &pgPost, true, nil + return &post, nil } func GetPostBySourceID(ctx context.Context, db *gorm.DB, sourceID string) (*models.Post, error) { - return executeCheckQuery(ctx, db, "source_id = ?", sourceID) -} + var post models.Post + err := db.WithContext(ctx).Raw(`SELECT p.id AS id, p.rating as rating FROM "Post" AS p INNER JOIN "PostReference" AS pr ON p.id = pr.post_id AND pr.source_id = $1 LIMIT 1`, sourceID).First(&post).Error -func executeCheckQuery(ctx context.Context, db *gorm.DB, query string, args ...interface{}) (bool, error) { - var count int64 - - err := db.WithContext(ctx).Model(&models.Post{}).Where(query, args...).Count(&count).Error if err != nil { - return false, err + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil, nil + } + return nil, err } - exists := count > 0 - - log.WithFields(log.Fields{ - "query": query, - "args": args, - "exists": exists, - }).Trace("database: executed check query") - - return exists, nil + return &post, nil } diff --git a/internal/postgres/tag.go b/internal/postgres/tag.go index 124742e..40e0f68 100644 --- a/internal/postgres/tag.go +++ b/internal/postgres/tag.go @@ -30,12 +30,6 @@ func CreateTagNodeWitRelation(ctx context.Context, db *gorm.DB, PostID models.An return fmt.Errorf("PostID is empty") } - resultTag := db.WithContext(ctx).Where(tag).FirstOrCreate(tag) - - if resultTag.Error != nil { - return resultTag.Error - } - pgPost := models.Post{ BaseModel: models.BaseModel{ ID: string(PostID), diff --git a/internal/postgres/user.go b/internal/postgres/user.go index c7b0586..8afd07f 100644 --- a/internal/postgres/user.go +++ b/internal/postgres/user.go @@ -37,19 +37,6 @@ func CreateUserNodeWithSourceRelation(ctx context.Context, db *gorm.DB, anthrove return fmt.Errorf("anthroveUserID cannot be empty") } - user := models.User{ - BaseModel: models.BaseModel{ - ID: string(anthroveUserID), - }, - } - - if err := db.WithContext(ctx).FirstOrCreate(&user).Error; err != nil { - log.WithFields(log.Fields{ - "anthrove_user_id": anthroveUserID, - }).Error("database: failed to find or create user") - return err - } - source := models.Source{ Domain: sourceDomain, } @@ -62,7 +49,7 @@ func CreateUserNodeWithSourceRelation(ctx context.Context, db *gorm.DB, anthrove } userSource := models.UserSource{ - UserID: user.ID, + User: models.User{BaseModel: models.BaseModel{ID: string(anthroveUserID)}}, SourceID: source.ID, AccountUsername: username, AccountID: userID, diff --git a/pkg/models/userSource.go b/pkg/models/userSource.go index 97793d6..59788a1 100644 --- a/pkg/models/userSource.go +++ b/pkg/models/userSource.go @@ -1,6 +1,7 @@ package models type UserSource struct { + User User `gorm:"foreignKey:ID;references:UserID"` UserID string `gorm:"primaryKey"` Source Source `gorm:"foreignKey:ID;references:SourceID"` SourceID string `gorm:"primaryKey"`