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) {
|
func GetPostByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID string) (*models.Post, error) {
|
||||||
if anthrovePostID == "" {
|
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
|
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
|
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 err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return nil, false, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
return nil, false, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var post models.Post
|
return &post, nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPostBySourceID(ctx context.Context, db *gorm.DB, sourceID string) (*models.Post, error) {
|
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 {
|
if err != nil {
|
||||||
return false, err
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
exists := count > 0
|
return &post, nil
|
||||||
|
|
||||||
log.WithFields(log.Fields{
|
|
||||||
"query": query,
|
|
||||||
"args": args,
|
|
||||||
"exists": exists,
|
|
||||||
}).Trace("database: executed check query")
|
|
||||||
|
|
||||||
return exists, nil
|
|
||||||
}
|
}
|
||||||
|
@ -30,12 +30,6 @@ func CreateTagNodeWitRelation(ctx context.Context, db *gorm.DB, PostID models.An
|
|||||||
return fmt.Errorf("PostID is empty")
|
return fmt.Errorf("PostID is empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
resultTag := db.WithContext(ctx).Where(tag).FirstOrCreate(tag)
|
|
||||||
|
|
||||||
if resultTag.Error != nil {
|
|
||||||
return resultTag.Error
|
|
||||||
}
|
|
||||||
|
|
||||||
pgPost := models.Post{
|
pgPost := models.Post{
|
||||||
BaseModel: models.BaseModel{
|
BaseModel: models.BaseModel{
|
||||||
ID: string(PostID),
|
ID: string(PostID),
|
||||||
|
@ -37,19 +37,6 @@ func CreateUserNodeWithSourceRelation(ctx context.Context, db *gorm.DB, anthrove
|
|||||||
return fmt.Errorf("anthroveUserID cannot be empty")
|
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{
|
source := models.Source{
|
||||||
Domain: sourceDomain,
|
Domain: sourceDomain,
|
||||||
}
|
}
|
||||||
@ -62,7 +49,7 @@ func CreateUserNodeWithSourceRelation(ctx context.Context, db *gorm.DB, anthrove
|
|||||||
}
|
}
|
||||||
|
|
||||||
userSource := models.UserSource{
|
userSource := models.UserSource{
|
||||||
UserID: user.ID,
|
User: models.User{BaseModel: models.BaseModel{ID: string(anthroveUserID)}},
|
||||||
SourceID: source.ID,
|
SourceID: source.ID,
|
||||||
AccountUsername: username,
|
AccountUsername: username,
|
||||||
AccountID: userID,
|
AccountID: userID,
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
type UserSource struct {
|
type UserSource struct {
|
||||||
|
User User `gorm:"foreignKey:ID;references:UserID"`
|
||||||
UserID string `gorm:"primaryKey"`
|
UserID string `gorm:"primaryKey"`
|
||||||
Source Source `gorm:"foreignKey:ID;references:SourceID"`
|
Source Source `gorm:"foreignKey:ID;references:SourceID"`
|
||||||
SourceID string `gorm:"primaryKey"`
|
SourceID string `gorm:"primaryKey"`
|
||||||
|
Reference in New Issue
Block a user