From 1a863a06cfe635f7461a2dd85a2dba1b2465fcfc Mon Sep 17 00:00:00 2001 From: liangchenye Date: Tue, 8 Mar 2016 10:24:29 +0800 Subject: [PATCH] remove the useless pointer of NextPage field; check namespace notfound error Signed-off-by: liangchenye --- api/v1/models.go | 2 +- api/v1/routes.go | 15 ++++++++++++--- database/pgsql/queries.go | 3 ++- database/pgsql/vulnerability.go | 10 +++++++++- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/api/v1/models.go b/api/v1/models.go index fc2a4e60..f3b3c5e0 100644 --- a/api/v1/models.go +++ b/api/v1/models.go @@ -277,7 +277,7 @@ type NamespaceEnvelope struct { type VulnerabilityEnvelope struct { Vulnerability *Vulnerability `json:"Vulnerability,omitempty"` Vulnerabilities *[]Vulnerability `json:"Vulnerabilities,omitempty"` - NextPage *string `json:"NextPage,omitempty"` + NextPage string `json:"NextPage,omitempty"` Error *Error `json:"Error,omitempty"` } diff --git a/api/v1/routes.go b/api/v1/routes.go index af2d8b3f..3edbe769 100644 --- a/api/v1/routes.go +++ b/api/v1/routes.go @@ -212,8 +212,17 @@ func getVulnerabilities(w http.ResponseWriter, r *http.Request, p httprouter.Par } } - dbVulns, nextPage, err := ctx.Store.ListVulnerabilities(p.ByName("namespaceName"), limit, page) - if err != nil { + namespace := p.ByName("namespaceName") + if namespace == "" { + writeResponse(w, r, http.StatusBadRequest, VulnerabilityEnvelope{Error: &Error{"namespace should not be empty"}}) + return getNotificationRoute, http.StatusBadRequest + } + + dbVulns, nextPage, err := ctx.Store.ListVulnerabilities(namespace, limit, page) + if err == cerrors.ErrNotFound { + writeResponse(w, r, http.StatusNotFound, VulnerabilityEnvelope{Error: &Error{err.Error()}}) + return getVulnerabilityRoute, http.StatusNotFound + } else if err != nil { writeResponse(w, r, http.StatusInternalServerError, VulnerabilityEnvelope{Error: &Error{err.Error()}}) return getVulnerabilitiesRoute, http.StatusInternalServerError } @@ -234,7 +243,7 @@ func getVulnerabilities(w http.ResponseWriter, r *http.Request, p httprouter.Par nextPageStr = string(nextPageBytes) } - writeResponse(w, r, http.StatusOK, VulnerabilityEnvelope{Vulnerabilities: &vulns, NextPage: &nextPageStr}) + writeResponse(w, r, http.StatusOK, VulnerabilityEnvelope{Vulnerabilities: &vulns, NextPage: nextPageStr}) return getVulnerabilitiesRoute, http.StatusOK } diff --git a/database/pgsql/queries.go b/database/pgsql/queries.go index 0b67ecf5..aba20d70 100644 --- a/database/pgsql/queries.go +++ b/database/pgsql/queries.go @@ -38,7 +38,8 @@ const ( UNION SELECT id FROM new_namespace` - listNamespace = `SELECT id, name FROM Namespace` + searchNamespace = `SELECT id FROM Namespace WHERE name = $1` + listNamespace = `SELECT id, name FROM Namespace` // feature.go soiFeature = ` diff --git a/database/pgsql/vulnerability.go b/database/pgsql/vulnerability.go index 035f1413..74ee9828 100644 --- a/database/pgsql/vulnerability.go +++ b/database/pgsql/vulnerability.go @@ -31,6 +31,15 @@ import ( func (pgSQL *pgSQL) ListVulnerabilities(namespaceName string, limit int, startID int) ([]database.Vulnerability, int, error) { defer observeQueryTime("listVulnerabilities", "all", time.Now()) + // Query Namespace. + var id int + err := pgSQL.QueryRow(searchNamespace, namespaceName).Scan(&id) + if err != nil { + return nil, -1, handleError("searchNamespace", err) + } else if id == 0 { + return nil, -1, cerrors.ErrNotFound + } + // Query. query := searchVulnerabilityBase + searchVulnerabilityByNamespace rows, err := pgSQL.Query(query, namespaceName, startID, limit+1) @@ -71,7 +80,6 @@ func (pgSQL *pgSQL) ListVulnerabilities(namespaceName string, limit int, startID return nil, -1, handleError("searchVulnerabilityByNamespace.Rows()", err) } - fmt.Println(nextID) return vulns, nextID, nil }