diff --git a/.env.example b/.env.example index 0c40507..d4f19a8 100644 --- a/.env.example +++ b/.env.example @@ -4,6 +4,9 @@ DB_PASSWORD= DB_NAME= DB_PORT= +APP_PORT= +GRACEFULL_TIMEOUT= + REDIS_HOST= REDIS_USERNAME= REDIS_PORT= diff --git a/cmd/gorm/main.go b/cmd/gorm/main.go index 7905807..d1387de 100644 --- a/cmd/gorm/main.go +++ b/cmd/gorm/main.go @@ -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") } diff --git a/cmd/legalgo/main.go b/cmd/legalgo/main.go index b8e560c..41e3d87 100644 --- a/cmd/legalgo/main.go +++ b/cmd/legalgo/main.go @@ -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 diff --git a/config/conf.go b/config/conf.go index 50bd012..c62bec2 100644 --- a/config/conf.go +++ b/config/conf.go @@ -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 diff --git a/database/news_model.go b/database/news_model.go index 378f3d1..77d6fab 100644 --- a/database/news_model.go +++ b/database/news_model.go @@ -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"` diff --git a/database/staff_model.go b/database/staff_model.go index 566aaa3..0833912 100644 --- a/database/staff_model.go +++ b/database/staff_model.go @@ -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"` diff --git a/database/subscribe_model.go b/database/subscribe_model.go index 745b0a7..b7cd964 100644 --- a/database/subscribe_model.go +++ b/database/subscribe_model.go @@ -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"` diff --git a/database/tags_module.go b/database/tags_module.go index f00c3ba..cc1d07b 100644 --- a/database/tags_module.go +++ b/database/tags_module.go @@ -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"` diff --git a/database/user_model.go b/database/user_model.go index fe21c5f..88e93a9 100644 --- a/database/user_model.go +++ b/database/user_model.go @@ -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"` } diff --git a/internal/accessor/subscribe/create.go b/internal/accessor/subscribe/create.go index 26db13a..a413893 100644 --- a/internal/accessor/subscribe/create.go +++ b/internal/accessor/subscribe/create.go @@ -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 } diff --git a/internal/accessor/subscribeplan/create.go b/internal/accessor/subscribeplan/create.go index 6196c2c..0266133 100644 --- a/internal/accessor/subscribeplan/create.go +++ b/internal/accessor/subscribeplan/create.go @@ -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, } diff --git a/internal/accessor/subscribeplan/get_default.go b/internal/accessor/subscribeplan/get_default.go new file mode 100644 index 0000000..74349cf --- /dev/null +++ b/internal/accessor/subscribeplan/get_default.go @@ -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 +} diff --git a/internal/accessor/subscribeplan/impl.go b/internal/accessor/subscribeplan/impl.go index 8477281..f91feeb 100644 --- a/internal/accessor/subscribeplan/impl.go +++ b/internal/accessor/subscribeplan/impl.go @@ -12,6 +12,7 @@ type SubsPlan struct { type SubsPlanIntf interface { Create(subscribeplandomain.SubscribePlanReq) error GetAll() ([]subscribeplandomain.SubscribePlan, error) + GetDefault() (subscribeplandomain.SubscribePlan, error) } func New( diff --git a/internal/domain/auth/login.go b/internal/domain/auth/login.go index 8f515b8..c57784b 100644 --- a/internal/domain/auth/login.go +++ b/internal/domain/auth/login.go @@ -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"` - Email string `json:"email"` - Password string `json:"password"` + ID string `json:"id"` + Email string `json:"email"` + Password string `json:"password"` } diff --git a/internal/domain/auth/register.go b/internal/domain/auth/register.go index d6c46db..fb04006 100644 --- a/internal/domain/auth/register.go +++ b/internal/domain/auth/register.go @@ -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,11 +8,11 @@ type RegisterUserReq struct { } type User struct { - ID uuid.UUID `json:"id"` - Email string `json:"email"` - Password string `json:"password"` - Phone string `json:"phone"` - SubscribeID string `json:"subscribe_id"` + ID string `json:"id"` + Email string `json:"email"` + Password string `json:"password"` + Phone string `json:"phone"` + SubscribeID string `json:"subscribe_id"` } type RegisterStaffReq struct { @@ -23,7 +21,7 @@ type RegisterStaffReq struct { } type Staff struct { - ID uuid.UUID `json:"id"` - Email string `json:"email" validate:"required"` - Password string `json:"password" validate:"required"` + ID string `json:"id"` + Email string `json:"email" validate:"required"` + Password string `json:"password" validate:"required"` } diff --git a/internal/domain/subscribe/spec.go b/internal/domain/subscribe/spec.go index 152e3da..3fc2cfe 100644 --- a/internal/domain/subscribe/spec.go +++ b/internal/domain/subscribe/spec.go @@ -1,8 +1,6 @@ package subscribedomain -import "github.com/google/uuid" - type Subscribe struct { - ID uuid.UUID `json:"id"` - SubscribePlanID string `json:"subscribe_plan_id"` + ID string `json:"id"` + SubscribePlanID string `json:"subscribe_plan_id"` } diff --git a/internal/domain/subscribe_plan/spec.go b/internal/domain/subscribe_plan/spec.go index 1a5175d..eabd396 100644 --- a/internal/domain/subscribe_plan/spec.go +++ b/internal/domain/subscribe_plan/spec.go @@ -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"` - Code string `json:"code"` - Name string `json:"name"` + ID string `json:"id"` + Code string `json:"code"` + Name string `json:"name"` } diff --git a/internal/services/auth/impl.go b/internal/services/auth/impl.go index 8ea2eed..419061a 100644 --- a/internal/services/auth/impl.go +++ b/internal/services/auth/impl.go @@ -3,14 +3,16 @@ 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" ) type AuthSvc struct { - staffRepo staffrepository.StaffIntf - userRepo userrepository.UserIntf - subsRepo subscriberepository.SubsIntf + 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, + staffRepo: staffRepo, + userRepo: userRepo, + subsRepo: subsRepo, + subsPlanRepo: subsPlanRepo, } } diff --git a/internal/services/auth/register_staff.go b/internal/services/auth/register_staff.go index 40efe76..bc40417 100644 --- a/internal/services/auth/register_staff.go +++ b/internal/services/auth/register_staff.go @@ -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, } diff --git a/internal/services/auth/register_user.go b/internal/services/auth/register_user.go index 7432179..a48f793 100644 --- a/internal/services/auth/register_user.go +++ b/internal/services/auth/register_user.go @@ -15,13 +15,18 @@ 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 != "" { - subsId, err = a.subsRepo.Create(spec.SubscribePlanID) + if spec.SubscribePlanID == "" { + subsPlan, err := a.subsPlanRepo.GetDefault() 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) @@ -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, diff --git a/makefile b/makefile index cad3fa8..ccd9427 100644 --- a/makefile +++ b/makefile @@ -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)