refactor: use GetValidatedDTO for request validation
This commit is contained in:
@@ -109,9 +109,8 @@ func (h *PostHandler) GetPost(w http.ResponseWriter, r *http.Request) {
|
|||||||
// @Failure 500 {object} PostResponse "Internal server error"
|
// @Failure 500 {object} PostResponse "Internal server error"
|
||||||
// @Router /api/posts [post]
|
// @Router /api/posts [post]
|
||||||
func (h *PostHandler) CreatePost(w http.ResponseWriter, r *http.Request) {
|
func (h *PostHandler) CreatePost(w http.ResponseWriter, r *http.Request) {
|
||||||
req, ok := GetValidatedDTO[dto.CreatePostRequest](r)
|
request, ok := GetValidatedDTO[dto.CreatePostRequest](w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
SendErrorResponse(w, "Invalid request", http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,9 +119,9 @@ func (h *PostHandler) CreatePost(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
title := security.SanitizeInput(req.Title)
|
title := security.SanitizeInput(request.Title)
|
||||||
url := security.SanitizeURL(req.URL)
|
url := security.SanitizeURL(request.URL)
|
||||||
content := security.SanitizePostContent(req.Content)
|
content := security.SanitizePostContent(request.Content)
|
||||||
|
|
||||||
if url == "" {
|
if url == "" {
|
||||||
SendErrorResponse(w, "Invalid URL", http.StatusBadRequest)
|
SendErrorResponse(w, "Invalid URL", http.StatusBadRequest)
|
||||||
@@ -263,14 +262,13 @@ func (h *PostHandler) UpdatePost(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req, ok := GetValidatedDTO[dto.UpdatePostRequest](r)
|
request, ok := GetValidatedDTO[dto.UpdatePostRequest](w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
SendErrorResponse(w, "Invalid request", http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
title := security.SanitizeInput(req.Title)
|
title := security.SanitizeInput(request.Title)
|
||||||
content := security.SanitizePostContent(req.Content)
|
content := security.SanitizePostContent(request.Content)
|
||||||
|
|
||||||
post.Title = title
|
post.Title = title
|
||||||
post.Content = content
|
post.Content = content
|
||||||
|
|||||||
Reference in New Issue
Block a user