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)
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user