mirror of
https://github.com/semaphoreui/semaphore.git
synced 2024-11-23 12:30:41 +01:00
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:
parent
b61833d4fb
commit
fa69fd13d8
@ -41,11 +41,26 @@ func GetInventory(w http.ResponseWriter, r *http.Request) {
|
|||||||
project := context.Get(r, "project").(db.Project)
|
project := context.Get(r, "project").(db.Project)
|
||||||
var inv []db.Inventory
|
var inv []db.Inventory
|
||||||
|
|
||||||
query, args, _ := squirrel.Select("*").
|
sort := r.URL.Query().Get("sort")
|
||||||
From("project__inventory").
|
order := r.URL.Query().Get("order")
|
||||||
Where("project_id=?", project.ID).
|
|
||||||
OrderBy("name asc").
|
if order != "asc" && order != "desc" {
|
||||||
ToSql()
|
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 {
|
if _, err := db.Mysql.Select(&inv, query, args...); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
Loading…
Reference in New Issue
Block a user