refactor: project name and set default plan for new user

This commit is contained in:
ericprd 2025-02-27 01:14:16 +08:00
parent 46472d01fe
commit 775a9def75
21 changed files with 123 additions and 66 deletions

View File

@ -4,6 +4,9 @@ DB_PASSWORD=
DB_NAME= DB_NAME=
DB_PORT= DB_PORT=
APP_PORT=
GRACEFULL_TIMEOUT=
REDIS_HOST= REDIS_HOST=
REDIS_USERNAME= REDIS_USERNAME=
REDIS_PORT= REDIS_PORT=

View File

@ -1,5 +1,39 @@
package main package main
func main() { import (
"log"
"github.com/ardeman/project-legalgo-go/config"
"github.com/ardeman/project-legalgo-go/database"
subscribeplandomain "github.com/ardeman/project-legalgo-go/internal/domain/subscribe_plan"
"github.com/google/uuid"
"github.com/joho/godotenv"
)
func main() {
if err := godotenv.Load(); err != nil {
log.Fatal("cannot load environment file")
}
config.InitEnv()
db, err := database.NewDB()
if err != nil {
log.Fatalf("failed to connect to database: %v", err)
}
if err := db.Migrate(); err != nil {
log.Fatal("Migration failed: ", err)
}
var temp subscribeplandomain.SubscribePlan
if err := db.Where("code = ?", "basic").First(&temp).Error; err != nil {
db.Create(&subscribeplandomain.SubscribePlan{
ID: uuid.NewString(),
Code: "basic",
Name: "Basic",
})
}
log.Print("migrate success")
} }

View File

