94 lines
1.9 KiB
Go
94 lines
1.9 KiB
Go
|
|
package newshttp
|
||
|
|
|
||
|
|
import (
|
||
|
|
staffrepository "legalgo-BE-go/internal/accessor/staff"
|
||
|
|
newsdomain "legalgo-BE-go/internal/domain/news"
|
||
|
|
newssvc "legalgo-BE-go/internal/services/news"
|
||
|
|
"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 Create(
|
||
|
|
validate *validator.Validate,
|
||
|
|
newsSvc newssvc.News,
|
||
|
|
staffRepo staffrepository.StaffIntf,
|
||
|
|
router chi.Router,
|
||
|
|
) {
|
||
|
|
router.Post("/news/create", func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
ctx := r.Context()
|
||
|
|
var spec newsdomain.NewsReq
|
||
|
|
|
||
|
|
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||
|
|
response.ResponseWithErrorCode(
|
||
|
|
ctx,
|
||
|
|
w,
|
||
|
|
err,
|
||
|
|
response.ErrBadRequest.Code,
|
||
|
|
response.ErrBadRequest.HttpCode,
|
||
|
|
err.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
|
||
|
|
}
|
||
|
|
|
||
|
|
destructedToken, err := utils.GetTokenDetail(r)
|
||
|
|
if err != nil {
|
||
|
|
response.ResponseWithErrorCode(
|
||
|
|
ctx,
|
||
|
|
w,
|
||
|
|
err,
|
||
|
|
response.ErrBadRequest.Code,
|
||
|
|
response.ErrBadRequest.HttpCode,
|
||
|
|
err.Error(),
|
||
|
|
)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
staffProfile, err := staffRepo.GetStaffByEmail(destructedToken.Email)
|
||
|
|
if err != nil {
|
||
|
|
response.ResponseWithErrorCode(
|
||
|
|
ctx,
|
||
|
|
w,
|
||
|
|
err,
|
||
|
|
response.ErrBadRequest.Code,
|
||
|
|
response.ErrBadRequest.HttpCode,
|
||
|
|
err.Error(),
|
||
|
|
)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := newsSvc.CreateModel(spec, staffProfile.ID); err != nil {
|
||
|
|
response.ResponseWithErrorCode(
|
||
|
|
ctx,
|
||
|
|
w,
|
||
|
|
err,
|
||
|
|
response.ErrBadRequest.Code,
|
||
|
|
response.ErrBadRequest.HttpCode,
|
||
|
|
err.Error(),
|
||
|
|
)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
response.RespondJsonSuccess(ctx, w, struct {
|
||
|
|
Message string
|
||
|
|
}{
|
||
|
|
Message: "news created successfully.",
|
||
|
|
})
|
||
|
|
})
|
||
|
|
}
|