2025-03-09 00:47:24 +08:00
|
|
|
package adshttp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
authmiddleware "legalgo-BE-go/internal/api/http/middleware/auth"
|
|
|
|
|
adsdomain "legalgo-BE-go/internal/domain/ads"
|
|
|
|
|
adssvc "legalgo-BE-go/internal/services/ads"
|
|
|
|
|
"legalgo-BE-go/internal/utilities/response"
|
2025-03-10 10:28:23 +08:00
|
|
|
"legalgo-BE-go/internal/utilities/utils"
|
2025-03-09 00:47:24 +08:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
2025-03-10 10:28:23 +08:00
|
|
|
"github.com/go-playground/validator/v10"
|
2025-03-09 00:47:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Create(
|
|
|
|
|
router chi.Router,
|
|
|
|
|
adsSvc adssvc.Ads,
|
2025-03-10 10:28:23 +08:00
|
|
|
validate *validator.Validate,
|
2025-03-09 00:47:24 +08:00
|
|
|
) {
|
2025-03-10 10:28:23 +08:00
|
|
|
router.
|
|
|
|
|
With(authmiddleware.Authorize()).
|
|
|
|
|
Post("/ads/create", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
var spec adsdomain.AdsReq
|
2025-03-09 00:47:24 +08:00
|
|
|
|
2025-03-10 10:28:23 +08:00
|
|
|
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
|
|
|
|
response.RespondJsonErrorWithCode(
|
|
|
|
|
ctx,
|
|
|
|
|
w,
|
|
|
|
|
err,
|
|
|
|
|
response.ErrBadRequest.Code,
|
|
|
|
|
response.ErrBadRequest.HttpCode,
|
|
|
|
|
"failed to unmarshal body",
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-03-09 00:47:24 +08:00
|
|
|
|
2025-03-10 10:28:23 +08:00
|
|
|
if err := validate.Struct(spec); err != nil {
|
|
|
|
|
response.ResponseWithErrorCode(
|
|
|
|
|
ctx,
|
|
|
|
|
w,
|
|
|
|
|
err,
|
|
|
|
|
response.ErrBadRequest.Code,
|
|
|
|
|
response.ErrBadRequest.HttpCode,
|
|
|
|
|
err.(validator.ValidationErrors).Error(),
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-03-09 00:47:24 +08:00
|
|
|
|
2025-03-10 10:28:23 +08:00
|
|
|
if err := adsSvc.Create(spec); err != nil {
|
|
|
|
|
response.RespondJsonErrorWithCode(
|
|
|
|
|
ctx,
|
|
|
|
|
w,
|
|
|
|
|
err,
|
|
|
|
|
response.ErrBadRequest.Code,
|
|
|
|
|
response.ErrBadRequest.HttpCode,
|
|
|
|
|
err.Error(),
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-03-09 00:47:24 +08:00
|
|
|
|
2025-03-10 10:28:23 +08:00
|
|
|
response.RespondJsonSuccess(ctx, w, struct {
|
|
|
|
|
Message string
|
|
|
|
|
}{
|
|
|
|
|
Message: "ads created successfully",
|
|
|
|
|
})
|
2025-03-09 00:47:24 +08:00
|
|
|
})
|
|
|
|
|
}
|