2025-02-24 16:48:20 +08:00
|
|
|
package subscribeplanhttp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2025-02-27 07:25:25 +08:00
|
|
|
subscribeplandomain "legalgo-BE-go/internal/domain/subscribe_plan"
|
|
|
|
|
subscribeplansvc "legalgo-BE-go/internal/services/subscribe_plan"
|
|
|
|
|
"legalgo-BE-go/internal/utilities/response"
|
|
|
|
|
"legalgo-BE-go/internal/utilities/utils"
|
|
|
|
|
|
2025-02-24 16:48:20 +08:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func CreateSubscribePlan(
|
|
|
|
|
router chi.Router,
|
|
|
|
|
validate *validator.Validate,
|
|
|
|
|
subsSvc subscribeplansvc.SubsPlanIntf,
|
|
|
|
|
) {
|
|
|
|
|
router.Post("/subscribe-plan/create", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
|
|
var spec subscribeplandomain.SubscribePlanReq
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-24 20:18:09 +08:00
|
|
|
if err := subsSvc.CreatePlan(spec); err != nil {
|
2025-02-24 16:48:20 +08:00
|
|
|
response.ResponseWithErrorCode(
|
|
|
|
|
ctx,
|
|
|
|
|
w,
|
|
|
|
|
err,
|
|
|
|
|
response.ErrCreateEntity.Code,
|
|
|
|
|
response.ErrCreateEntity.HttpCode,
|
|
|
|
|
err.Error(),
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response.RespondJsonSuccess(ctx, w, struct {
|
|
|
|
|
Message string
|
|
|
|
|
}{
|
2025-02-25 11:07:17 +08:00
|
|
|
Message: "subscription plan created successfully.",
|
2025-02-24 16:48:20 +08:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|