Compare commits

..

No commits in common. "78777ce7be48fb302784e7f93b1107cd42b4b80a" and "b248cbf0c798043c09cd7d7923ac535815d4561d" have entirely different histories.

10 changed files with 45 additions and 225 deletions

3
go.mod
View File

@ -3,9 +3,7 @@ module git.dragse.it/anthrove/otter-space-sdk
go 1.22.0 go 1.22.0
require ( require (
github.com/brianvoe/gofakeit/v7 v7.0.3
github.com/lib/pq v1.10.7 github.com/lib/pq v1.10.7
github.com/matoous/go-nanoid/v2 v2.1.0
github.com/neo4j/neo4j-go-driver/v5 v5.17.0 github.com/neo4j/neo4j-go-driver/v5 v5.17.0
github.com/rubenv/sql-migrate v1.6.1 github.com/rubenv/sql-migrate v1.6.1
github.com/sirupsen/logrus v1.9.3 github.com/sirupsen/logrus v1.9.3
@ -21,6 +19,7 @@ require (
github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect github.com/jinzhu/now v1.1.5 // indirect
github.com/matoous/go-nanoid/v2 v2.1.0 // indirect
golang.org/x/crypto v0.21.0 // indirect golang.org/x/crypto v0.21.0 // indirect
golang.org/x/sync v0.7.0 // indirect golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.18.0 // indirect golang.org/x/sys v0.18.0 // indirect

6
go.sum
View File

@ -1,5 +1,3 @@
github.com/brianvoe/gofakeit/v7 v7.0.3 h1:tGCt+eYfhTMWE1ko5G2EO1f/yE44yNpIwUb4h32O0wo=
github.com/brianvoe/gofakeit/v7 v7.0.3/go.mod h1:QXuPeBw164PJCzCUZVmgpgHJ3Llj49jSLVkKPMtxtxA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -44,8 +42,8 @@ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=

View File

@ -1,60 +0,0 @@
package postgres
import (
"context"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
func CreateAnthrovePostNode(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, anthroveRating models.Rating) error {
post := pgModels.Post{
BaseModel: pgModels.BaseModel{
ID: string(anthrovePostID),
},
Rating: anthroveRating,
}
err := db.WithContext(ctx).Create(&post).Error
if err != nil {
return err
}
log.WithFields(log.Fields{
"anthrove_post_id": anthrovePostID,
"anthrove_post_rating": anthroveRating,
}).Trace("database: created anthrove post")
return nil
}
func CheckIfAnthrovePostNodeExistsByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID) (bool, error) {
return executeCheckQuery(ctx, db, "id = ?", string(anthrovePostID))
}
func CheckIfAnthrovePostNodeExistsBySourceURL(ctx context.Context, db *gorm.DB, sourceURL string) (bool, error) {
return executeCheckQuery(ctx, db, "url = ?", sourceURL)
}
func CheckIfAnthrovePostNodeExistsBySourceID(ctx context.Context, db *gorm.DB, sourceID string) (bool, error) {
return executeCheckQuery(ctx, db, "source_id = ?", sourceID)
}
func executeCheckQuery(ctx context.Context, db *gorm.DB, query string, args ...interface{}) (bool, error) {
var count int64
err := db.WithContext(ctx).Model(&pgModels.Post{}).Where(query, args...).Count(&count).Error
if err != nil {
return false, err
}
exists := count > 0
log.WithFields(log.Fields{
"query": query,
"args": args,
"exists": exists,
}).Trace("database: executed check query")
return exists, nil
}

View File

@ -1,76 +0,0 @@
package postgres
import (
"context"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
func EstablishAnthrovePostToSourceLink(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string) error {
var post pgModels.Post
var source pgModels.Source
// Find the post
err := db.WithContext(ctx).Where("id = ?", anthrovePostID).First(&post).Error
if err != nil {
return err
}
// Find the source
err = db.WithContext(ctx).Where("domain = ?", anthroveSourceDomain).First(&source).Error
if err != nil {
return err
}
// Establish the relationship
err = db.WithContext(ctx).Model(&post).Association("Sources").Append(&source)
if err != nil {
return err
}
log.WithFields(log.Fields{
"anthrove_post_id": anthrovePostID,
"anthrove_source_domain": anthroveSourceDomain,
}).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 {
userFavorite := pgModels.UserFavorite{
UserID: string(anthroveUserID),
PostID: string(anthrovePostID),
}
err := db.WithContext(ctx).Create(&userFavorite).Error
if err != nil {
return err
}
log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID,
"anthrove_post_id": anthrovePostID,
}).Trace("database: created user to post link")
return nil
}
func CheckUserToPostLink(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) {
var count int64
err := db.WithContext(ctx).Model(&pgModels.UserFavorite{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count).Error
if err != nil {
return false, err
}
exists := count > 0
log.WithFields(log.Fields{
"relationship_exists": exists,
"relationship_anthrove_user_id": anthroveUserID,
"relationship_anthrove_post_id": anthrovePostID,
}).Trace("database: checked user post relationship")
return exists, nil
}

View File

@ -36,17 +36,3 @@ func CreateTagNodeWitRelation(ctx context.Context, db *gorm.DB, PostID models.An
return nil return nil
} }
func GetTags(ctx context.Context, db *gorm.DB) ([]pgModels.Tag, error) {
var tags []pgModels.Tag
result := db.WithContext(ctx).Find(&tags)
if result.Error != nil {
return nil, result.Error
}
log.WithFields(log.Fields{
"tag_amount": len(tags),
}).Trace("database: got tags")
return tags, nil
}

View File

@ -45,16 +45,3 @@ func PostgresConvertToAnthroveTag(pgTag *pgModels.Tag) *graphModels.AnthroveTag
return graphTag return graphTag
} }
func ConvertToTagsWithFrequency(tags []pgModels.Tag) []graphModels.TagsWithFrequency {
var tagsWithFrequency []graphModels.TagsWithFrequency
for _, tag := range tags {
graphTag := PostgresConvertToAnthroveTag(&tag)
tagsWithFrequency = append(tagsWithFrequency, graphModels.TagsWithFrequency{
Frequency: 0,
Tags: *graphTag,
})
}
return tagsWithFrequency
}

View File

@ -57,9 +57,6 @@ CREATE TABLE "PostReference"
post_id TEXT REFERENCES "Post" (id), post_id TEXT REFERENCES "Post" (id),
source_id TEXT REFERENCES "Source" (id), source_id TEXT REFERENCES "Source" (id),
url TEXT NOT NULL UNIQUE, url TEXT NOT NULL UNIQUE,
full_file_url TEXT,
preview_file_url TEXT,
sample_file_url TEXT,
source_post_id TEXT, source_post_id TEXT,
PRIMARY KEY (post_id, source_id) PRIMARY KEY (post_id, source_id)
); );

