From c120c583e37bf5cf9ab1ff662f03c41a7ac0af77 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 14 Oct 2024 12:23:15 +0200 Subject: [PATCH] feat(database): added pools - added migration - added model --- pkg/database/migrations/003_pools.sql | 28 +++++++++++++++++++++ pkg/models/const.go | 1 + pkg/models/orm.go | 2 +- pkg/models/pools.go | 35 +++++++++++++++++++++++++++ pkg/models/pools_test.go | 27 +++++++++++++++++++++ 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 pkg/database/migrations/003_pools.sql create mode 100644 pkg/models/pools.go create mode 100644 pkg/models/pools_test.go diff --git a/pkg/database/migrations/003_pools.sql b/pkg/database/migrations/003_pools.sql new file mode 100644 index 0000000..2f77be8 --- /dev/null +++ b/pkg/database/migrations/003_pools.sql @@ -0,0 +1,28 @@ +-- +migrate Up +CREATE TYPE pool_category AS ENUM ('series', 'collection'); + +CREATE TABLE "Pool" +( + id CHAR(25) PRIMARY KEY, + name VARCHAR(75), + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + deleted_at TIMESTAMP NULL, + category POOL_CATEGORY NOT NULL +); + +CREATE TABLE "PoolPost" +( + pool_id CHAR(25) REFERENCES "Pool" (id), + post_id CHAR(25) REFERENCES "Post" (id), + order_position INT NOT NULL DEFAULT 0, + PRIMARY KEY (pool_id, post_id) +); + +CREATE TABLE "PoolReference" +( + pool_id CHAR(25) REFERENCES "Pool" (id), + source_id CHAR(25) REFERENCES "Source" (id), + url TEXT NOT NULL, + PRIMARY KEY (pool_id, source_id, url) +); \ No newline at end of file diff --git a/pkg/models/const.go b/pkg/models/const.go index 092a803..9755bfd 100644 --- a/pkg/models/const.go +++ b/pkg/models/const.go @@ -6,6 +6,7 @@ type ( UserID string PostID string PostURL string + PoolID string SourceID string SourceDomain string diff --git a/pkg/models/orm.go b/pkg/models/orm.go index 2cf6cf5..4ce9b06 100644 --- a/pkg/models/orm.go +++ b/pkg/models/orm.go @@ -8,7 +8,7 @@ import ( ) type ID interface { - UserID | SourceID | PostID | UserSourceID | UserFavoriteID + UserID | SourceID | PostID | UserSourceID | UserFavoriteID | PoolID } type BaseModel[T ID] struct { diff --git a/pkg/models/pools.go b/pkg/models/pools.go new file mode 100644 index 0000000..e1a6683 --- /dev/null +++ b/pkg/models/pools.go @@ -0,0 +1,35 @@ +package models + +type Pool struct { + BaseModel[PoolID] + Name string `json:"name" gorm:"type:varchar(25)"` + Category string `json:"category" gorm:"type:pool_category;type:enum('series', 'collection')"` +} + +func (Pool) TableName() string { + return "Pool" +} + +type PoolPost struct { + Pool Pool `json:"pool" gorm:"foreignKey:ID;references:PoolID"` + PoolID PoolID `json:"pool_id" gorm:""` + Post Post `json:"post" gorm:"foreignKey:ID;references:PostID"` + PostID PostID `json:"post_id" gorm:""` + OrderPosition int `json:"order_position" gorm:"not null;default:0"` +} + +func (PoolPost) TableName() string { + return "PoolPost" +} + +type PoolReference struct { + Pool Pool `json:"pool" gorm:"foreignKey:ID;references:PoolID"` + PoolID PoolID `json:"pool_id" gorm:""` + Source Source `json:"source" gorm:"foreignKey:ID;references:SourceID"` + SourceID SourceID `json:"source_id" gorm:""` + URL string `json:"url" gorm:"not null"` +} + +func (PoolReference) TableName() string { + return "PoolReference" +} diff --git a/pkg/models/pools_test.go b/pkg/models/pools_test.go new file mode 100644 index 0000000..530b0d5 --- /dev/null +++ b/pkg/models/pools_test.go @@ -0,0 +1,27 @@ +package models + +import "testing" + +func TestPool_TableName(t *testing.T) { + post := Pool{} + expectedTableName := "Pool" + if tableName := post.TableName(); tableName != expectedTableName { + t.Fatalf("expected %s, but got %s", expectedTableName, tableName) + } +} + +func TestPoolPost_TableName(t *testing.T) { + post := PoolPost{} + expectedTableName := "PoolPost" + if tableName := post.TableName(); tableName != expectedTableName { + t.Fatalf("expected %s, but got %s", expectedTableName, tableName) + } +} + +func TestPoolReference_TableName(t *testing.T) { + post := PoolReference{} + expectedTableName := "PoolReference" + if tableName := post.TableName(); tableName != expectedTableName { + t.Fatalf("expected %s, but got %s", expectedTableName, tableName) + } +}