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)
This commit is contained in:
laeshiny 2017-03-16 23:55:50 +09:00
parent b61833d4fb
commit fa69fd13d8

View File

@ -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)