BREAKING CHANGE: V2 of thr SDK #12

Merged
fenpaws merged 124 commits from develop/postgresql into main 2024-07-01 20:46:28 +00:00
5 changed files with 46 additions and 66 deletions
Showing only changes of commit 2621eea00c - Show all commits

View File

@ -5,17 +5,16 @@ import (
"errors"
"fmt"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
func CreateAnthrovePostNode(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, anthroveRating models.Rating) error {
func CreateAnthrovePostNode(ctx context.Context, db *gorm.DB, anthrovePost *models.Post) error {
post := models.Post{
BaseModel: models.BaseModel{
ID: string(anthrovePostID),
ID: string(anthrovePost.ID),
},
Rating: anthroveRating,
Rating: anthrovePost.Rating,
}
err := db.WithContext(ctx).Create(&post).Error
@ -24,14 +23,14 @@ func CreateAnthrovePostNode(ctx context.Context, db *gorm.DB, anthrovePostID mod
}
log.WithFields(log.Fields{
"anthrove_post_id": anthrovePostID,
"anthrove_post_rating": anthroveRating,
"anthrove_post_id": anthrovePost.ID,
"anthrove_post_rating": anthrovePost.Rating,
}).Trace("database: created anthrove post")
return nil
}
func CheckIfAnthrovePostNodeExistsByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID) (bool, error) {
func GetPostByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID string) (*models.Post, error) {
if anthrovePostID == "" {
return false, fmt.Errorf("anthrovePostID is required")
}
@ -39,9 +38,9 @@ func CheckIfAnthrovePostNodeExistsByAnthroveID(ctx context.Context, db *gorm.DB,
return executeCheckQuery(ctx, db, "id = ?", string(anthrovePostID))
}
func CheckIfAnthrovePostNodeExistsBySourceURL(ctx context.Context, db *gorm.DB, sourceURL string) (*graphModels.AnthrovePost, bool, error) {
postRef := models.PostReference{}
err := db.WithContext(ctx).Model(&models.PostReference{}).Where("url = ?", sourceURL).First(&postRef).Error
func GetPostBySourceURL(ctx context.Context, db *gorm.DB, sourceURL string) (*models.Post, bool, 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) {
@ -68,7 +67,7 @@ func CheckIfAnthrovePostNodeExistsBySourceURL(ctx context.Context, db *gorm.DB,
return &pgPost, true, nil
}
func CheckIfAnthrovePostNodeExistsBySourceID(ctx context.Context, db *gorm.DB, sourceID string) (bool, error) {
func GetPostBySourceID(ctx context.Context, db *gorm.DB, sourceID string) (*models.Post, error) {
return executeCheckQuery(ctx, db, "source_id = ?", sourceID)
}

View File

@ -3,17 +3,16 @@ package postgres
import (
"context"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
func EstablishAnthrovePostToSourceLink(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *graphModels.AnthrovePostRelationship) error {
func EstablishAnthrovePostToSourceLink(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, sourceDomain string, anthrovePostRelationship *models.PostReference) error {
var source models.Source
var err error
// Find the source
err = db.WithContext(ctx).Where("domain = ?", anthroveSourceDomain).First(&source).Error
err = db.WithContext(ctx).Where("domain = ?", sourceDomain).First(&source).Error
if err != nil {
return err
}
@ -22,7 +21,7 @@ func EstablishAnthrovePostToSourceLink(ctx context.Context, db *gorm.DB, anthrov
err = db.WithContext(ctx).Create(models.PostReference{
PostID: string(anthrovePostID),
SourceID: source.ID,
URL: anthrovePostRelationship.Url,
URL: anthrovePostRelationship.URL,
}).Error
if err != nil {
@ -31,13 +30,13 @@ func EstablishAnthrovePostToSourceLink(ctx context.Context, db *gorm.DB, anthrov
log.WithFields(log.Fields{
"anthrove_post_id": anthrovePostID,
"anthrove_source_domain": anthroveSourceDomain,
"anthrove_source_domain": sourceDomain,
}).Trace("database: created anthrove post to source link")
return nil
}
func EstablishUserToPostLink(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error {
func EstablishUserToPostLink(ctx context.Context, db *gorm.DB, anthroveUserID string, anthrovePostID string) error {
userFavorite := models.UserFavorite{
UserID: string(anthroveUserID),
PostID: string(anthrovePostID),

View File

@ -57,7 +57,7 @@ func CreateTagNodeWitRelation(ctx context.Context, db *gorm.DB, PostID models.An
return nil
}
func GetTags(ctx context.Context, db *gorm.DB) ([]models.Tag, error) {
func GetTags(ctx context.Context, db *gorm.DB) ([]models.TagsWithFrequency, error) {
var tags []models.Tag
result := db.WithContext(ctx).Find(&tags)
if result.Error != nil {

View File

@ -43,15 +43,15 @@ type OtterSpace interface {
// CheckPostNodeExistsByAnthroveID checks if a post node exists in the OtterSpace database by its Anthrove ID.
// It returns the post if it exists, a boolean indicating whether the post was found, and an error if the operation fails.
CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *models.Post) (*models.Post, bool, error)
CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *models.Post) (*models.Post, error)
// CheckPostNodeExistsBySourceURL checks if a post node exists in the OtterSpace database by its source URL.
// It returns the post if it exists, a boolean indicating whether the post was found, and an error if the operation fails.
CheckPostNodeExistsBySourceURL(ctx context.Context, sourceUrl string) (*models.Post, bool, error)
CheckPostNodeExistsBySourceURL(ctx context.Context, sourceUrl string) (*models.Post, error)
// CheckPostNodeExistsBySourceID checks if a post node exists in the OtterSpace database by its source ID.
// It returns the post if it exists, a boolean indicating whether the post was found, and an error if the operation fails.
CheckPostNodeExistsBySourceID(ctx context.Context, sourcePostID string) (*models.Post, bool, error)
CheckPostNodeExistsBySourceID(ctx context.Context, sourcePostID string) (*models.Post, error)
// GetUserFavoriteCount retrieves the count of a user's favorite posts from the OtterSpace database.
// It returns the count and an error if the operation fails.

View File

@ -6,9 +6,7 @@ import (
"embed"
"fmt"
"git.dragse.it/anthrove/otter-space-sdk/internal/postgres"
"git.dragse.it/anthrove/otter-space-sdk/internal/utils"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels"
_ "github.com/lib/pq"
migrate "github.com/rubenv/sql-migrate"
log "github.com/sirupsen/logrus"
@ -70,61 +68,55 @@ func (p *postgresqlConnection) AddUserWithRelationToSource(ctx context.Context,
return postgres.CreateUserNodeWithSourceRelation(ctx, p.db, anthroveUserID, sourceDomain, userID, userID)
}
func (p *postgresqlConnection) AddSource(ctx context.Context, anthroveSource *graphModels.AnthroveSource) error {
return postgres.CreateSourceNode(ctx, p.db, source)
func (p *postgresqlConnection) AddSource(ctx context.Context, anthroveSource *models.Source) error {
return postgres.CreateSourceNode(ctx, p.db, anthroveSource)
}
func (p *postgresqlConnection) AddPost(ctx context.Context, anthrovePost *graphModels.AnthrovePost) error {
return postgres.CreateAnthrovePostNode(ctx, p.db, anthrovePost.PostID, anthrovePost.Rating)
func (p *postgresqlConnection) AddPost(ctx context.Context, anthrovePost *models.Post) error {
return postgres.CreateAnthrovePostNode(ctx, p.db, anthrovePost)
}
func (p *postgresqlConnection) AddTagWithRelationToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *graphModels.AnthroveTag) error {
return postgres.CreateTagNodeWitRelation(ctx, p.db, anthrovePostID, utils.GraphConvertTag(anthroveTag))
func (p *postgresqlConnection) AddTagWithRelationToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error {
return postgres.CreateTagNodeWitRelation(ctx, p.db, anthrovePostID, anthroveTag)
}
func (p *postgresqlConnection) LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *graphModels.AnthrovePostRelationship) error {
return postgres.EstablishAnthrovePostToSourceLink(ctx, p.db, anthrovePostID, anthroveSourceDomain, anthrovePostRelationship)
func (p *postgresqlConnection) LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain string, anthrovePostRelationship *models.PostReference) error {
return postgres.EstablishAnthrovePostToSourceLink(ctx, p.db, anthrovePostID, sourceDomain, anthrovePostRelationship)
}
func (p *postgresqlConnection) LinkUserWithPost(ctx context.Context, anthroveUser *graphModels.AnthroveUser, anthrovePost *graphModels.AnthrovePost) error {
return postgres.EstablishUserToPostLink(ctx, p.db, anthroveUser.UserID, anthrovePost.PostID)
func (p *postgresqlConnection) LinkUserWithPost(ctx context.Context, anthroveUser *models.User, anthrovePost *models.Post) error {
return postgres.EstablishUserToPostLink(ctx, p.db, anthroveUser.ID, anthrovePost.ID)
}
func (p *postgresqlConnection) CheckUserPostLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID string, sourceUrl string) (bool, error) {
return postgres.CheckUserToPostLink(ctx, p.db, anthroveUserID, models.AnthrovePostID(sourcePostID))
}
func (p *postgresqlConnection) CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *graphModels.AnthrovePost) (*graphModels.AnthrovePost, bool, error) {
exists, err := postgres.CheckIfAnthrovePostNodeExistsByAnthroveID(ctx, p.db, anthrovePost.PostID)
return anthrovePost, exists, err
func (p *postgresqlConnection) CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *models.Post) (*models.Post, error) {
return postgres.GetPostByAnthroveID(ctx, p.db, anthrovePost.ID)
}
// CheckPostNodeExistsBySourceURL NOT WORKING! TODO!
func (p *postgresqlConnection) CheckPostNodeExistsBySourceURL(ctx context.Context, sourceUrl string) (*graphModels.AnthrovePost, bool, error) {
post, exists, err := postgres.CheckIfAnthrovePostNodeExistsBySourceURL(ctx, p.db, sourceUrl)
return post, exists, err
func (p *postgresqlConnection) CheckPostNodeExistsBySourceURL(ctx context.Context, sourceUrl string) (*models.Post, error) {
return postgres.GetPostBySourceURL(ctx, p.db, sourceUrl)
}
// CheckPostNodeExistsBySourceID NOT WORKING! TODO!
func (p *postgresqlConnection) CheckPostNodeExistsBySourceID(ctx context.Context, sourcePostID string) (*graphModels.AnthrovePost, bool, error) {
var post graphModels.AnthrovePost
exists, err := postgres.CheckIfAnthrovePostNodeExistsBySourceID(ctx, p.db, sourcePostID)
return &post, exists, err
func (p *postgresqlConnection) CheckPostNodeExistsBySourceID(ctx context.Context, sourcePostID string) (*models.Post, error) {
return postgres.GetPostBySourceID(ctx, p.db, sourcePostID)
}
func (p *postgresqlConnection) GetUserFavoriteCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) {
return postgres.GetUserFavoritesCount(ctx, p.db, anthroveUserID)
}
func (p *postgresqlConnection) GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]graphModels.AnthroveUserRelationship, error) {
func (p *postgresqlConnection) GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error) {
return postgres.GetUserSourceLink(ctx, p.db, anthroveUserID)
}
func (p *postgresqlConnection) GetSpecifiedUserSourceLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]graphModels.AnthroveUserRelationship, error) {
func (p *postgresqlConnection) GetSpecifiedUserSourceLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.UserSource, error) {
return postgres.GetSpecifiedUserSourceLink(ctx, p.db, anthroveUserID, sourceDisplayName)
}
func (p *postgresqlConnection) GetAnthroveUser(ctx context.Context, anthroveUserID models.AnthroveUserID) (*graphModels.AnthroveUser, error) {
func (p *postgresqlConnection) GetAnthroveUser(ctx context.Context, anthroveUserID models.AnthroveUserID) (*models.User, error) {
return postgres.GetAnthroveUser(ctx, p.db, anthroveUserID)
}
@ -132,34 +124,24 @@ func (p *postgresqlConnection) GetAllAnthroveUserIDs(ctx context.Context) ([]mod
return postgres.GetAllAnthroveUserIDs(ctx, p.db)
}
func (p *postgresqlConnection) GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*graphModels.FavoriteList, error) {
func (p *postgresqlConnection) GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) {
return postgres.GetUserFavoriteNodeWithPagination(ctx, p.db, anthroveUserID, skip, limit)
}
func (p *postgresqlConnection) GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]graphModels.TagsWithFrequency, error) {
func (p *postgresqlConnection) GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) {
return postgres.GetUserTagNodeWitRelationToFavedPosts(ctx, p.db, anthroveUserID)
}
func (p *postgresqlConnection) GetAllTags(ctx context.Context) ([]graphModels.TagsWithFrequency, error) {
tags, err := postgres.GetTags(ctx, p.db)
return utils.ConvertToTagsWithFrequency(tags), err
func (p *postgresqlConnection) GetAllTags(ctx context.Context) ([]models.TagsWithFrequency, error) {
return postgres.GetTags(ctx, p.db)
}
func (p *postgresqlConnection) GetAllSources(ctx context.Context) ([]graphModels.AnthroveSource, error) {
var anthroveSources []graphModels.AnthroveSource
source, err := postgres.GetAllSourceNodes(ctx, p.db)
for _, v := range source {
anthroveSource := utils.PostgresConvertToAnthroveSource(&v)
anthroveSources = append(anthroveSources, *anthroveSource)
}
return nil, err
func (p *postgresqlConnection) GetAllSources(ctx context.Context) ([]models.Source, error) {
return postgres.GetAllSourceNodes(ctx, p.db)
}
func (p *postgresqlConnection) GetSourceByURL(ctx context.Context, sourceUrl string) (*graphModels.AnthroveSource, error) {
source, err := postgres.GetSourceNodesByURL(ctx, p.db, sourceUrl)
return utils.PostgresConvertToAnthroveSource(source), err
func (p *postgresqlConnection) GetSourceByURL(ctx context.Context, sourceUrl string) (*models.Source, error) {
return postgres.GetSourceNodesByURL(ctx, p.db, sourceUrl)
}
func (p *postgresqlConnection) migrateDatabase(connectionString string) error {