Compare commits

...

5 Commits

Author SHA1 Message Date
ericprd
81c7a51256 docs: fix path 2025-02-24 20:32:08 +08:00
ericprd
3466a4b4c5 docs: fix path 2025-02-24 20:30:10 +08:00
ericprd
037acbe128 fix: path url and add docs 2025-02-24 20:27:07 +08:00
ericprd
d492f3ce44 feat: get all plans and improvement 2025-02-24 20:18:09 +08:00
ericprd
3b2321d5db fix: handle duplicate input 2025-02-24 19:47:40 +08:00
19 changed files with 250 additions and 21 deletions

View File

@ -22,6 +22,7 @@ type Subscribe struct {
type SubscribePlan struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"`
Code string `gorm:"not null" json:"code"`
Name string `gorm:"not null" json:"name"`
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP"`
UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP"`
}

View File

@ -7,8 +7,8 @@ import (
"gorm.io/gorm"
)
func (sr *StaffRepository) GetStaffByEmail(email string) (*authdomain.LoginRepoResponse, error) {
var staff authdomain.LoginRepoResponse
func (sr *StaffRepository) GetStaffByEmail(email string) (*authdomain.Staff, error) {
var staff authdomain.Staff
if email == "" {
return nil, errors.New("email is empty")

View File

@ -10,7 +10,7 @@ type StaffRepository struct {
}
type StaffIntf interface {
GetStaffByEmail(string) (*authdomain.LoginRepoResponse, error)
GetStaffByEmail(string) (*authdomain.Staff, error)
Create(*authdomain.Staff) (*authdomain.Staff, error)
}

View File

@ -5,13 +5,14 @@ import (
"github.com/google/uuid"
)
func (s *SubsPlan) Create(code string) error {
spec := &subscribeplandomain.SubscribePlan{
func (s *SubsPlan) Create(spec subscribeplandomain.SubscribePlanReq) error {
data := &subscribeplandomain.SubscribePlan{
ID: uuid.New(),
Code: code,
Code: spec.Code,
Name: spec.Name,
}
if err := s.DB.Create(&spec).Error; err != nil {
if err := s.DB.Create(&data).Error; err != nil {
return err
}

View File

@ -0,0 +1,15 @@
package subscribeplanrepository
import (
subscribeplandomain "github.com/ardeman/project-legalgo-go/internal/domain/subscribe_plan"
)
func (s *SubsPlan) GetAll() ([]subscribeplandomain.SubscribePlan, error) {
var subscribePlans []subscribeplandomain.SubscribePlan
if err := s.DB.Find(&subscribePlans).Error; err != nil {
return nil, err
}
return subscribePlans, nil
}

View File

@ -1,13 +1,17 @@
package subscribeplanrepository
import "github.com/ardeman/project-legalgo-go/database"
import (
"github.com/ardeman/project-legalgo-go/database"
subscribeplandomain "github.com/ardeman/project-legalgo-go/internal/domain/subscribe_plan"
)
type SubsPlan struct {
DB *database.DB
}
type SubsPlanIntf interface {
Create(string) error
Create(subscribeplandomain.SubscribePlanReq) error
GetAll() ([]subscribeplandomain.SubscribePlan, error)
}
func New(

View File

@ -3,12 +3,12 @@ package userrepository
import (
"errors"
usermodel "github.com/ardeman/project-legalgo-go/database/user"
authdomain "github.com/ardeman/project-legalgo-go/internal/domain/auth"
"gorm.io/gorm"
)
func (ur *UserRepository) GetUserByEmail(email string) (*usermodel.User, error) {
var user usermodel.User
func (ur *UserRepository) GetUserByEmail(email string) (*authdomain.User, error) {
var user authdomain.User
if email == "" {
return nil, errors.New("email is empty")

View File

@ -2,7 +2,6 @@ package userrepository
import (
"github.com/ardeman/project-legalgo-go/database"
usermodel "github.com/ardeman/project-legalgo-go/database/user"
authdomain "github.com/ardeman/project-legalgo-go/internal/domain/auth"
)
@ -11,7 +10,7 @@ type UserRepository struct {
}
type UserIntf interface {
GetUserByEmail(string) (*usermodel.User, error)
GetUserByEmail(string) (*authdomain.User, error)
CreateUser(*authdomain.User) (*authdomain.User, error)
}

View File

@ -45,7 +45,7 @@ func CreateSubscribePlan(
return
}
if err := subsSvc.CreatePlan(spec.Code); err != nil {
if err := subsSvc.CreatePlan(spec); err != nil {
response.ResponseWithErrorCode(
ctx,
w,

View File

@ -0,0 +1,32 @@
package subscribeplanhttp
import (
"net/http"
subscribeplansvc "github.com/ardeman/project-legalgo-go/internal/services/subscribe_plan"
"github.com/ardeman/project-legalgo-go/internal/utilities/response"
"github.com/go-chi/chi/v5"
)
func GetAllPlan(
router chi.Router,
subsPlanSvc subscribeplansvc.SubsPlanIntf,
) {
router.Get("/subscribe-plan/get-all", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
subsPlan, err := subsPlanSvc.GetAllPlan()
if err != nil {
response.ResponseWithErrorCode(
ctx,
w,
err,
response.ErrBadRequest.Code,
response.ErrBadRequest.HttpCode,
response.ErrBadRequest.Message,
)
return
}
response.RespondJsonSuccess(ctx, w, subsPlan)
})
}

View File

@ -5,5 +5,6 @@ import "go.uber.org/fx"
var Module = fx.Module("subscribe-plan",
fx.Invoke(
CreateSubscribePlan,
GetAllPlan,
),
)

View File

@ -4,9 +4,11 @@ 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"`
}

View File

@ -7,8 +7,8 @@ import (
"github.com/ardeman/project-legalgo-go/internal/utilities/utils"
)
func (sv *AuthSvc) LoginAsUser(spec authdomain.LoginReq) (string, error) {
user, err := sv.userRepo.GetUserByEmail(spec.Email)
func (a *AuthSvc) LoginAsUser(spec authdomain.LoginReq) (string, error) {
user, err := a.userRepo.GetUserByEmail(spec.Email)
if err != nil {
return "", errors.New(err.Error())
}

View File

@ -9,6 +9,10 @@ import (
)
func (a *AuthSvc) RegisterStaff(spec authdomain.RegisterStaffReq) (string, error) {
_, err := a.staffRepo.GetStaffByEmail(spec.Email)
if err == nil {
return "", errors.New("this email address is already in use")
}
hashedPwd, err := HashPassword(spec.Password)
if err != nil {
return "", err

View File

@ -9,6 +9,12 @@ import (
)
func (a *AuthSvc) RegisterUser(spec authdomain.RegisterUserReq) (string, error) {
_, err := a.userRepo.GetUserByEmail(spec.Email)
if err == nil {
return "", errors.New("this email address is already in use")
}
subsId, err := a.subsRepo.Create(spec.SubscribePlanID)
if err != nil {
return "", nil

View File

@ -1,5 +1,7 @@
package subscribeplansvc
func (sb *SubsPlanSvc) CreatePlan(code string) error {
return sb.subsAccs.Create(code)
import subscribeplandomain "github.com/ardeman/project-legalgo-go/internal/domain/subscribe_plan"
func (sb *SubsPlanSvc) CreatePlan(spec subscribeplandomain.SubscribePlanReq) error {
return sb.subsAccs.Create(spec)
}

View File

@ -0,0 +1,14 @@
package subscribeplansvc
import (
subscribeplandomain "github.com/ardeman/project-legalgo-go/internal/domain/subscribe_plan"
)
func (s *SubsPlanSvc) GetAllPlan() ([]subscribeplandomain.SubscribePlan, error) {
subsPlan, err := s.subsAccs.GetAll()
if err != nil {
return nil, err
}
return subsPlan, nil
}

View File

@ -1,13 +1,17 @@
package subscribeplansvc
import subscribeplanrepository "github.com/ardeman/project-legalgo-go/internal/accessor/subscribeplan"
import (
subscribeplanrepository "github.com/ardeman/project-legalgo-go/internal/accessor/subscribeplan"
subscribeplandomain "github.com/ardeman/project-legalgo-go/internal/domain/subscribe_plan"
)
type SubsPlanSvc struct {
subsAccs subscribeplanrepository.SubsPlanIntf
}
type SubsPlanIntf interface {
CreatePlan(string) error
CreatePlan(subscribeplandomain.SubscribePlanReq) error
GetAllPlan() ([]subscribeplandomain.SubscribePlan, error)
}
func New(

144
openapi.yml Normal file
View File

@ -0,0 +1,144 @@
openapi: 3.0.0
info:
title: Staff and User API
version: 1.0.0
description: API for handling staff and user login, registration, and subscription plan creation.
paths:
/staff/login:
post:
summary: Login for staff
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
email:
type: string
format: email
password:
type: string
required:
- email
- password
responses:
"200":
description: Successful login
"400":
description: Bad request
/staff/register:
post:
summary: Register a new staff member
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
email:
type: string
format: email
password:
type: string
required:
- email
- password
responses:
"201":
description: Staff member created
"400":
description: Bad request
"409":
description: Conflict (email already registered)
/user/login:
post:
summary: Login for user
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
email:
type: string
format: email
password:
type: string
required:
- email
- password
responses:
"200":
description: Successful login
"400":
description: Bad request
/user/register:
post:
summary: Register a new user
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
email:
type: string
format: email
password:
type: string
subscribe_plan_id:
type: string
phone:
type: string
format: phone
required:
- email
- password
- subscribe_plan_id
- phone
responses:
"201":
description: User created
"400":
description: Bad request
"409":
description: Conflict (email already registered)
/subscribe-plan/create:
post:
summary: Create a new subscription plan
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
code:
type: string
required:
- code
responses:
"201":
description: Subscription plan created
"400":
description: Bad request
"409":
description: Conflict (plan code already exists)
/subscribe-plan/get-all:
get:
summary: Get all subscription plan
responses:
"201":
description: Subscription plan created
"400":
description: Bad request