@ -28,11 +28,6 @@ func run(lc fx.Lifecycle, db *database.DB, apiRouter chi.Router) {
lc.Append(fx.Hook{ lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error { OnStart: func(ctx context.Context) error {
fmt.Println("Application has started...") fmt.Println("Application has started...")
// Run migration
if err := db.Migrate(); err != nil {
log.Fatal("Migration failed: ", err)
}
pkgconfig.Router(apiRouter) pkgconfig.Router(apiRouter)
return nil return nil

View File

@ -1,6 +1,8 @@
package config package config
import "github.com/ardeman/project-legalgo-go/internal/utilities/utils" import (
"github.com/ardeman/project-legalgo-go/internal/utilities/utils"
)
var ( var (
APP_PORT int APP_PORT int

View File

@ -2,13 +2,11 @@ package database
import ( import (
"time" "time"
"github.com/google/uuid"
) )
type News struct { type News struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"` ID string `gorm:"primaryKey" json:"id"`
AuthorID uuid.UUID `gorm:"type:uuid;not null" json:"author_id"` AuthorID string `gorm:"not null" json:"author_id"`
Title string `gorm:"default:null" json:"title"` Title string `gorm:"default:null" json:"title"`
Content string `gorm:"default:null" json:"content"` Content string `gorm:"default:null" json:"content"`
IsPremium bool `gorm:"default:false" json:"is_premium"` IsPremium bool `gorm:"default:false" json:"is_premium"`

View File

@ -2,12 +2,10 @@ package database
import ( import (
"time" "time"
"github.com/google/uuid"
) )
type Staff struct { type Staff struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"` ID string `gorm:"primaryKey" json:"id"`
Username string `gorm:"default:null" json:"username"` Username string `gorm:"default:null" json:"username"`
Email string `gorm:"unique,not null" json:"email"` Email string `gorm:"unique,not null" json:"email"`
Password string `gorm:"not null" json:"password"` Password string `gorm:"not null" json:"password"`

View File

@ -2,13 +2,11 @@ package database
import ( import (
"time" "time"
"github.com/google/uuid"
) )
type Subscribe struct { type Subscribe struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"` ID string `gorm:"primaryKey" json:"id"`
SubscribePlanID uuid.UUID `gorm:"type:uuid;not null" json:"subscribe_plan_id"` SubscribePlanID string `gorm:"not null" json:"subscribe_plan_id"`
StartDate time.Time `gorm:"default:CURRENT_TIMESTAMP"` StartDate time.Time `gorm:"default:CURRENT_TIMESTAMP"`
EndDate time.Time `gorm:"default:null"` EndDate time.Time `gorm:"default:null"`
Status string `gorm:"default:'inactive'"` Status string `gorm:"default:'inactive'"`
@ -20,7 +18,7 @@ type Subscribe struct {
} }
type SubscribePlan struct { type SubscribePlan struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"` ID string `gorm:"primaryKey" json:"id"`
Code string `gorm:"not null" json:"code"` Code string `gorm:"not null" json:"code"`
Name string `gorm:"not null" json:"name"` Name string `gorm:"not null" json:"name"`
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP"` CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP"`

View File

@ -2,12 +2,10 @@ package database
import ( import (
"time" "time"
"github.com/google/uuid"
) )
type Tags struct { type Tags struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey;not null" json:"id"` ID string `gorm:"primaryKey;not null" json:"id"`
Code string `gorm:"not null" json:"code"` Code string `gorm:"not null" json:"code"`
Name string `gorm:"not null" json:"name"` Name string `gorm:"not null" json:"name"`
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"` CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`

View File

@ -2,17 +2,16 @@ package database
import ( import (
"time" "time"
"github.com/google/uuid"
) )
type User struct { type User struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"` ID string `gorm:"primaryKey" json:"id"`
SubscribeID string `gorm:"not null" json:"subscribe_id"` SubscribeID string `gorm:"not null" json:"subscribe_id"`
Email string `gorm:"unique,not null" json:"email"` Email string `gorm:"unique,not null" json:"email"`
Password string `gorm:"not null" json:"password"` Password string `gorm:"not null" json:"password"`
Phone string `gorm:"default:null" json:"phone"` 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"` CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"` UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`
Subscribe Subscribe `gorm:"foreignKey:SubscribeID;constraint:OnDelete:CASCADE"`
} }

View File

@ -7,7 +7,7 @@ import (
func (s *SubsAccs) Create(subsPlanId string) (string, error) { func (s *SubsAccs) Create(subsPlanId string) (string, error) {
spec := &subscribedomain.Subscribe{ spec := &subscribedomain.Subscribe{
ID: uuid.New(), ID: uuid.NewString(),
SubscribePlanID: subsPlanId, SubscribePlanID: subsPlanId,
} }
@ -15,5 +15,5 @@ func (s *SubsAccs) Create(subsPlanId string) (string, error) {
return "", err return "", err
} }
return spec.ID.String(), nil return spec.ID, nil
} }

View File

@ -7,7 +7,7 @@ import (
func (s *SubsPlan) Create(spec subscribeplandomain.SubscribePlanReq) error { func (s *SubsPlan) Create(spec subscribeplandomain.SubscribePlanReq) error {
data := &subscribeplandomain.SubscribePlan{ data := &subscribeplandomain.SubscribePlan{
ID: uuid.New(), ID: uuid.NewString(),
Code: spec.Code, Code: spec.Code,
Name: spec.Name, Name: spec.Name,
} }

View File

@ -0,0 +1,20 @@
package subscribeplanrepository
import (
subscribeplandomain "github.com/ardeman/project-legalgo-go/internal/domain/subscribe_plan"
"github.com/google/uuid"
)
func (s *SubsPlan) GetDefault() (subscribeplandomain.SubscribePlan, error) {
var subscribePlan subscribeplandomain.SubscribePlan
if err := s.DB.Where("code = ?", "basic").First(&subscribePlan).Error; err != nil {
s.DB.Create(&subscribeplandomain.SubscribePlan{
ID: uuid.NewString(),
Code: "basic",
Name: "Basic",
})
}
return subscribePlan, nil
}

View File

@ -12,6 +12,7 @@ type SubsPlan struct {
type SubsPlanIntf interface { type SubsPlanIntf interface {
Create(subscribeplandomain.SubscribePlanReq) error Create(subscribeplandomain.SubscribePlanReq) error
GetAll() ([]subscribeplandomain.SubscribePlan, error) GetAll() ([]subscribeplandomain.SubscribePlan, error)
GetDefault() (subscribeplandomain.SubscribePlan, error)
} }
func New( func New(

View File

@ -1,7 +1,5 @@
package authdomain package authdomain
import "github.com/google/uuid"
type LoginReq struct { type LoginReq struct {
Email string `json:"email" validate:"required"` Email string `json:"email" validate:"required"`
Password string `json:"password" validate:"required"` Password string `json:"password" validate:"required"`
@ -12,7 +10,7 @@ type AuthResponse struct {
} }
type LoginRepoResponse struct { type LoginRepoResponse struct {
ID uuid.UUID `json:"id"` ID string `json:"id"`
Email string `json:"email"` Email string `json:"email"`
Password string `json:"password"` Password string `json:"password"`
} }

View File

@ -1,7 +1,5 @@
package authdomain package authdomain
import "github.com/google/uuid"
type RegisterUserReq struct { type RegisterUserReq struct {
Email string `json:"email" validate:"required"` Email string `json:"email" validate:"required"`
Password string `json:"password" validate:"required"` Password string `json:"password" validate:"required"`
@ -10,11 +8,11 @@ type RegisterUserReq struct {
} }
type User struct { type User struct {
ID uuid.UUID `json:"id"` ID string `json:"id"`
Email string `json:"email"` Email string `json:"email"`
Password string `json:"password"` Password string `json:"password"`
Phone string `json:"phone"` Phone string `json:"phone"`
SubscribeID string `json:"subscribe_id"` SubscribeID string `json:"subscribe_id"`
} }
type RegisterStaffReq struct { type RegisterStaffReq struct {
@ -23,7 +21,7 @@ type RegisterStaffReq struct {
} }
type Staff struct { type Staff struct {
ID uuid.UUID `json:"id"` ID string `json:"id"`
Email string `json:"email" validate:"required"` Email string `json:"email" validate:"required"`
Password string `json:"password" validate:"required"` Password string `json:"password" validate:"required"`
} }

View File

@ -1,8 +1,6 @@
package subscribedomain package subscribedomain
import "github.com/google/uuid"
type Subscribe struct { type Subscribe struct {
ID uuid.UUID `json:"id"` ID string `json:"id"`
SubscribePlanID string `json:"subscribe_plan_id"` SubscribePlanID string `json:"subscribe_plan_id"`
} }

View File

@ -1,14 +1,12 @@
package subscribeplandomain package subscribeplandomain
import "github.com/google/uuid"
type SubscribePlanReq struct { type SubscribePlanReq struct {
Code string `json:"code" validate:"required"` Code string `json:"code" validate:"required"`
Name string `json:"name" validate:"required"` Name string `json:"name" validate:"required"`
} }
type SubscribePlan struct { type SubscribePlan struct {
ID uuid.UUID `json:"id"` ID string `json:"id"`
Code string `json:"code"` Code string `json:"code"`
Name string `json:"name"` Name string `json:"name"`
} }

View File

@ -3,14 +3,16 @@ package authsvc
import ( import (
staffrepository "github.com/ardeman/project-legalgo-go/internal/accessor/staff" staffrepository "github.com/ardeman/project-legalgo-go/internal/accessor/staff"
subscriberepository "github.com/ardeman/project-legalgo-go/internal/accessor/subscribe" subscriberepository "github.com/ardeman/project-legalgo-go/internal/accessor/subscribe"
subscribeplanrepository "github.com/ardeman/project-legalgo-go/internal/accessor/subscribeplan"
userrepository "github.com/ardeman/project-legalgo-go/internal/accessor/user_repository" userrepository "github.com/ardeman/project-legalgo-go/internal/accessor/user_repository"
authdomain "github.com/ardeman/project-legalgo-go/internal/domain/auth" authdomain "github.com/ardeman/project-legalgo-go/internal/domain/auth"
) )
type AuthSvc struct { type AuthSvc struct {
staffRepo staffrepository.StaffIntf staffRepo staffrepository.StaffIntf
userRepo userrepository.UserIntf userRepo userrepository.UserIntf
subsRepo subscriberepository.SubsIntf subsRepo subscriberepository.SubsIntf
subsPlanRepo subscribeplanrepository.SubsPlanIntf
} }
type AuthIntf interface { type AuthIntf interface {
@ -24,10 +26,12 @@ func New(
staffRepo staffrepository.StaffIntf, staffRepo staffrepository.StaffIntf,
userRepo userrepository.UserIntf, userRepo userrepository.UserIntf,
subsRepo subscriberepository.SubsIntf, subsRepo subscriberepository.SubsIntf,
subsPlanRepo subscribeplanrepository.SubsPlanIntf,
) AuthIntf { ) AuthIntf {
return &AuthSvc{ return &AuthSvc{
staffRepo: staffRepo, staffRepo: staffRepo,
userRepo: userRepo, userRepo: userRepo,
subsRepo: subsRepo, subsRepo: subsRepo,
subsPlanRepo: subsPlanRepo,
} }
} }

View File

@ -19,7 +19,7 @@ func (a *AuthSvc) RegisterStaff(spec authdomain.RegisterStaffReq) (string, error
} }
user := authdomain.Staff{ user := authdomain.Staff{
ID: uuid.New(), ID: uuid.NewString(),
Email: spec.Email, Email: spec.Email,
Password: hashedPwd, Password: hashedPwd,
} }

View File

@ -15,13 +15,18 @@ func (a *AuthSvc) RegisterUser(spec authdomain.RegisterUserReq) (string, error)
return "", errors.New("this email address is already in use") return "", errors.New("this email address is already in use")
} }
var subsId string if spec.SubscribePlanID == "" {
subsPlan, err := a.subsPlanRepo.GetDefault()
if spec.SubscribePlanID != "" {
subsId, err = a.subsRepo.Create(spec.SubscribePlanID)
if err != nil { if err != nil {
return "", nil return "", err
} }
spec.SubscribePlanID = subsPlan.ID
}
subsId, err := a.subsRepo.Create(spec.SubscribePlanID)
if err != nil {
return "", nil
} }
hashedPwd, err := HashPassword(spec.Password) hashedPwd, err := HashPassword(spec.Password)
@ -30,7 +35,7 @@ func (a *AuthSvc) RegisterUser(spec authdomain.RegisterUserReq) (string, error)
} }
user := authdomain.User{ user := authdomain.User{
ID: uuid.New(), ID: uuid.NewString(),
Email: spec.Email, Email: spec.Email,
SubscribeID: subsId, SubscribeID: subsId,
Password: hashedPwd, Password: hashedPwd,

View File

@ -1,5 +1,7 @@
BINARY_NAME=./cmd/legalgo/main.go BINARY_NAME=./cmd/legalgo/main.go
BINARY_MIGRATE_NAME=./cmd/gorm/main.go
OUTPUT_DIR=./bin/legalgo OUTPUT_DIR=./bin/legalgo
OUTPUT_MIGRATE_DIR=./bin/migrate
all: build all: build
@ -11,6 +13,14 @@ run: build
@echo "Building and running..." @echo "Building and running..."
$(OUTPUT_DIR) $(OUTPUT_DIR)
build-migrate:
@echo "Building the Migrate..."
go build -o $(OUTPUT_MIGRATE_DIR) $(BINARY_MIGRATE_NAME)
migrate: build-migrate
@echo "Building and running Migrate..."
$(OUTPUT_MIGRATE_DIR)
clean: clean:
@echo "Cleaning the build..." @echo "Cleaning the build..."
rm -f $(OUTPUT_DIR) rm -f $(OUTPUT_DIR)