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_PORT=
APP_PORT=
GRACEFULL_TIMEOUT=
REDIS_HOST=
REDIS_USERNAME=
REDIS_PORT=

View File

@ -1,5 +1,39 @@
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{
OnStart: func(ctx context.Context) error {
fmt.Println("Application has started...")
// Run migration
if err := db.Migrate(); err != nil {
log.Fatal("Migration failed: ", err)
}
pkgconfig.Router(apiRouter)
return nil

View File

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

View File

@ -2,13 +2,11 @@ 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"`
ID string `gorm:"primaryKey" json:"id"`
AuthorID string `gorm:"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"`

View File

@ -2,12 +2,10 @@ package database
import (
"time"
"github.com/google/uuid"
)
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"`
Email string `gorm:"unique,not null" json:"email"`
Password string `gorm:"not null" json:"password"`

View File

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

View File

@ -2,12 +2,10 @@ package database
import (
"time"
"github.com/google/uuid"
)
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"`
Name string `gorm:"not null" json:"name"`
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`

View File

@ -2,17 +2,16 @@ package database
import (
"time"
"github.com/google/uuid"
)
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"`
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"`
Subscribe Subscribe `gorm:"foreignKey:SubscribeID;constraint:OnDelete:CASCADE"`
}

View File

@ -7,7 +7,7 @@ import (
func (s *SubsAccs) Create(subsPlanId string) (string, error) {
spec := &subscribedomain.Subscribe{
ID: uuid.New(),
ID: uuid.NewString(),
SubscribePlanID: subsPlanId,
}
@ -15,5 +15,5 @@ func (s *SubsAccs) Create(subsPlanId string) (string, error) {
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 {
data := &subscribeplandomain.SubscribePlan{
ID: uuid.New(),
ID: uuid.NewString(),
Code: spec.Code,
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 {
Create(subscribeplandomain.SubscribePlanReq) error
GetAll() ([]subscribeplandomain.SubscribePlan, error)
GetDefault() (subscribeplandomain.SubscribePlan, error)
}
func New(

View File

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

View File

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

View File

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

View File

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

View File

@ -3,6 +3,7 @@ package authsvc
import (
staffrepository "github.com/ardeman/project-legalgo-go/internal/accessor/staff"
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"
authdomain "github.com/ardeman/project-legalgo-go/internal/domain/auth"
)
@ -11,6 +12,7 @@ type AuthSvc struct {
staffRepo staffrepository.StaffIntf
userRepo userrepository.UserIntf
subsRepo subscriberepository.SubsIntf
subsPlanRepo subscribeplanrepository.SubsPlanIntf
}
type AuthIntf interface {
@ -24,10 +26,12 @@ func New(
staffRepo staffrepository.StaffIntf,
userRepo userrepository.UserIntf,
subsRepo subscriberepository.SubsIntf,
subsPlanRepo subscribeplanrepository.SubsPlanIntf,
) AuthIntf {
return &AuthSvc{
staffRepo: staffRepo,
userRepo: userRepo,
subsRepo: subsRepo,
subsPlanRepo: subsPlanRepo,
}
}

View File

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

View File

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

View File

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