feat: update CreateUser to use dto.RegisterRequest and update MountRoutes to apply validation middleware
This commit is contained in:
@@ -99,13 +99,9 @@ func (h *UserHandler) GetUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
// @Failure 500 {object} UserResponse "Internal server error"
|
// @Failure 500 {object} UserResponse "Internal server error"
|
||||||
// @Router /api/users [post]
|
// @Router /api/users [post]
|
||||||
func (h *UserHandler) CreateUser(w http.ResponseWriter, r *http.Request) {
|
func (h *UserHandler) CreateUser(w http.ResponseWriter, r *http.Request) {
|
||||||
var req struct {
|
req, ok := GetValidatedDTO[dto.RegisterRequest](r)
|
||||||
Username string `json:"username"`
|
if !ok {
|
||||||
Email string `json:"email"`
|
SendErrorResponse(w, "Invalid request", http.StatusBadRequest)
|
||||||
Password string `json:"password"`
|
|
||||||
}
|
|
||||||
|
|
||||||
if !DecodeJSONRequest(w, r, &req) {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,7 +185,7 @@ func (h *UserHandler) MountRoutes(r chi.Router, config RouteModuleConfig) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected.Get("/users", h.GetUsers)
|
protected.Get("/users", h.GetUsers)
|
||||||
protected.Post("/users", h.CreateUser)
|
protected.Post("/users", WithValidation[dto.RegisterRequest](config.ValidationMiddleware, h.CreateUser))
|
||||||
protected.Get("/users/{id}", h.GetUser)
|
protected.Get("/users/{id}", h.GetUser)
|
||||||
protected.Get("/users/{id}/posts", h.GetUserPosts)
|
protected.Get("/users/{id}/posts", h.GetUserPosts)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user