feat: tag routes
This commit is contained in:
parent
613379c8a5
commit
acd80480dd
11
database/category_model.go
Normal file
11
database/category_model.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type Category struct {
|
||||||
|
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"`
|
||||||
|
UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`
|
||||||
|
}
|
||||||
@ -43,5 +43,7 @@ func (db *DB) Migrate() error {
|
|||||||
&SubscribePlan{},
|
&SubscribePlan{},
|
||||||
&Subscribe{},
|
&Subscribe{},
|
||||||
&User{},
|
&User{},
|
||||||
|
&Tag{},
|
||||||
|
&Category{},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tags struct {
|
type Tag struct {
|
||||||
ID string `gorm:"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"`
|
||||||
@ -5,6 +5,7 @@ import (
|
|||||||
staffrepository "legalgo-BE-go/internal/accessor/staff"
|
staffrepository "legalgo-BE-go/internal/accessor/staff"
|
||||||
subscriberepository "legalgo-BE-go/internal/accessor/subscribe"
|
subscriberepository "legalgo-BE-go/internal/accessor/subscribe"
|
||||||
subscribeplanrepository "legalgo-BE-go/internal/accessor/subscribeplan"
|
subscribeplanrepository "legalgo-BE-go/internal/accessor/subscribeplan"
|
||||||
|
tagrepository "legalgo-BE-go/internal/accessor/tag"
|
||||||
userrepository "legalgo-BE-go/internal/accessor/user_repository"
|
userrepository "legalgo-BE-go/internal/accessor/user_repository"
|
||||||
|
|
||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
@ -16,4 +17,5 @@ var Module = fx.Module("repository", fx.Provide(
|
|||||||
redisaccessor.New,
|
redisaccessor.New,
|
||||||
subscribeplanrepository.New,
|
subscribeplanrepository.New,
|
||||||
subscriberepository.New,
|
subscriberepository.New,
|
||||||
|
tagrepository.New,
|
||||||
))
|
))
|
||||||
|
|||||||
21
internal/accessor/tag/create.go
Normal file
21
internal/accessor/tag/create.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package tagrepository
|
||||||
|
|
||||||
|
import (
|
||||||
|
tagdomain "legalgo-BE-go/internal/domain/tag"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (acc *accessor) Create(spec tagdomain.TagReq) error {
|
||||||
|
data := &tagdomain.Tag{
|
||||||
|
ID: uuid.NewString(),
|
||||||
|
Code: spec.Code,
|
||||||
|
Name: spec.Name,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := acc.DB.Create(&data).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
15
internal/accessor/tag/get_all.go
Normal file
15
internal/accessor/tag/get_all.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package tagrepository
|
||||||
|
|
||||||
|
import (
|
||||||
|
tagdomain "legalgo-BE-go/internal/domain/tag"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (acc *accessor) GetAll() ([]tagdomain.Tag, error) {
|
||||||
|
var tags []tagdomain.Tag
|
||||||
|
|
||||||
|
if err := acc.DB.Find(&tags).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return tags, nil
|
||||||
|
}
|
||||||
21
internal/accessor/tag/impl.go
Normal file
21
internal/accessor/tag/impl.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package tagrepository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"legalgo-BE-go/database"
|
||||||
|
tagdomain "legalgo-BE-go/internal/domain/tag"
|
||||||
|
)
|
||||||
|
|
||||||
|
type accessor struct {
|
||||||
|
DB *database.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
type TagAccessor interface {
|
||||||
|
Create(tagdomain.TagReq) error
|
||||||
|
GetAll() ([]tagdomain.Tag, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(
|
||||||
|
db *database.DB,
|
||||||
|
) TagAccessor {
|
||||||
|
return &accessor{db}
|
||||||
|
}
|
||||||
@ -3,6 +3,7 @@ package internalhttp
|
|||||||
import (
|
import (
|
||||||
authhttp "legalgo-BE-go/internal/api/http/auth"
|
authhttp "legalgo-BE-go/internal/api/http/auth"
|
||||||
subscribeplanhttp "legalgo-BE-go/internal/api/http/subscribe_plan"
|
subscribeplanhttp "legalgo-BE-go/internal/api/http/subscribe_plan"
|
||||||
|
taghttp "legalgo-BE-go/internal/api/http/tag"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/go-chi/cors"
|
"github.com/go-chi/cors"
|
||||||
@ -19,6 +20,7 @@ var Module = fx.Module("router",
|
|||||||
),
|
),
|
||||||
authhttp.Module,
|
authhttp.Module,
|
||||||
subscribeplanhttp.Module,
|
subscribeplanhttp.Module,
|
||||||
|
taghttp.Module,
|
||||||
)
|
)
|
||||||
|
|
||||||
func initRouter() chi.Router {
|
func initRouter() chi.Router {
|
||||||
|
|||||||
67
internal/api/http/tag/create.go
Normal file
67
internal/api/http/tag/create.go
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
package taghttp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
tagdomain "legalgo-BE-go/internal/domain/tag"
|
||||||
|
tagsvc "legalgo-BE-go/internal/services/tag"
|
||||||
|
"legalgo-BE-go/internal/utilities/response"
|
||||||
|
"legalgo-BE-go/internal/utilities/utils"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Create(
|
||||||
|
router chi.Router,
|
||||||
|
validate *validator.Validate,
|
||||||
|
tagSvc tagsvc.TagIntf,
|
||||||
|
) {
|
||||||
|
router.Post("/tag/create", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ctx := r.Context()
|
||||||
|
|
||||||
|
var spec tagdomain.TagReq
|
||||||
|
|
||||||
|
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||||
|
response.ResponseWithErrorCode(
|
||||||
|
ctx,
|
||||||
|
w,
|
||||||
|
err,
|
||||||
|
response.ErrBadRequest.Code,
|
||||||
|
response.ErrBadRequest.HttpCode,
|
||||||
|
"failed to unmarshal request",
|
||||||
|
)
|
||||||
|
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 := tagSvc.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: "tag created successfully.",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
32
internal/api/http/tag/get.go
Normal file
32
internal/api/http/tag/get.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package taghttp
|
||||||
|
|
||||||
|
import (
|
||||||
|
tagsvc "legalgo-BE-go/internal/services/tag"
|
||||||
|
"legalgo-BE-go/internal/utilities/response"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Get(
|
||||||
|
router chi.Router,
|
||||||
|
tagSvc tagsvc.TagIntf,
|
||||||
|
) {
|
||||||
|
router.Get("/tag", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ctx := r.Context()
|
||||||
|
subsPlan, err := tagSvc.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/tag/module.go
Normal file
8
internal/api/http/tag/module.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package taghttp
|
||||||
|
|
||||||
|
import "go.uber.org/fx"
|
||||||
|
|
||||||
|
var Module = fx.Module("tag", fx.Invoke(
|
||||||
|
Create,
|
||||||
|
Get,
|
||||||
|
))
|
||||||
@ -3,7 +3,7 @@ package authdomain
|
|||||||
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"`
|
||||||
Phone string `json:"phone"`
|
Phone string `json:"phone" validate:"required"`
|
||||||
SubscribePlanID string `json:"subscribe_plan_id"`
|
SubscribePlanID string `json:"subscribe_plan_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
12
internal/domain/tag/spec.go
Normal file
12
internal/domain/tag/spec.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package tagdomain
|
||||||
|
|
||||||
|
type TagReq struct {
|
||||||
|
Code string `json:"code" validate:"required"`
|
||||||
|
Name string `json:"name" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Tag struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Code string `json:"code"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
@ -4,6 +4,7 @@ import (
|
|||||||
serviceauth "legalgo-BE-go/internal/services/auth"
|
serviceauth "legalgo-BE-go/internal/services/auth"
|
||||||
subscribesvc "legalgo-BE-go/internal/services/subscribe"
|
subscribesvc "legalgo-BE-go/internal/services/subscribe"
|
||||||
subscribeplansvc "legalgo-BE-go/internal/services/subscribe_plan"
|
subscribeplansvc "legalgo-BE-go/internal/services/subscribe_plan"
|
||||||
|
tagsvc "legalgo-BE-go/internal/services/tag"
|
||||||
|
|
||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
)
|
)
|
||||||
@ -13,5 +14,6 @@ var Module = fx.Module("services",
|
|||||||
serviceauth.New,
|
serviceauth.New,
|
||||||
subscribeplansvc.New,
|
subscribeplansvc.New,
|
||||||
subscribesvc.New,
|
subscribesvc.New,
|
||||||
|
tagsvc.New,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|||||||
@ -5,10 +5,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (s *SubsPlanSvc) GetAllPlan() ([]subscribeplandomain.SubscribePlan, error) {
|
func (s *SubsPlanSvc) GetAllPlan() ([]subscribeplandomain.SubscribePlan, error) {
|
||||||
subsPlan, err := s.subsAccs.GetAll()
|
return s.subsAccs.GetAll()
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return subsPlan, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
7
internal/services/tag/create.go
Normal file
7
internal/services/tag/create.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package tagsvc
|
||||||
|
|
||||||
|
import tagdomain "legalgo-BE-go/internal/domain/tag"
|
||||||
|
|
||||||
|
func (i *impl) Create(spec tagdomain.TagReq) error {
|
||||||
|
return i.TagRepo.Create(spec)
|
||||||
|
}
|
||||||
7
internal/services/tag/get_all.go
Normal file
7
internal/services/tag/get_all.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package tagsvc
|
||||||
|
|
||||||
|
import tagdomain "legalgo-BE-go/internal/domain/tag"
|
||||||
|
|
||||||
|
func (i *impl) GetAll() ([]tagdomain.Tag, error) {
|
||||||
|
return i.TagRepo.GetAll()
|
||||||
|
}
|
||||||
23
internal/services/tag/impl.go
Normal file
23
internal/services/tag/impl.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package tagsvc
|
||||||
|
|
||||||
|
import (
|
||||||
|
tagrepository "legalgo-BE-go/internal/accessor/tag"
|
||||||
|
tagdomain "legalgo-BE-go/internal/domain/tag"
|
||||||
|
)
|
||||||
|
|
||||||
|
type impl struct {
|
||||||
|
TagRepo tagrepository.TagAccessor
|
||||||
|
}
|
||||||
|
|
||||||
|
type TagIntf interface {
|
||||||
|
Create(tagdomain.TagReq) error
|
||||||
|
GetAll() ([]tagdomain.Tag, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(
|
||||||
|
tagRepo tagrepository.TagAccessor,
|
||||||
|
) TagIntf {
|
||||||
|
return &impl{
|
||||||
|
TagRepo: tagRepo,
|
||||||
|
}
|
||||||
|
}
|
||||||
101
openapi.yml
101
openapi.yml
@ -429,3 +429,104 @@ paths:
|
|||||||
type: string
|
type: string
|
||||||
message:
|
message:
|
||||||
type: string
|
type: string
|
||||||
|
|
||||||
|
/tag:
|
||||||
|
get:
|
||||||
|
summary: Get all tags
|
||||||
|
tags:
|
||||||
|
- Tags
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: Successfully retrieved all tags
|
||||||
|
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
|
||||||
|
|
||||||
|
/tag/create:
|
||||||
|
post:
|
||||||
|
summary: Create a new tag
|
||||||
|
tags:
|
||||||
|
- Tags
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
code:
|
||||||
|
type: string
|
||||||
|
required:
|
||||||
|
- code
|
||||||
|
- name
|
||||||
|
responses:
|
||||||
|
"201":
|
||||||
|
description: Tag created
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
data:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
message:
|
||||||
|
type: string
|
||||||
|
example: "tag 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 (tag 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