Skip to content

Commit 45130ad

Browse files
committed
feat: add filtering by query parameter term
1 parent fa3f105 commit 45130ad

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

internal/handler/retrival.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"database/sql"
77
"encoding/json"
88
"errors"
9+
"fmt"
910
"log"
1011
"net/http"
1112

@@ -53,8 +54,9 @@ func GetPostByIdHandler(db *sql.DB) http.Handler {
5354

5455
func GetAllPostsHandler(db *sql.DB) http.Handler {
5556
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
56-
queryStmt := "select * from posts"
57-
rows, err := db.QueryContext(r.Context(), queryStmt)
57+
term := r.URL.Query().Get("term")
58+
queryStmt := "select * from posts where title like $1 or content like $1 or category like $1"
59+
rows, err := db.QueryContext(r.Context(), queryStmt, fmt.Sprintf("%%%s%%", term))
5860
if err != nil {
5961
log.Printf("Error querying posts: %v", err)
6062
http.Error(w, "Internal server error", http.StatusInternalServerError)

0 commit comments

Comments
 (0)