From fa69fd13d8840ad1134198bac3914595fc4cd944 Mon Sep 17 00:00:00 2001 From: laeshiny Date: Thu, 16 Mar 2017 23:55:50 +0900 Subject: [PATCH] Add sort, order parameter to Get Request of "project/id/inventory" For sorting the list, add parameters to Get Request - sort: sorting target name example) name (or type) - order: ordering manner example) asc (or desc) --- api/projects/inventory.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/api/projects/inventory.go b/api/projects/inventory.go index 658d5b5a..438cc501 100644 --- a/api/projects/inventory.go +++ b/api/projects/inventory.go @@ -41,11 +41,26 @@ func GetInventory(w http.ResponseWriter, r *http.Request) { project := context.Get(r, "project").(db.Project) var inv []db.Inventory - query, args, _ := squirrel.Select("*"). - From("project__inventory"). - Where("project_id=?", project.ID). - OrderBy("name asc"). - ToSql() + sort := r.URL.Query().Get("sort") + order := r.URL.Query().Get("order") + + if order != "asc" && order != "desc" { + order = "asc" + } + + q := squirrel.Select("*"). + From("project__inventory pi") + + switch sort { + case "name", "type": + q = q.Where("pi.project_id=?", project.ID). + OrderBy("pi." + sort + " " + order) + default: + q = q.Where("pi.project_id=?", project.ID). + OrderBy("pi.name " + order) + } + + query, args, _ := q.ToSql() if _, err := db.Mysql.Select(&inv, query, args...); err != nil { panic(err)