Compare commits
No commits in common. "78777ce7be48fb302784e7f93b1107cd42b4b80a" and "b248cbf0c798043c09cd7d7923ac535815d4561d" have entirely different histories.
78777ce7be
...
b248cbf0c7
3
go.mod
3
go.mod
@ -3,9 +3,7 @@ module git.dragse.it/anthrove/otter-space-sdk
|
||||
go 1.22.0
|
||||
|
||||
require (
|
||||
github.com/brianvoe/gofakeit/v7 v7.0.3
|
||||
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/rubenv/sql-migrate v1.6.1
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
@ -21,6 +19,7 @@ require (
|
||||
github.com/jackc/puddle/v2 v2.2.1 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // 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/sync v0.7.0 // indirect
|
||||
golang.org/x/sys v0.18.0 // indirect
|
||||
|
6
go.sum
6
go.sum
@ -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.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
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/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.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
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/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
|
||||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
|
||||
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
@ -36,17 +36,3 @@ func CreateTagNodeWitRelation(ctx context.Context, db *gorm.DB, PostID models.An
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -45,16 +45,3 @@ func PostgresConvertToAnthroveTag(pgTag *pgModels.Tag) *graphModels.AnthroveTag
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -57,9 +57,6 @@ CREATE TABLE "PostReference"
|
||||
post_id TEXT REFERENCES "Post" (id),
|
||||
source_id TEXT REFERENCES "Source" (id),
|
||||
url TEXT NOT NULL UNIQUE,
|
||||
full_file_url TEXT,
|
||||
preview_file_url TEXT,
|
||||
sample_file_url TEXT,
|
||||
source_post_id TEXT,
|
||||
PRIMARY KEY (post_id, source_id)
|
||||
);
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"database/sql"
|
||||
"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"
|
||||
@ -21,13 +22,11 @@ var embedMigrations embed.FS
|
||||
|
||||
type postgresqlConnection struct {
|
||||
db *gorm.DB
|
||||
debug bool
|
||||
}
|
||||
|
||||
func NewPostgresqlConnection(debugOutput bool) OtterSpace {
|
||||
func NewPostgresqlConnection() OtterSpace {
|
||||
return &postgresqlConnection{
|
||||
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 {
|
||||
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 {
|
||||
@ -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 {
|
||||
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 {
|
||||
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) {
|
||||
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) {
|
||||
exists, err := postgres.CheckIfAnthrovePostNodeExistsByAnthroveID(ctx, p.db, anthrovePost.PostID)
|
||||
return anthrovePost, exists, err
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
// CheckPostNodeExistsBySourceURL NOT WORKING! TODO!
|
||||
func (p *postgresqlConnection) CheckPostNodeExistsBySourceURL(ctx context.Context, sourceUrl string) (*graphModels.AnthrovePost, bool, error) {
|
||||
var post graphModels.AnthrovePost
|
||||
exists, err := postgres.CheckIfAnthrovePostNodeExistsBySourceURL(ctx, p.db, sourceUrl)
|
||||
return &post, exists, err
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
// 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
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
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) {
|
||||
tags, err := postgres.GetTags(ctx, p.db)
|
||||
|
||||
return utils.ConvertToTagsWithFrequency(tags), err
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (p *postgresqlConnection) GetAllSources(ctx context.Context) ([]graphModels.AnthroveSource, error) {
|
||||
@ -172,16 +170,13 @@ func (p *postgresqlConnection) migrateDatabase(connectionString string) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("postgres migration: %v", err)
|
||||
}
|
||||
|
||||
if p.debug {
|
||||
if n != 0 {
|
||||
log.Infof("postgres migration: applied %d migrations!", n)
|
||||
log.Tracef("postgres migration: applied %d migrations!", n)
|
||||
|
||||
} else {
|
||||
log.Info("postgres migration: nothing to migrate")
|
||||
log.Trace("postgres migration: nothing to migrate")
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -14,15 +14,12 @@ type BaseModel struct {
|
||||
}
|
||||
|
||||
func (base *BaseModel) BeforeCreate(db *gorm.DB) error {
|
||||
|
||||
if base.ID == "" {
|
||||
id, err := gonanoid.New()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
base.ID = id
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -5,7 +5,4 @@ type PostReference struct {
|
||||
SourceID string `gorm:"primaryKey"`
|
||||
URL string `gorm:"not null;unique"`
|
||||
SourcePostID string
|
||||
FullFileURL string
|
||||
PreviewFileURL string
|
||||
SampleFileURL string
|
||||
}
|
||||
|
Reference in New Issue
Block a user