From 0e6a8fd61ef773da3977978d0c2aa5f72cd1bc34 Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 9 Aug 2024 21:37:54 +0200 Subject: [PATCH] feat(database): added additional columns added more columns BREAKING-CHANGE: breaks database compatibility to older SDK versions --- pkg/database/source.go | 27 ++++++++++++++++----------- pkg/models/const.go | 31 ++++++++++++++++++------------- pkg/models/orm.go | 2 +- pkg/models/orm_test.go | 4 ++-- pkg/models/post.go | 2 +- pkg/models/post_test.go | 2 +- pkg/models/source.go | 2 +- pkg/models/source_test.go | 2 +- pkg/models/user.go | 2 +- pkg/models/userFavorite.go | 9 ++++++--- pkg/models/userSource.go | 5 +++-- pkg/models/user_test.go | 2 +- 12 files changed, 52 insertions(+), 38 deletions(-) diff --git a/pkg/database/source.go b/pkg/database/source.go index 544bfcc..3967a3f 100644 --- a/pkg/database/source.go +++ b/pkg/database/source.go @@ -2,18 +2,23 @@ package database import ( "context" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" ) -type Source interface { - - // CreateSource adds a new source to the database. - CreateSource(ctx context.Context, anthroveSource *models.Source) error - - // GetAllSources retrieves all sources. - GetAllSources(ctx context.Context) ([]models.Source, error) - - // GetSourceByDomain retrieves a source by its URL. - GetSourceByDomain(ctx context.Context, sourceDomain models.AnthroveSourceDomain) (*models.Source, error) +func CreateUserSource(ctx context.Context, userSource models.UserSource) (models.UserSource, error) { + var user models.UserSource + return user, nil +} + +func UpdateUserSource(ctx context.Context, userSource models.UserSource) error { + return nil +} + +func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.UserSource, error) { + var user models.UserSource + return user, nil +} + +func DeleteUserSource(ctx context.Context, id models.UserSourceID) error { + return nil } diff --git a/pkg/models/const.go b/pkg/models/const.go index 7e84b0c..4f690a4 100644 --- a/pkg/models/const.go +++ b/pkg/models/const.go @@ -2,20 +2,25 @@ package models import "time" -type AnthroveUserID string -type AnthrovePostID string -type AnthroveSourceID string -type AnthroveSourceDomain string -type AnthrovePostURL string -type AnthroveTagGroupName string -type AnthroveTagAliasName string -type AnthroveTagID string -type AnthroveScrapeTimeInterval int -type AnthroveUserLastScrapeTime time.Time -type AnthroveTagName string +type ( + UserID string + PostID string + SourceID string + SourceDomain string + PostURL string + TagGroupName string + TagAliasName string + TagID string + ScrapeTimeInterval int + UserLastScrapeTime time.Time + TagName string -type Rating string -type TagType string + Rating string + TagType string + + UserSourceID string + UserFavoriteID string +) const ( SFW Rating = "safe" diff --git a/pkg/models/orm.go b/pkg/models/orm.go index f9209d9..a4ec52f 100644 --- a/pkg/models/orm.go +++ b/pkg/models/orm.go @@ -8,7 +8,7 @@ import ( ) type ID interface { - AnthroveUserID | AnthroveSourceID | AnthrovePostID + UserID | SourceID | PostID | UserSourceID | UserFavoriteID } type BaseModel[T ID] struct { diff --git a/pkg/models/orm_test.go b/pkg/models/orm_test.go index cf9e1a4..780b67d 100644 --- a/pkg/models/orm_test.go +++ b/pkg/models/orm_test.go @@ -42,8 +42,8 @@ func TestBaseModel_BeforeCreate(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - base := &BaseModel[AnthrovePostID]{ - ID: AnthrovePostID(tt.fields.ID), + base := &BaseModel[PostID]{ + ID: PostID(tt.fields.ID), CreatedAt: tt.fields.CreatedAt, UpdatedAt: tt.fields.UpdatedAt, DeletedAt: tt.fields.DeletedAt, diff --git a/pkg/models/post.go b/pkg/models/post.go index d272387..8b24e60 100644 --- a/pkg/models/post.go +++ b/pkg/models/post.go @@ -2,7 +2,7 @@ package models // Post model type Post struct { - BaseModel[AnthrovePostID] + BaseModel[PostID] Rating Rating `json:"rating" gorm:"type:enum('safe','questionable','explicit')"` Tags []Tag `json:"-" gorm:"many2many:post_tags;"` Favorites []UserFavorites `json:"-" gorm:"foreignKey:PostID"` diff --git a/pkg/models/post_test.go b/pkg/models/post_test.go index 2e7228d..7c6856f 100644 --- a/pkg/models/post_test.go +++ b/pkg/models/post_test.go @@ -4,7 +4,7 @@ import "testing" func TestPost_TableName(t *testing.T) { type fields struct { - BaseModel BaseModel[AnthrovePostID] + BaseModel BaseModel[PostID] Rating Rating Tags []Tag Favorites []UserFavorites diff --git a/pkg/models/source.go b/pkg/models/source.go index 9027b55..aa90bbb 100644 --- a/pkg/models/source.go +++ b/pkg/models/source.go @@ -2,7 +2,7 @@ package models // Source model type Source struct { - BaseModel[AnthroveSourceID] + BaseModel[SourceID] DisplayName string `json:"display_name" ` Domain string `json:"domain" gorm:"not null;unique"` Icon string `json:"icon" gorm:"not null"` diff --git a/pkg/models/source_test.go b/pkg/models/source_test.go index e65b2c8..876db3a 100644 --- a/pkg/models/source_test.go +++ b/pkg/models/source_test.go @@ -4,7 +4,7 @@ import "testing" func TestSource_TableName(t *testing.T) { type fields struct { - BaseModel BaseModel[AnthroveSourceID] + BaseModel BaseModel[SourceID] DisplayName string Domain string Icon string diff --git a/pkg/models/user.go b/pkg/models/user.go index bc2d575..e2a88c1 100644 --- a/pkg/models/user.go +++ b/pkg/models/user.go @@ -2,7 +2,7 @@ package models // User model type User struct { - BaseModel[AnthroveUserID] + BaseModel[UserID] Favorites []UserFavorites `json:"-" gorm:"foreignKey:UserID"` Sources []UserSource `json:"-" gorm:"foreignKey:UserID"` } diff --git a/pkg/models/userFavorite.go b/pkg/models/userFavorite.go index 182af8f..2572ce8 100644 --- a/pkg/models/userFavorite.go +++ b/pkg/models/userFavorite.go @@ -3,9 +3,12 @@ package models import "time" type UserFavorites struct { - UserID string `json:"user_id" gorm:"primaryKey"` - PostID string `json:"post_id" gorm:"primaryKey"` - CreatedAt time.Time `json:"-"` + BaseModel[UserFavoriteID] + UserID string `json:"user_id"` + PostID string `json:"post_id"` + UserSourceID UserSourceID `json:"user_source_id"` + UserSource UserSource `json:"-" gorm:"foreignKey:ID;references:UserSourceID"` + CreatedAt time.Time `json:"created_at"` } func (UserFavorites) TableName() string { diff --git a/pkg/models/userSource.go b/pkg/models/userSource.go index 9ee0f87..187c2a9 100644 --- a/pkg/models/userSource.go +++ b/pkg/models/userSource.go @@ -3,10 +3,11 @@ package models import "time" type UserSource struct { + BaseModel[UserSourceID] User User `json:"user" gorm:"foreignKey:ID;references:UserID"` - UserID string `json:"user_id" gorm:"primaryKey"` + UserID UserID `json:"user_id"` Source Source `json:"source" gorm:"foreignKey:ID;references:SourceID"` - SourceID string `json:"source_id" gorm:"primaryKey"` + SourceID SourceID `json:"source_id"` ScrapeTimeInterval string `json:"scrape_time_interval"` AccountUsername string `json:"account_username"` AccountID string `json:"account_id"` diff --git a/pkg/models/user_test.go b/pkg/models/user_test.go index c31745b..34db57f 100644 --- a/pkg/models/user_test.go +++ b/pkg/models/user_test.go @@ -4,7 +4,7 @@ import "testing" func TestUser_TableName(t *testing.T) { type fields struct { - BaseModel BaseModel[AnthroveUserID] + BaseModel BaseModel[UserID] Favorites []UserFavorites Sources []UserSource }