feat: tag routes
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
staffrepository "legalgo-BE-go/internal/accessor/staff"
|
||||
subscriberepository "legalgo-BE-go/internal/accessor/subscribe"
|
||||
subscribeplanrepository "legalgo-BE-go/internal/accessor/subscribeplan"
|
||||
tagrepository "legalgo-BE-go/internal/accessor/tag"
|
||||
userrepository "legalgo-BE-go/internal/accessor/user_repository"
|
||||
|
||||
"go.uber.org/fx"
|
||||
@@ -16,4 +17,5 @@ var Module = fx.Module("repository", fx.Provide(
|
||||
redisaccessor.New,
|
||||
subscribeplanrepository.New,
|
||||
subscriberepository.New,
|
||||
tagrepository.New,
|
||||
))
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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 (
|
||||
authhttp "legalgo-BE-go/internal/api/http/auth"
|
||||
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/cors"
|
||||
@@ -19,6 +20,7 @@ var Module = fx.Module("router",
|
||||
),
|
||||
authhttp.Module,
|
||||
subscribeplanhttp.Module,
|
||||
taghttp.Module,
|
||||
)
|
||||
|
||||
func initRouter() chi.Router {
|
||||
|
||||
@@ -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.",
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
@@ -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 {
|
||||
Email string `json:"email" validate:"required"`
|
||||
Password string `json:"password" validate:"required"`
|
||||
Phone string `json:"phone"`
|
||||
Phone string `json:"phone" validate:"required"`
|
||||
SubscribePlanID string `json:"subscribe_plan_id"`
|
||||
}
|
||||
|
||||
|
||||
@@ -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"
|
||||
subscribesvc "legalgo-BE-go/internal/services/subscribe"
|
||||
subscribeplansvc "legalgo-BE-go/internal/services/subscribe_plan"
|
||||
tagsvc "legalgo-BE-go/internal/services/tag"
|
||||
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
@@ -13,5 +14,6 @@ var Module = fx.Module("services",
|
||||
serviceauth.New,
|
||||
subscribeplansvc.New,
|
||||
subscribesvc.New,
|
||||
tagsvc.New,
|
||||
),
|
||||
)
|
||||
|
||||
@@ -5,10 +5,5 @@ import (
|
||||
)
|
||||
|
||||
func (s *SubsPlanSvc) GetAllPlan() ([]subscribeplandomain.SubscribePlan, error) {
|
||||
subsPlan, err := s.subsAccs.GetAll()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return subsPlan, nil
|
||||
return s.subsAccs.GetAll()
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user