2025-03-07 17:39:26 +08:00
|
|
|
package staffhttp
|
2025-02-27 18:59:58 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
2025-03-05 21:21:44 +08:00
|
|
|
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"
|
|
|
|
|
)
|
|
|
|
|
|
2025-03-07 17:39:26 +08:00
|
|
|
func Update(
|
2025-02-27 18:59:58 +08:00
|
|
|
router chi.Router,
|
2025-03-05 21:21:44 +08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-05 21:21:44 +08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-05 21:21:44 +08:00
|
|
|
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)
|
|
|
|
|
})
|
|
|
|
|
}
|