-
Notifications
You must be signed in to change notification settings - Fork 2
/
custom_list.go
52 lines (44 loc) · 1.47 KB
/
custom_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package api
import (
"net/http"
"github.com/uadmin/uadmin"
"github.com/uadmin/uadmin-docs/sample_project/todo/models"
)
// CustomListAPIHandler !
func CustomListAPIHandler(w http.ResponseWriter, r *http.Request) {
// Fetch Data from DB
todo := []models.Todo{}
// Assigns a map as a string of interface to store any types of values
results := []map[string]interface{}{}
// "id" - order the todo model by id
// false - to sort in descending order
// 0 - start at index 0
// 5 - get five records
// &todo - todo model to execute
// "" - fetch the id of the model itself
uadmin.AdminPage("id", false, 0, 5, &todo, "")
// Loop to fetch the record of todo
for i := range todo {
// Accesses and fetches the record of the linking models in Todo
uadmin.Preload(&todo[i])
// Assigns the string of interface in each Todo fields
results = append(results, map[string]interface{}{
"ID": todo[i].ID,
"Name": todo[i].Name,
"Description": todo[i].Description,
// This returns only the name of the Category model, not the
// other fields
"Category": todo[i].Category.Name,
// This returns only the name of the Friend model, not the
// other fields
"Friend": todo[i].Friend.Name,
// This returns only the name of the Item model, not the other
// fields
"Item": todo[i].Item.Name,
"TargetDate": todo[i].TargetDate,
"Progress": todo[i].Progress,
})
}
// Prints the results in JSON format
uadmin.ReturnJSON(w, r, results)
}