feat: category routes
This commit is contained in:
parent
acd80480dd
commit
bdf6dd131e
21
internal/accessor/category/create.go
Normal file
21
internal/accessor/category/create.go
Normal file
@ -0,0 +1,21 @@
|
||||
package categoryrepository
|
||||
|
||||
import (
|
||||
categorydomain "legalgo-BE-go/internal/domain/category"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (a *accessor) Create(spec categorydomain.CategoryReq) error {
|
||||
data := categorydomain.Category{
|
||||
ID: uuid.NewString(),
|
||||
Name: spec.Name,
|
||||
Code: spec.Code,
|
||||
}
|
||||
|
||||
if err := a.DB.Create(&data).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
13
internal/accessor/category/get_all.go
Normal file
13
internal/accessor/category/get_all.go
Normal file
@ -0,0 +1,13 @@
|
||||
package categoryrepository
|
||||
|
||||
import categorydomain "legalgo-BE-go/internal/domain/category"
|
||||
|
||||
func (a *accessor) GetAll() ([]categorydomain.Category, error) {
|
||||
var categories []categorydomain.Category
|
||||
|
||||
if err := a.DB.Find(&categories).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return categories, nil
|
||||
}
|
||||
21
internal/accessor/category/impl.go
Normal file
21
internal/accessor/category/impl.go
Normal file
@ -0,0 +1,21 @@
|
||||
package categoryrepository
|
||||
|
||||
import (
|
||||
"legalgo-BE-go/database"
|
||||
categorydomain "legalgo-BE-go/internal/domain/category"
|
||||
)
|
||||
|
||||
type accessor struct {
|
||||
DB *database.DB
|
||||
}
|
||||
|
||||
type Category interface {
|
||||
Create(categorydomain.CategoryReq) error
|
||||
GetAll() ([]categorydomain.Category, error)
|
||||
}
|
||||
|
||||
func New(
|
||||
db *database.DB,
|
||||
) Category {
|
||||
return &accessor{db}
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
categoryrepository "legalgo-BE-go/internal/accessor/category"
|
||||
redisaccessor "legalgo-BE-go/internal/accessor/redis"
|
||||
staffrepository "legalgo-BE-go/internal/accessor/staff"
|
||||
subscriberepository "legalgo-BE-go/internal/accessor/subscribe"
|
||||
@ -18,4 +19,5 @@ var Module = fx.Module("repository", fx.Provide(
|
||||
subscribeplanrepository.New,
|
||||
subscriberepository.New,
|
||||
tagrepository.New,
|
||||
categoryrepository.New,
|
||||
))
|
||||
|
||||
67
internal/api/http/category/create.go
Normal file
67
internal/api/http/category/create.go
Normal file
@ -0,0 +1,67 @@
|
||||
package categoryhttp
|
||||
|
||||
import (
|
||||
categorydomain "legalgo-BE-go/internal/domain/category"
|
||||
categorysvc "legalgo-BE-go/internal/services/category"
|
||||
"legalgo-BE-go/internal/utilities/response"
|
||||
"legalgo-BE-go/internal/utilities/utils"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
func Create(
|
||||
router chi.Router,
|
||||
validate *validator.Validate,
|
||||
categorySvc categorysvc.Category,
|
||||
) {
|
||||
router.Post("/category/create", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var spec categorydomain.CategoryReq
|
||||
|
||||
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if err := validate.Struct(spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.(validator.ValidationErrors).Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if err := categorySvc.Create(spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrCreateEntity.Code,
|
||||
response.ErrCreateEntity.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
response.RespondJsonSuccess(ctx, w, struct {
|
||||
Message string
|
||||
}{
|
||||
Message: "category created successfully",
|
||||
})
|
||||
})
|
||||
}
|
||||
32
internal/api/http/category/get_all.go
Normal file
32
internal/api/http/category/get_all.go
Normal file
@ -0,0 +1,32 @@
|
||||
package categoryhttp
|
||||
|
||||
import (
|
||||
categorysvc "legalgo-BE-go/internal/services/category"
|
||||
"legalgo-BE-go/internal/utilities/response"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func GetAll(
|
||||
router chi.Router,
|
||||
categorySvc categorysvc.Category,
|
||||
) {
|
||||
router.Get("/category", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
subsPlan, err := categorySvc.GetAll()
|
||||
if err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
response.RespondJsonSuccess(ctx, w, subsPlan)
|
||||
})
|
||||
}
|
||||
8
internal/api/http/category/module.go
Normal file
8
internal/api/http/category/module.go
Normal file
@ -0,0 +1,8 @@
|
||||
package categoryhttp
|
||||
|
||||
import "go.uber.org/fx"
|
||||
|
||||
var Module = fx.Module("categories", fx.Invoke(
|
||||
Create,
|
||||
GetAll,
|
||||
))
|
||||
@ -2,6 +2,7 @@ package internalhttp
|
||||
|
||||
import (
|
||||
authhttp "legalgo-BE-go/internal/api/http/auth"
|
||||
categoryhttp "legalgo-BE-go/internal/api/http/category"
|
||||
subscribeplanhttp "legalgo-BE-go/internal/api/http/subscribe_plan"
|
||||
taghttp "legalgo-BE-go/internal/api/http/tag"
|
||||
|
||||
@ -21,6 +22,7 @@ var Module = fx.Module("router",
|
||||
authhttp.Module,
|
||||
subscribeplanhttp.Module,
|
||||
taghttp.Module,
|
||||
categoryhttp.Module,
|
||||
)
|
||||
|
||||
func initRouter() chi.Router {
|
||||
|
||||
@ -8,7 +8,7 @@ import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func Get(
|
||||
func GetAll(
|
||||
router chi.Router,
|
||||
tagSvc tagsvc.TagIntf,
|
||||
) {
|
||||
@ -4,5 +4,5 @@ import "go.uber.org/fx"
|
||||
|
||||
var Module = fx.Module("tag", fx.Invoke(
|
||||
Create,
|
||||
Get,
|
||||
GetAll,
|
||||
))
|
||||
|
||||
12
internal/domain/category/spec.go
Normal file
12
internal/domain/category/spec.go
Normal file
@ -0,0 +1,12 @@
|
||||
package categorydomain
|
||||
|
||||
type Category struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Code string `json:"code"`
|
||||
}
|
||||
|
||||
type CategoryReq struct {
|
||||
Name string `json:"name" validate:"required"`
|
||||
Code string `json:"code" validate:"required"`
|
||||
}
|
||||
7
internal/services/category/create.go
Normal file
7
internal/services/category/create.go
Normal file
@ -0,0 +1,7 @@
|
||||
package categorysvc
|
||||
|
||||
import categorydomain "legalgo-BE-go/internal/domain/category"
|
||||
|
||||
func (i *impl) Create(spec categorydomain.CategoryReq) error {
|
||||
return i.categoryRepo.Create(spec)
|
||||
}
|
||||
7
internal/services/category/get_all.go
Normal file
7
internal/services/category/get_all.go
Normal file
@ -0,0 +1,7 @@
|
||||
package categorysvc
|
||||
|
||||
import categorydomain "legalgo-BE-go/internal/domain/category"
|
||||
|
||||
func (i *impl) GetAll() ([]categorydomain.Category, error) {
|
||||
return i.categoryRepo.GetAll()
|
||||
}
|
||||
23
internal/services/category/impl.go
Normal file
23
internal/services/category/impl.go
Normal file
@ -0,0 +1,23 @@
|
||||
package categorysvc
|
||||
|
||||
import (
|
||||
categoryrepository "legalgo-BE-go/internal/accessor/category"
|
||||
categorydomain "legalgo-BE-go/internal/domain/category"
|
||||
)
|
||||
|
||||
type impl struct {
|
||||
categoryRepo categoryrepository.Category
|
||||
}
|
||||
|
||||
type Category interface {
|
||||
Create(categorydomain.CategoryReq) error
|
||||
GetAll() ([]categorydomain.Category, error)
|
||||
}
|
||||
|
||||
func New(
|
||||
categoryRepo categoryrepository.Category,
|
||||
) Category {
|
||||
return &impl{
|
||||
categoryRepo: categoryRepo,
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
serviceauth "legalgo-BE-go/internal/services/auth"
|
||||
categorysvc "legalgo-BE-go/internal/services/category"
|
||||
subscribesvc "legalgo-BE-go/internal/services/subscribe"
|
||||
subscribeplansvc "legalgo-BE-go/internal/services/subscribe_plan"
|
||||
tagsvc "legalgo-BE-go/internal/services/tag"
|
||||
@ -15,5 +16,6 @@ var Module = fx.Module("services",
|
||||
subscribeplansvc.New,
|
||||
subscribesvc.New,
|
||||
tagsvc.New,
|
||||
categorysvc.New,
|
||||
),
|
||||
)
|
||||
|
||||
@ -3,5 +3,5 @@ package tagsvc
|
||||
import tagdomain "legalgo-BE-go/internal/domain/tag"
|
||||
|
||||
func (i *impl) Create(spec tagdomain.TagReq) error {
|
||||
return i.TagRepo.Create(spec)
|
||||
return i.tagRepo.Create(spec)
|
||||
}
|
||||
|
||||
@ -3,5 +3,5 @@ package tagsvc
|
||||
import tagdomain "legalgo-BE-go/internal/domain/tag"
|
||||
|
||||
func (i *impl) GetAll() ([]tagdomain.Tag, error) {
|
||||
return i.TagRepo.GetAll()
|
||||
return i.tagRepo.GetAll()
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
type impl struct {
|
||||
TagRepo tagrepository.TagAccessor
|
||||
tagRepo tagrepository.TagAccessor
|
||||
}
|
||||
|
||||
type TagIntf interface {
|
||||
@ -17,7 +17,5 @@ type TagIntf interface {
|
||||
func New(
|
||||
tagRepo tagrepository.TagAccessor,
|
||||
) TagIntf {
|
||||
return &impl{
|
||||
TagRepo: tagRepo,
|
||||
}
|
||||
return &impl{tagRepo}
|
||||
}
|
||||
|
||||
189
openapi.yml
189
openapi.yml
@ -5,6 +5,50 @@ info:
|
||||
description: API for handling staff and user login, registration, and subscription plan creation.
|
||||
|
||||
paths:
|
||||
/staff/profile:
|
||||
get:
|
||||
summary: "get staff profile"
|
||||
tags:
|
||||
- Staff
|
||||
parameters:
|
||||
- name: "id"
|
||||
in: query
|
||||
description: "staff id"
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
responses:
|
||||
"200":
|
||||
description: Success get profile
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
data:
|
||||
type: object
|
||||
properties:
|
||||
id:
|
||||
type: string
|
||||
email:
|
||||
type: string
|
||||
username:
|
||||
type: string
|
||||
"400":
|
||||
description: Bad request
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
error:
|
||||
type: object
|
||||
properties:
|
||||
code:
|
||||
type: string
|
||||
message:
|
||||
type: string
|
||||
|
||||
/staff/login:
|
||||
post:
|
||||
summary: Login for staff
|
||||
@ -120,50 +164,6 @@ paths:
|
||||
message:
|
||||
type: string
|
||||
|
||||
/staff/profile:
|
||||
get:
|
||||
summary: "get staff profile"
|
||||
tags:
|
||||
- Staff
|
||||
parameters:
|
||||
- name: "id"
|
||||
in: query
|
||||
description: "staff id"
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
responses:
|
||||
"200":
|
||||
description: Success get profile
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
data:
|
||||
type: object
|
||||
properties:
|
||||
id:
|
||||
type: string
|
||||
email:
|
||||
type: string
|
||||
username:
|
||||
type: string
|
||||
"400":
|
||||
description: Bad request
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
error:
|
||||
type: object
|
||||
properties:
|
||||
code:
|
||||
type: string
|
||||
message:
|
||||
type: string
|
||||
|
||||
/user/profile:
|
||||
get:
|
||||
summary: "get staff profile"
|
||||
@ -530,3 +530,104 @@ paths:
|
||||
type: string
|
||||
message:
|
||||
type: string
|
||||
|
||||
/category:
|
||||
get:
|
||||
summary: Get all categories
|
||||
tags:
|
||||
- Category
|
||||
responses:
|
||||
"200":
|
||||
description: Successfully retrieved all categories
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
data:
|
||||
type: array
|
||||
items:
|
||||
type: object
|
||||
properties:
|
||||
id:
|
||||
type: string
|
||||
code:
|
||||
type: string
|
||||
name:
|
||||
type: string
|
||||
"400":
|
||||
description: Bad request
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
error:
|
||||
type: object
|
||||
properties:
|
||||
code:
|
||||
type: string
|
||||
message:
|
||||
type: string
|
||||
|
||||
/category/create:
|
||||
post:
|
||||
summary: Create a new category
|
||||
tags:
|
||||
- Tags
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
name:
|
||||
type: string
|
||||
code:
|
||||
type: string
|
||||
required:
|
||||
- code
|
||||
- name
|
||||
responses:
|
||||
"201":
|
||||
description: Category created
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
data:
|
||||
type: object
|
||||
properties:
|
||||
message:
|
||||
type: string
|
||||
example: "category created successfully."
|
||||
"400":
|
||||
description: Bad request
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
error:
|
||||
type: object
|
||||
properties:
|
||||
code:
|
||||
type: string
|
||||
message:
|
||||
type: string
|
||||
"409":
|
||||
description: Conflict (category code already exists)
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
error:
|
||||
type: object
|
||||
properties:
|
||||
code:
|
||||
type: string
|
||||
message:
|
||||
type: string
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user