refactor(postgres): Refactor post retrieval functions and remove unnecessary code in tag and user creation
Signed-off-by: soxx <soxx@fenpa.ws>
This commit is contained in:
parent
9471941e02
commit
bb7cb23c2a
@ -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
|
||||
}
|
||||
|
||||
func GetPostBySourceURL(ctx context.Context, db *gorm.DB, sourceURL string) (*models.Post, bool, error) {
|
||||
return &post, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -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),
|
||||
|
@ -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,
|
||||
|
@ -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"`
|
||||
|
Reference in New Issue
Block a user