From f4afd8df62a94781fbef7d8a8d3a9393512cda93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Wed, 14 Apr 2021 17:27:03 +0200 Subject: [PATCH] Add search --- version.go | 2 +- web/frontend/src/App.vue | 1 + web/frontend/src/components/AttemptList.vue | 2 +- web/frontend/src/components/SearchResult.vue | 81 ++++++++++++++++++++ web/frontend/src/main.ts | 6 ++ web/server.go | 53 +++++++++---- 6 files changed, 130 insertions(+), 15 deletions(-) create mode 100644 web/frontend/src/components/SearchResult.vue diff --git a/version.go b/version.go index 71ba5ba..f9475da 100644 --- a/version.go +++ b/version.go @@ -2,7 +2,7 @@ package apiary import "fmt" -var Version = "v0.1.1" +var Version = "v0.1.2" var Build string func FullVersion() string { diff --git a/web/frontend/src/App.vue b/web/frontend/src/App.vue index f52b73e..ac630eb 100644 --- a/web/frontend/src/App.vue +++ b/web/frontend/src/App.vue @@ -18,6 +18,7 @@ Home Stats Attempts + Search diff --git a/web/frontend/src/components/AttemptList.vue b/web/frontend/src/components/AttemptList.vue index 4830464..8c1051c 100644 --- a/web/frontend/src/components/AttemptList.vue +++ b/web/frontend/src/components/AttemptList.vue @@ -8,7 +8,7 @@ hover :items="attempts" :fields="fields" - > + />

diff --git a/web/frontend/src/components/SearchResult.vue b/web/frontend/src/components/SearchResult.vue new file mode 100644 index 0000000..d96bc88 --- /dev/null +++ b/web/frontend/src/components/SearchResult.vue @@ -0,0 +1,81 @@ + + + + + diff --git a/web/frontend/src/main.ts b/web/frontend/src/main.ts index f2b87a2..4d13772 100644 --- a/web/frontend/src/main.ts +++ b/web/frontend/src/main.ts @@ -4,6 +4,7 @@ import VueRouter, { RouteConfig } from 'vue-router'; import AttemptList from '@/components/AttemptList.vue'; import Home from '@/components/Home.vue'; import Stats from '@/components/Stats.vue'; +import SearchResult from '@/components/SearchResult.vue'; import 'bootstrap/dist/css/bootstrap.css'; import 'bootstrap-vue/dist/bootstrap-vue.css'; import '@fontsource/rubik'; @@ -38,6 +39,11 @@ const routes: Array = [ name: 'Stats', component: Stats, }, + { + path: '/search', + name: 'Search', + component: SearchResult, + } ]; const router = new VueRouter({ diff --git a/web/server.go b/web/server.go index 6325ff2..de56226 100644 --- a/web/server.go +++ b/web/server.go @@ -216,24 +216,51 @@ func (s *Server) HandlerQuery(w http.ResponseWriter, r *http.Request) { queryType := r.URL.Query().Get("type") query := r.URL.Query().Get("query") - if query == "" || queryType == "" { + if query == "" { s.WriteAPIError(w, r, http.StatusBadRequest, "Invalid query or query type") return } - aq := store.AttemptQuery{ - QueryType: store.AttemptQueryType(queryType), - Query: query, - } + results := []models.LoginAttempt{} + if queryType == "" { + uq := store.AttemptQuery{ + QueryType: store.AttemptQueryType(store.AttemptQueryTypePassword), + Query: query, + } + pq := store.AttemptQuery{ + QueryType: store.AttemptQueryType(store.AttemptQueryTypeUsername), + Query: query, + } + userResults, err := s.store.Query(uq) + if err != nil { + s.WriteAPIError(w, r, http.StatusInternalServerError, "Unable to perform query") + s.ServerLogger.Warnw("Error performing query", "error", err) + return + } + passwordResults, err := s.store.Query(pq) + if err != nil { + s.WriteAPIError(w, r, http.StatusInternalServerError, "Unable to perform query") + s.ServerLogger.Warnw("Error performing query", "error", err) + return + } - results, err := s.store.Query(aq) - if err != nil { - s.WriteAPIError(w, r, http.StatusInternalServerError, "Unable to perform query") - s.ServerLogger.Warnw("Error performing query", "error", err) - return - } - if results == nil { - results = []models.LoginAttempt{} + results = append(results, userResults...) + results = append(results, passwordResults...) + + } else { + aq := store.AttemptQuery{ + QueryType: store.AttemptQueryType(queryType), + Query: query, + } + + queryResults, err := s.store.Query(aq) + if err != nil { + s.WriteAPIError(w, r, http.StatusInternalServerError, "Unable to perform query") + s.ServerLogger.Warnw("Error performing query", "error", err) + return + } + + results = append(results, queryResults...) } encoder := json.NewEncoder(w)