fix: refactor layout init

This commit is contained in:
ericprd
2025-02-26 22:28:19 +08:00
parent ba075b67fd
commit 0100134527
16 changed files with 154 additions and 39 deletions
+12 -16
View File
@@ -2,11 +2,8 @@ package database
import (
"fmt"
"os"
staffmodel "github.com/ardeman/project-legalgo-go/database/staff"
subsmodel "github.com/ardeman/project-legalgo-go/database/subscribe"
usermodel "github.com/ardeman/project-legalgo-go/database/user"
"github.com/ardeman/project-legalgo-go/config"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
@@ -16,16 +13,15 @@ type DB struct {
}
func NewDB() (*DB, error) {
var (
host = os.Getenv("DB_HOST")
user = os.Getenv("DB_USER")
password = os.Getenv("DB_PASSWORD")
dbName = os.Getenv("DB_NAME")
dbPort = os.Getenv("DB_PORT")
dsn := fmt.Sprintf(
"host=%s user=%s password=%s dbname=%s port=%v sslmode=disable",
config.DB_HOST,
config.DB_USER,
config.DB_PASSWORD,
config.DB_NAME,
config.DB_PORT,
)
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%v sslmode=disable", host, user, password, dbName, dbPort)
if dsn == "" {
return nil, fmt.Errorf("DATABASE_URL environment is not set")
}
@@ -42,9 +38,9 @@ func NewDB() (*DB, error) {
func (db *DB) Migrate() error {
// Auto Migrate the User model
return db.AutoMigrate(
&staffmodel.Staff{},
&subsmodel.SubscribePlan{},
&subsmodel.Subscribe{},
&usermodel.User{},
&Staff{},
&SubscribePlan{},
&Subscribe{},
&User{},
)
}
+19
View File
@@ -0,0 +1,19 @@
package database
import (
"time"
"github.com/google/uuid"
)
type News struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"`
AuthorID uuid.UUID `gorm:"type:uuid;not null" json:"author_id"`
Title string `gorm:"default:null" json:"title"`
Content string `gorm:"default:null" json:"content"`
IsPremium bool `gorm:"default:false" json:"is_premium"`
Slug string `gorm:"default:null" json:"slug"`
FeaturedImage string `gorm:"default:null" json:"featured_image"`
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`
}
@@ -1,4 +1,4 @@
package staffmodel
package database
import (
"time"
@@ -8,6 +8,7 @@ import (
type Staff struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"`
Username string `gorm:"default:null" json:"username"`
Email string `gorm:"unique,not null" json:"email"`
Password string `gorm:"not null" json:"password"`
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
@@ -1,4 +1,4 @@
package subsmodel
package database
import (
"time"
+15
View File
@@ -0,0 +1,15 @@
package database
import (
"time"
"github.com/google/uuid"
)
type Tags struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey;not null" json:"id"`
Code string `gorm:"not null" json:"code"`
Name string `gorm:"not null" json:"name"`
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`
}
-19
View File
@@ -1,19 +0,0 @@
package usermodel
import (
"time"
subsmodel "github.com/ardeman/project-legalgo-go/database/subscribe"
"github.com/google/uuid"
)
type User struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"`
SubscribeID string `gorm:"not null" json:"subscribe_id"`
Email string `gorm:"unique,not null" json:"email"`
Password string `gorm:"not null" json:"password"`
Phone string `gorm:"default:null" json:"phone"`
Subscribe subsmodel.Subscribe `gorm:"foreignKey:SubscribeID;constraint:OnDelete:CASCADE"`
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`
}
+18
View File
@@ -0,0 +1,18 @@
package database
import (
"time"
"github.com/google/uuid"
)
type User struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"`
SubscribeID string `gorm:"not null" json:"subscribe_id"`
Email string `gorm:"unique,not null" json:"email"`
Password string `gorm:"not null" json:"password"`
Phone string `gorm:"default:null" json:"phone"`
Subscribe Subscribe `gorm:"foreignKey:SubscribeID;constraint:OnDelete:CASCADE"`
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`
}