76 lines
1.4 KiB
Go
Raw Normal View History

package staffhttp
2025-02-27 18:59:58 +08:00
import (
"errors"
staffdomain "legalgo-BE-go/internal/domain/staff"
2025-02-27 18:59:58 +08:00
authsvc "legalgo-BE-go/internal/services/auth"
"legalgo-BE-go/internal/utilities/response"
"legalgo-BE-go/internal/utilities/utils"
"net/http"
"github.com/go-chi/chi/v5"
)
func Update(
2025-02-27 18:59:58 +08:00
router chi.Router,
authSvc authsvc.Auth,
2025-02-27 18:59:58 +08:00
) {
router.Patch("/staff/{id}/update", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
id := chi.URLParam(r, "id")
if id == "" {
response.ResponseWithErrorCode(
ctx,
w,
errors.New("provided id is empty"),
response.ErrBadRequest.Code,
response.ErrBadRequest.HttpCode,
"required params is not provided",
)
return
}
var spec staffdomain.StaffRegister
2025-02-27 18:59:58 +08:00
if err := utils.UnmarshalBody(r, &spec); err != nil {
response.ResponseWithErrorCode(
ctx,
w,
err,
response.ErrBadRequest.Code,
response.ErrBadRequest.HttpCode,
"failed to unmarshal request",
)
return
}
staff := staffdomain.Staff{
2025-02-27 18:59:58 +08:00
ID: id,
Email: spec.Email,
Password: spec.Password,
2025-03-03 13:23:04 +08:00
Name: spec.Name,
2025-02-27 18:59:58 +08:00
}
if err := authSvc.UpdateStaff(staff); err != nil {
response.ResponseWithErrorCode(
ctx,
w,
err,
response.ErrBadRequest.Code,
response.ErrBadRequest.HttpCode,
err.Error(),
)
return
}
2025-03-03 13:23:04 +08:00
responsePayload := struct {
2025-02-27 18:59:58 +08:00
Message string
2025-03-03 13:23:04 +08:00
}{
2025-02-27 18:59:58 +08:00
Message: "update staff success",
}
response.RespondJsonSuccess(ctx, w, responsePayload)
})
}