Compare commits

...

8 Commits

10 changed files with 225 additions and 45 deletions

3
go.mod
View File

@ -3,7 +3,9 @@ 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
@ -19,7 +21,6 @@ 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
View File

@ -1,3 +1,5 @@
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=
@ -42,8 +44,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.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
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=
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=

60
internal/postgres/post.go Normal file
View File

@ -0,0 +1,60 @@
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

@ -0,0 +1,76 @@
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,3 +36,17 @@ 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
}

View File

@ -45,3 +45,16 @@ 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
}

View File

@ -27,9 +27,9 @@ CREATE TABLE "Post"
CREATE TABLE "Source"
(
id CHAR(25) UNIQUE PRIMARY KEY,
display_name TEXT NULL,
icon TEXT NULL,
domain TEXT NOT NULL UNIQUE,
display_name TEXT NULL,
icon TEXT NULL,
domain TEXT NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP NULL
@ -54,10 +54,13 @@ CREATE TABLE "User"
CREATE TABLE "PostReference"
(
post_id TEXT REFERENCES "Post" (id),
source_id TEXT REFERENCES "Source" (id),
url TEXT NOT NULL UNIQUE,
source_post_id TEXT,
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)
);
@ -77,8 +80,8 @@ CREATE TABLE "TagGroup"
CREATE TABLE "UserFavorites"
(
user_id TEXT UNIQUE REFERENCES "User" (id),
post_id TEXT UNIQUE REFERENCES "Post" (id),
user_id TEXT UNIQUE REFERENCES "User" (id),
post_id TEXT UNIQUE REFERENCES "Post" (id),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (user_id, post_id)
);

View File

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

View File

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

View File

@ -1,8 +1,11 @@
package pgModels
type PostReference struct {
PostID string `gorm:"primaryKey"`
SourceID string `gorm:"primaryKey"`
URL string `gorm:"not null;unique"`
SourcePostID string
PostID string `gorm:"primaryKey"`
SourceID string `gorm:"primaryKey"`
URL string `gorm:"not null;unique"`
SourcePostID string
FullFileURL string
PreviewFileURL string
SampleFileURL string
}