From 5a693063bbccbb90ca7699cf147e7daa029de338 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 4 Jun 2024 08:49:46 +0200 Subject: [PATCH] feat(sources): finalized model with gorm annotation Signed-off-by: SoXX --- pkg/models/pgModels/post.go | 11 +++++++---- pkg/models/pgModels/postReference.go | 8 ++++++++ pkg/models/pgModels/source.go | 6 ++++-- pkg/models/pgModels/tag.go | 11 +++++++---- pkg/models/pgModels/user.go | 2 ++ pkg/models/pgModels/userFavorite.go | 9 +++++++++ pkg/models/pgModels/userSource.go | 8 ++++++++ 7 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 pkg/models/pgModels/postReference.go create mode 100644 pkg/models/pgModels/userFavorite.go create mode 100644 pkg/models/pgModels/userSource.go diff --git a/pkg/models/pgModels/post.go b/pkg/models/pgModels/post.go index a4cf61a..1ba9262 100644 --- a/pkg/models/pgModels/post.go +++ b/pkg/models/pgModels/post.go @@ -7,8 +7,11 @@ import ( // Post model type Post struct { BaseModel - Rating models.Rating `gorm:"type:rating"` - Body string `gorm:"type:text"` - UserID int `gorm:"index"` - Status string `gorm:"type:post_status"` + Rating models.Rating `gorm:"type:enum('safe','questionable','explicit')"` + Body string `gorm:"type:text"` + UserID int + Status string + Tags []Tag `gorm:"many2many:post_tags;"` + Favorites []UserFavorite `gorm:"foreignKey:PostID"` + References []PostReference `gorm:"foreignKey:PostID"` } diff --git a/pkg/models/pgModels/postReference.go b/pkg/models/pgModels/postReference.go new file mode 100644 index 0000000..ae4574c --- /dev/null +++ b/pkg/models/pgModels/postReference.go @@ -0,0 +1,8 @@ +package pgModels + +type PostReference struct { + PostID string `gorm:"primaryKey"` + SourceID string `gorm:"primaryKey"` + URL string `gorm:"not null;unique"` + SourcePostID string +} diff --git a/pkg/models/pgModels/source.go b/pkg/models/pgModels/source.go index 73e2535..22c727f 100644 --- a/pkg/models/pgModels/source.go +++ b/pkg/models/pgModels/source.go @@ -3,6 +3,8 @@ package pgModels // Source model type Source struct { BaseModel - DisplayName string `gorm:"type:text"` - Domain string `gorm:"type:text;not null;unique"` + DisplayName string + Domain string `gorm:"not null;unique"` + UserSources []UserSource `gorm:"foreignKey:SourceID"` + References []PostReference `gorm:"foreignKey:SourceID"` } diff --git a/pkg/models/pgModels/tag.go b/pkg/models/pgModels/tag.go index 4c246a1..406f744 100644 --- a/pkg/models/pgModels/tag.go +++ b/pkg/models/pgModels/tag.go @@ -4,18 +4,21 @@ import "git.dragse.it/anthrove/otter-space-sdk/pkg/models" // Tag models type Tag struct { - Name string `gorm:"primaryKey"` - Type models.TagType `gorm:"type:tag_type"` + Name string `gorm:"primaryKey"` + Type models.TagType + Aliases []TagAlias `gorm:"foreignKey:TagID"` + Groups []TagGroup `gorm:"foreignKey:TagID"` + Posts []Post `gorm:"many2many:post_tags;"` } // TagAlias model type TagAlias struct { Name string `gorm:"primaryKey"` - TagID string `gorm:"index"` + TagID string } // TagGroup model type TagGroup struct { Name string `gorm:"primaryKey"` - TagID string `gorm:"index"` + TagID string } diff --git a/pkg/models/pgModels/user.go b/pkg/models/pgModels/user.go index be072d8..7fe7e73 100644 --- a/pkg/models/pgModels/user.go +++ b/pkg/models/pgModels/user.go @@ -3,4 +3,6 @@ package pgModels // User model type User struct { BaseModel + Favorites []UserFavorite `gorm:"foreignKey:UserID"` + Sources []UserSource `gorm:"foreignKey:UserID"` } diff --git a/pkg/models/pgModels/userFavorite.go b/pkg/models/pgModels/userFavorite.go new file mode 100644 index 0000000..2e194a3 --- /dev/null +++ b/pkg/models/pgModels/userFavorite.go @@ -0,0 +1,9 @@ +package pgModels + +import "time" + +type UserFavorite struct { + UserID string `gorm:"primaryKey"` + PostID string `gorm:"primaryKey"` + CreatedAt time.Time +} diff --git a/pkg/models/pgModels/userSource.go b/pkg/models/pgModels/userSource.go new file mode 100644 index 0000000..7319994 --- /dev/null +++ b/pkg/models/pgModels/userSource.go @@ -0,0 +1,8 @@ +package pgModels + +type UserSource struct { + UserID string `gorm:"primaryKey"` + SourceID string `gorm:"primaryKey"` + AccountUsername string + AccountID string +}