Added search/items endpoint for searching all items.
This commit is contained in:
parent
b426f59be7
commit
012c2bd645
14
handlers.go
14
handlers.go
|
@ -260,6 +260,20 @@ func GetItemImageHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write(imageData)
|
w.Write(imageData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// searchItemsHandler handles the GET /items/search endpoint.
|
||||||
|
func SearchItemsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
query := r.URL.Query().Get("q")
|
||||||
|
if query == "" {
|
||||||
|
http.Error(w, "Search query is required", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(query)
|
||||||
|
var items []Item
|
||||||
|
db.Where("name GLOB ? OR description GLOB ?", "*"+query+"*", "*"+query+"*").Find(&items)
|
||||||
|
json.NewEncoder(w).Encode(items)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// getItemHandler handles the GET /items/{id} endpoint.
|
// getItemHandler handles the GET /items/{id} endpoint.
|
||||||
func GetItemHandler(w http.ResponseWriter, r *http.Request) {
|
func GetItemHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
|
2
main.go
2
main.go
|
@ -58,6 +58,8 @@ func main() {
|
||||||
router.Handle("/items/{id}", AuthMiddleware(http.HandlerFunc(UpdateItemHandler))).Methods("PUT", "OPTIONS")
|
router.Handle("/items/{id}", AuthMiddleware(http.HandlerFunc(UpdateItemHandler))).Methods("PUT", "OPTIONS")
|
||||||
router.Handle("/items/{id}", AuthMiddleware(http.HandlerFunc(DeleteItemHandler))).Methods("DELETE", "OPTIONS")
|
router.Handle("/items/{id}", AuthMiddleware(http.HandlerFunc(DeleteItemHandler))).Methods("DELETE", "OPTIONS")
|
||||||
router.Handle("/items/{id}/image", AuthMiddleware(http.HandlerFunc(GetItemImageHandler))).Methods("GET", "OPTIONS")
|
router.Handle("/items/{id}/image", AuthMiddleware(http.HandlerFunc(GetItemImageHandler))).Methods("GET", "OPTIONS")
|
||||||
|
fmt.Println("Registering route for search items...")
|
||||||
|
router.Handle("/search/items", AuthMiddleware(http.HandlerFunc(SearchItemsHandler))).Methods("GET", "OPTIONS")
|
||||||
// Add a new route for uploading an image with AuthMiddleware
|
// Add a new route for uploading an image with AuthMiddleware
|
||||||
router.HandleFunc("/items/{id}/upload", UploadItemImageHandler).
|
router.HandleFunc("/items/{id}/upload", UploadItemImageHandler).
|
||||||
Methods("POST").
|
Methods("POST").
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# API base URL
|
||||||
|
API_BASE_URL="http://localhost:8080"
|
||||||
|
|
||||||
|
# Login credentials
|
||||||
|
USERNAME="boxuser"
|
||||||
|
PASSWORD="boxuser"
|
||||||
|
|
||||||
|
# Get a new JWT token
|
||||||
|
TOKEN=$(curl -s -X POST -H "Content-Type: application/json" \
|
||||||
|
-d "{\"username\":\"$USERNAME\", \"password\":\"$PASSWORD\"}" \
|
||||||
|
"$API_BASE_URL/login" | jq -r '.token')
|
||||||
|
|
||||||
|
# Request all items using the obtained token
|
||||||
|
curl -X GET \
|
||||||
|
http://localhost:8080/search/items?q=brand \
|
||||||
|
-H "Authorization: Bearer $TOKEN" \
|
||||||
|
-H 'Content-Type: application/json'
|
||||||
|
|
||||||
|
# Print the response
|
||||||
|
echo "$response"
|
Loading…
Reference in New Issue