View File

@ -5,6 +5,7 @@ import (
"database/sql" "database/sql"
"embed" "embed"
"fmt" "fmt"
"git.dragse.it/anthrove/otter-space-sdk/internal/postgres" "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/internal/utils"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
@ -21,13 +22,11 @@ var embedMigrations embed.FS
type postgresqlConnection struct { type postgresqlConnection struct {
db *gorm.DB db *gorm.DB
debug bool
} }
func NewPostgresqlConnection(debugOutput bool) OtterSpace { func NewPostgresqlConnection() OtterSpace {
return &postgresqlConnection{ return &postgresqlConnection{
db: nil, db: nil,
debug: debugOutput,
} }
} }
@ -64,7 +63,8 @@ func (p *postgresqlConnection) AddSource(ctx context.Context, anthroveSource *gr
} }
func (p *postgresqlConnection) AddPost(ctx context.Context, anthrovePost *graphModels.AnthrovePost) error { func (p *postgresqlConnection) AddPost(ctx context.Context, anthrovePost *graphModels.AnthrovePost) error {
return postgres.CreateAnthrovePostNode(ctx, p.db, anthrovePost.PostID, anthrovePost.Rating) //TODO implement me
panic("implement me")
} }
func (p *postgresqlConnection) AddTagWithRelationToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *graphModels.AnthroveTag) error { func (p *postgresqlConnection) AddTagWithRelationToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *graphModels.AnthroveTag) error {
@ -72,34 +72,33 @@ func (p *postgresqlConnection) AddTagWithRelationToPost(ctx context.Context, ant
} }
func (p *postgresqlConnection) LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *graphModels.AnthrovePostRelationship) error { func (p *postgresqlConnection) LinkPostWithSource(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveSourceDomain string, anthrovePostRelationship *graphModels.AnthrovePostRelationship) error {
return postgres.EstablishAnthrovePostToSourceLink(ctx, p.db, anthrovePostID, anthroveSourceDomain) //TODO implement me
panic("implement me")
} }
func (p *postgresqlConnection) LinkUserWithPost(ctx context.Context, anthroveUser *graphModels.AnthroveUser, anthrovePost *graphModels.AnthrovePost) error { func (p *postgresqlConnection) LinkUserWithPost(ctx context.Context, anthroveUser *graphModels.AnthroveUser, anthrovePost *graphModels.AnthrovePost) error {
return postgres.EstablishUserToPostLink(ctx, p.db, anthroveUser.UserID, anthrovePost.PostID) //TODO implement me
panic("implement me")
} }
func (p *postgresqlConnection) CheckUserPostLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID string, sourceUrl string) (bool, error) { 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)) //TODO implement me
panic("implement me")
} }
func (p *postgresqlConnection) CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *graphModels.AnthrovePost) (*graphModels.AnthrovePost, bool, error) { func (p *postgresqlConnection) CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *graphModels.AnthrovePost) (*graphModels.AnthrovePost, bool, error) {
exists, err := postgres.CheckIfAnthrovePostNodeExistsByAnthroveID(ctx, p.db, anthrovePost.PostID) //TODO implement me
return anthrovePost, exists, err panic("implement me")
} }
// CheckPostNodeExistsBySourceURL NOT WORKING! TODO!
func (p *postgresqlConnection) CheckPostNodeExistsBySourceURL(ctx context.Context, sourceUrl string) (*graphModels.AnthrovePost, bool, error) { func (p *postgresqlConnection) CheckPostNodeExistsBySourceURL(ctx context.Context, sourceUrl string) (*graphModels.AnthrovePost, bool, error) {
var post graphModels.AnthrovePost //TODO implement me
exists, err := postgres.CheckIfAnthrovePostNodeExistsBySourceURL(ctx, p.db, sourceUrl) panic("implement me")
return &post, exists, err
} }
// CheckPostNodeExistsBySourceID NOT WORKING! TODO!
func (p *postgresqlConnection) CheckPostNodeExistsBySourceID(ctx context.Context, sourcePostID string) (*graphModels.AnthrovePost, bool, error) { func (p *postgresqlConnection) CheckPostNodeExistsBySourceID(ctx context.Context, sourcePostID string) (*graphModels.AnthrovePost, bool, error) {
var post graphModels.AnthrovePost //TODO implement me
exists, err := postgres.CheckIfAnthrovePostNodeExistsBySourceID(ctx, p.db, sourcePostID) panic("implement me")
return &post, exists, err
} }
func (p *postgresqlConnection) GetUserFavoriteCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) { func (p *postgresqlConnection) GetUserFavoriteCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) {
@ -138,9 +137,8 @@ func (p *postgresqlConnection) GetUserTagsTroughFavedPosts(ctx context.Context,
} }
func (p *postgresqlConnection) GetAllTags(ctx context.Context) ([]graphModels.TagsWithFrequency, error) { func (p *postgresqlConnection) GetAllTags(ctx context.Context) ([]graphModels.TagsWithFrequency, error) {
tags, err := postgres.GetTags(ctx, p.db) //TODO implement me
panic("implement me")
return utils.ConvertToTagsWithFrequency(tags), err
} }
func (p *postgresqlConnection) GetAllSources(ctx context.Context) ([]graphModels.AnthroveSource, error) { func (p *postgresqlConnection) GetAllSources(ctx context.Context) ([]graphModels.AnthroveSource, error) {
@ -172,16 +170,13 @@ func (p *postgresqlConnection) migrateDatabase(connectionString string) error {
if err != nil { if err != nil {
return fmt.Errorf("postgres migration: %v", err) return fmt.Errorf("postgres migration: %v", err)
} }
if p.debug {
if n != 0 { if n != 0 {
log.Infof("postgres migration: applied %d migrations!", n) log.Tracef("postgres migration: applied %d migrations!", n)
} else { } else {
log.Info("postgres migration: nothing to migrate") log.Trace("postgres migration: nothing to migrate")
} }
}
return nil return nil
} }

View File

@ -14,15 +14,12 @@ type BaseModel struct {
} }
func (base *BaseModel) BeforeCreate(db *gorm.DB) error { func (base *BaseModel) BeforeCreate(db *gorm.DB) error {
if base.ID == "" {
id, err := gonanoid.New() id, err := gonanoid.New()
if err != nil { if err != nil {
return err return err
} }
base.ID = id base.ID = id
}
return nil return nil
} }

View File

@ -5,7 +5,4 @@ type PostReference struct {
SourceID string `gorm:"primaryKey"` SourceID string `gorm:"primaryKey"`
URL string `gorm:"not null;unique"` URL string `gorm:"not null;unique"`
SourcePostID string SourcePostID string
FullFileURL string
PreviewFileURL string
SampleFileURL string
} }