From 429f68899dd373d8531288a6ae4e091d233b2c86 Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 28 Jun 2024 10:43:55 +0200 Subject: [PATCH] feat(database): new fields Signed-off-by: SoXX --- internal/postgres/user.go | 13 ++++++-- pkg/database/database.go | 6 +++- .../migrations/001_inital_database.sql | 30 +++++++------------ pkg/database/postgres.go | 15 ++++++++++ pkg/models/const.go | 5 ++-- pkg/models/userSource.go | 19 +++++++----- 6 files changed, 54 insertions(+), 34 deletions(-) diff --git a/internal/postgres/user.go b/internal/postgres/user.go index d7b365a..b5c0ee3 100644 --- a/internal/postgres/user.go +++ b/internal/postgres/user.go @@ -6,6 +6,7 @@ import ( otterError "git.dragse.it/anthrove/otter-space-sdk/pkg/error" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" + gonanoid "github.com/matoous/go-nanoid/v2" log "github.com/sirupsen/logrus" "gorm.io/gorm" ) @@ -56,15 +57,21 @@ func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUs return &otterError.EntityValidationFailed{Reason: "accountUsername cannot be empty"} } + validationCode, err := gonanoid.New(25) + + if err != nil { + return err + } + result := db.WithContext(ctx).Exec(`WITH userObj AS ( INSERT INTO "User" (id) VALUES ($1) ON CONFLICT (id) DO NOTHING ) - INSERT INTO "UserSource" (user_id, source_id, account_username, account_id) - SELECT $2, source.id, $3, $4 + INSERT INTO "UserSource" (user_id, source_id, account_username, account_id, account_validate, account_validation_key) + SELECT $2, source.id, $3, $4, false, $5 FROM "Source" AS source - WHERE source.id = $5;`, anthroveUserID, anthroveUserID, accountUsername, accountId, sourceID) + WHERE source.id = $6;`, anthroveUserID, anthroveUserID, accountUsername, accountId, validationCode, sourceID) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { diff --git a/pkg/database/database.go b/pkg/database/database.go index 3f739b4..691bd98 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -2,6 +2,7 @@ package database import ( "context" + "git.dragse.it/anthrove/otter-space-sdk/pkg/models" ) @@ -76,5 +77,8 @@ type OtterSpace interface { CreateTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName, tagID models.AnthroveTagID) DeleteTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName) error - UpdateUserWithScrapeTimeInterval(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error + UpdateUserSourceScrapeTimeInterval(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error + UpdateUserSourceLastScrapeTime(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, lastScrapeTime models.AnthroveUserLastScrapeTime) error + + UpdateUserSourceValidation(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, valid bool) error } diff --git a/pkg/database/migrations/001_inital_database.sql b/pkg/database/migrations/001_inital_database.sql index d580538..03932c9 100644 --- a/pkg/database/migrations/001_inital_database.sql +++ b/pkg/database/migrations/001_inital_database.sql @@ -27,7 +27,7 @@ CREATE TABLE "Post" CREATE TABLE "Source" ( - id CHAR(25) PRIMARY KEY, + id CHAR(25) PRIMARY KEY, display_name TEXT NULL, icon TEXT NULL, domain TEXT NOT NULL UNIQUE, @@ -89,11 +89,14 @@ CREATE TABLE "UserFavorites" CREATE TABLE "UserSource" ( - user_id TEXT REFERENCES "User" (id), - source_id TEXT REFERENCES "Source" (id), - scrape_time_interval TEXT, - account_username TEXT, - account_id TEXT, + user_id TEXT REFERENCES "User" (id), + source_id TEXT REFERENCES "Source" (id), + scrape_time_interval INT, + account_username TEXT, + account_id TEXT, + last_scrape_time TIMESTAMP, + account_validate BOOL DEFAULT FALSE, + account_validation_key CHAR(25), PRIMARY KEY (user_id, source_id), UNIQUE (source_id, account_username, account_id) ); @@ -103,17 +106,4 @@ CREATE TABLE "post_tags" post_id TEXT REFERENCES "Post" (id), tag_name TEXT REFERENCES "Tag" (name), PRIMARY KEY (post_id, tag_name) -); - --- +migrate Down -DROP TYPE Rating; -DROP TYPE TagType; -DROP TABLE Post; -DROP TABLE Source; -DROP TABLE Tag; -DROP TABLE User; -DROP TABLE PostReference; -DROP TABLE TagAlias; -DROP TABLE TagGroup; -DROP TABLE UserFavorites; -DROP TABLE UserSource; \ No newline at end of file +); \ No newline at end of file diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index 1770610..fe078dc 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -193,6 +193,21 @@ func (p *postgresqlConnection) UpdateUserWithScrapeTimeInterval(ctx context.Cont panic("implement me") } +func (p *postgresqlConnection) UpdateUserSourceScrapeTimeInterval(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error { + //TODO implement me + panic("implement me") +} + +func (p *postgresqlConnection) UpdateUserSourceLastScrapeTime(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, lastScrapeTime models.AnthroveUserLastScrapeTime) error { + //TODO implement me + panic("implement me") +} + +func (p *postgresqlConnection) UpdateUserSourceValidation(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, valid bool) error { + //TODO implement me + panic("implement me") +} + func (p *postgresqlConnection) migrateDatabase(dbPool *gorm.DB) error { dialect := "postgres" migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"} diff --git a/pkg/models/const.go b/pkg/models/const.go index 13646fe..0e24228 100644 --- a/pkg/models/const.go +++ b/pkg/models/const.go @@ -1,7 +1,5 @@ package models -import "google.golang.org/genproto/googleapis/type/date" - type AnthroveUserID string type AnthrovePostID string type AnthroveSourceID string @@ -10,7 +8,8 @@ type AnthrovePostURL string type AnthroveTagGroupName string type AnthroveTagAliasName string type AnthroveTagID string -type AnthroveScrapeTimeInterval date.Date +type AnthroveScrapeTimeInterval int +type AnthroveUserLastScrapeTime int type Rating string type TagType string diff --git a/pkg/models/userSource.go b/pkg/models/userSource.go index 59788a1..8ac124e 100644 --- a/pkg/models/userSource.go +++ b/pkg/models/userSource.go @@ -1,13 +1,18 @@ package models +import "time" + type UserSource struct { - User User `gorm:"foreignKey:ID;references:UserID"` - UserID string `gorm:"primaryKey"` - Source Source `gorm:"foreignKey:ID;references:SourceID"` - SourceID string `gorm:"primaryKey"` - ScrapeTimeInterval string - AccountUsername string - AccountID string + User User `gorm:"foreignKey:ID;references:UserID"` + UserID string `gorm:"primaryKey"` + Source Source `gorm:"foreignKey:ID;references:SourceID"` + SourceID string `gorm:"primaryKey"` + ScrapeTimeInterval string + AccountUsername string + AccountID string + LastScrapeTime time.Time + AccountValidate bool + AccountValidationKey string } func (UserSource) TableName() string {