chores: refactor struct and structure project
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package categoryhttp
|
||||
|
||||
import (
|
||||
authmiddleware "legalgo-BE-go/internal/api/http/middleware/auth"
|
||||
categorydomain "legalgo-BE-go/internal/domain/category"
|
||||
categorysvc "legalgo-BE-go/internal/services/category"
|
||||
"legalgo-BE-go/internal/utilities/response"
|
||||
@@ -16,52 +17,54 @@ func Create(
|
||||
validate *validator.Validate,
|
||||
categorySvc categorysvc.Category,
|
||||
) {
|
||||
router.Post("/category/create", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
router.
|
||||
With(authmiddleware.Authorize()).
|
||||
Post("/category/create", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var spec categorydomain.CategoryReq
|
||||
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(),
|
||||
)
|
||||
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
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 := 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
|
||||
}
|
||||
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",
|
||||
response.RespondJsonSuccess(ctx, w, struct {
|
||||
Message string
|
||||
}{
|
||||
Message: "category created successfully",
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ func GetAll(
|
||||
) {
|
||||
router.Get("/category", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
subsPlan, err := categorySvc.GetAllModel()
|
||||
subsPlan, err := categorySvc.GetAll()
|
||||
// subsPlan, err := categorySvc.GetAll()
|
||||
if err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
|
||||
@@ -5,4 +5,5 @@ import "go.uber.org/fx"
|
||||
var Module = fx.Module("categories", fx.Invoke(
|
||||
Create,
|
||||
GetAll,
|
||||
Update,
|
||||
))
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package categoryhttp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
authmiddleware "legalgo-BE-go/internal/api/http/middleware/auth"
|
||||
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 Update(
|
||||
router chi.Router,
|
||||
validate *validator.Validate,
|
||||
categorySvc categorysvc.Category,
|
||||
) {
|
||||
router.
|
||||
With(authmiddleware.Authorize()).
|
||||
Put("/category/{category_id}/update", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
categoryID := chi.URLParam(r, "category_id")
|
||||
|
||||
if categoryID == "" {
|
||||
response.RespondJsonErrorWithCode(
|
||||
ctx,
|
||||
w,
|
||||
fmt.Errorf("category id is not provided"),
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"category id is not provided",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
var spec categorydomain.CategoryReq
|
||||
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||
response.RespondJsonErrorWithCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"failed to unmarshal body",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if err := validate.Struct(spec); err != nil {
|
||||
response.RespondJsonErrorWithCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.(validator.ValidationErrors).Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if err := categorySvc.Update(categoryID, spec); err != nil {
|
||||
response.RespondJsonErrorWithCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
response.RespondJsonSuccess(ctx, w, struct {
|
||||
Message string
|
||||
}{
|
||||
Message: "update category success",
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user