Commit 90f4ed07906acbb91fca30874806b7bf72804f54

Authored by Marko Tikvić
1 parent 6f4b8a7111
Exists in master and in 1 other branch v2

sped up loadTable()

1 1 TODO:
2 2 * http utility:
3   - 1. add parameters to the ProcessHeaders to enable/disable token/role-access-rights checks
4   - 2. check for Content-Type header, if clients expects something other than JSON respond with appropriate HTTP code
  3 +1. add parameters to the ProcessHeaders to enable/disable token/role-access-rights checks
  4 +2. check for Content-Type header, if clients expects something other than JSON respond with appropriate HTTP code
... ...
... ... @@ -5,8 +5,8 @@ import (
5 5 "encoding/json"
6 6 )
7 7  
8   -var _apiVersion = ""
9   -var _authEndPoint = ""
  8 +var _apiVersion = "/api/v1"
  9 +var _authEndPoint = "/token"
10 10  
11 11 func SetApiVersion(ver string) string {
12 12 _apiVersion = ver
... ... @@ -17,10 +17,6 @@ func SetAuthEndpoint(ep string) {
17 17 _authEndPoint = ep
18 18 }
19 19  
20   -////
21   -//// ERROR UTILITY
22   -////
23   -
24 20 const templateHttpErr500_EN = "An internal server error has occurred."
25 21 const templateHttpErr500_RS = "Došlo je do greške na serveru."
26 22 const templateHttpErr400_EN = "Bad request: invalid request body."
... ... @@ -63,13 +59,9 @@ func RespondWithHttpError500(w http.ResponseWriter, req *http.Request) {
63 59 })
64 60 }
65 61  
66   -////
67   -//// HANDLER FUNC WRAPPER
68   -////
69   -
70   -//TODO: Add parameters to enable/disable token and roles authorization checks
  62 +//TODO: Add parameters to enable/disable roles authorization checks
71 63 // Sets common headers and checks for token validity.
72   -func ProcessHeaders(fn http.HandlerFunc, authEnabled bool) http.HandlerFunc {
  64 +func ProcessHeaders(handlerFunc http.HandlerFunc, authEnabled bool) http.HandlerFunc {
73 65 return func(w http.ResponseWriter, req *http.Request) {
74 66 // @TODO: check Content-type header (must be application/json)
75 67 // ctype := w.Header.Get("Content-Type")
... ... @@ -114,23 +106,15 @@ func ProcessHeaders(fn http.HandlerFunc, authEnabled bool) http.HandlerFunc {
114 106  
115 107 err := req.ParseForm()
116 108 if err != nil {
117   - RespondWithHttpError(w, req, http.StatusBadRequest,
118   - []HttpErrorDesc{
119   - {Lang: "en", Desc: templateHttpErr400_EN},
120   - {Lang: "rs", Desc: templateHttpErr400_RS},
121   - })
  109 + RespondWithHttpError400(w, req)
122 110 return
123 111 }
124 112  
125 113 // execute HandlerFunc
126   - fn(w, req)
  114 + handlerFunc(w, req)
127 115 }
128 116 }
129 117  
130   -////
131   -//// NOT FOUND HANDLER
132   -////
133   -
134 118 func NotFoundHandler(w http.ResponseWriter, req *http.Request) {
135 119 RespondWithHttpError(w, req, http.StatusNotFound, []HttpErrorDesc{
136 120 {Lang: "en", Desc: "Not found."},
... ...
... ... @@ -58,11 +58,14 @@ func NewPayload(r *http.Request, table string) Payload {
58 58 var pload Payload
59 59  
60 60 pload.Method = r.Method + " " + r.URL.Path
61   - pload.Params = make(map[string]string, 0)
62   - pload.Lang = loadTranslations(table)
63   - pload.Fields = loadFields(table)
64   - pload.IdField = loadIdField(table)
65   - pload.Correlations = loadCorrelations(table)
  61 +
  62 + if table != "" {
  63 + pload.Params = make(map[string]string, 0)
  64 + pload.Lang = loadTranslations(table)
  65 + pload.Fields = loadFields(table)
  66 + pload.IdField = loadIdField(table)
  67 + pload.Correlations = loadCorrelations(table)
  68 + }
66 69  
67 70 return pload
68 71 }
... ...
... ... @@ -15,11 +15,20 @@ type ListOptions struct {
15 15 }
16 16  
17 17 type ListFilter struct {
18   - ObjectType string `json:"-"`
19   - FiltersField string `json:"filtersField"`
20   - DefaultValues string `json:"defaultValues"`
21   - FiltersType string `json:"filtersType"`
22   - FiltersLabel string `json:"filtersLabel"`
  18 + ObjectType string `json:"-"`
  19 + FiltersField string `json:"filtersField"`
  20 + DefaultValues string `json:"defaultValues"`
  21 + FiltersType string `json:"filtersType"`
  22 + FiltersLabel string `json:"filtersLabel"`
  23 + DropdownConfig Dropdown `json:"dropdownConfig"`
  24 +}
  25 +
  26 +type Dropdown struct {
  27 + ObjectType string `json:"objectType"`
  28 + FiltersField string `json:"filtersField"`
  29 + IdField string `json:"idField"`
  30 + LabelField string `json:"labelField"`
  31 +
23 32 }
24 33  
25 34 type ListGraph struct {
... ... @@ -80,6 +89,27 @@ type ListConfig struct {
80 89 Details ListDetails `json:"details"`
81 90 }
82 91  
  92 +func GetListConfig(db *ora.Ses, objType string) (ListConfig, error) {
  93 + resp := NewDefaultList(objType)
  94 + var err error
  95 +
  96 + err = setListParams(db, &resp, objType)
  97 + resp.Children, err = getListChildren(db, objType)
  98 + resp.Actions, err = getListActions(db, objType)
  99 + resp.Filters, err = getListFilters(db, objType)
  100 + resp.Options, err = getListOptions(db, objType)
  101 + resp.Parent, err = getListParent(db, objType)
  102 + resp.Graphs, err = getListGraph(db, objType)
  103 + resp.Pivots, err = getListPivot(db, objType)
  104 + resp.Details, err = getListDetails(db, objType)
  105 +
  106 + if err != nil {
  107 + return ListConfig{}, err
  108 + }
  109 +
  110 + return resp, nil
  111 +}
  112 +
83 113 func GetListConfigObjectIdField(db *ora.Ses, otype string) string {
84 114 var resp string
85 115 var err error
... ... @@ -137,27 +167,6 @@ func NewDefaultList(objType string) ListConfig {
137 167 return list
138 168 }
139 169  
140   -func GetListConfig(db *ora.Ses, objType string) (ListConfig, error) {
141   - resp := NewDefaultList(objType)
142   - var err error
143   -
144   - err = setListParams(db, &resp, objType)
145   - resp.Children, err = getListChildren(db, objType)
146   - resp.Actions, err = getListActions(db, objType)
147   - resp.Filters, err = getListFilters(db, objType)
148   - resp.Options, err = getListOptions(db, objType)
149   - resp.Parent, err = getListParent(db, objType)
150   - resp.Graphs, err = getListGraph(db, objType)
151   - resp.Pivots, err = getListPivot(db, objType)
152   - resp.Details, err = getListDetails(db, objType)
153   -
154   - if err != nil {
155   - return ListConfig{}, err
156   - }
157   -
158   - return resp, nil
159   -}
160   -
161 170 func setListParams(db *ora.Ses, list *ListConfig, objType string) error {
162 171 var err error
163 172 var stmt *ora.Stmt
... ... @@ -272,13 +281,19 @@ func getListFilters(db *ora.Ses, objType string) ([]ListFilter, error) {
272 281 for _, field := range filtersField {
273 282 filters, _ := getFiltersByFilterField(db, field)
274 283 for _, filter := range filters {
275   - resp = append(resp, ListFilter{
276   - ObjectType: objType,
277   - FiltersField: field,
278   - DefaultValues: filter.DefaultValues,
279   - FiltersLabel: filter.Label,
280   - FiltersType: filter.Type,
281   - })
  284 + var f ListFilter
  285 + f.ObjectType = objType
  286 + f.FiltersField = field
  287 + f.DefaultValues = filter.DefaultValues
  288 + f.FiltersLabel = filter.Label
  289 + f.FiltersType = filter.Type
  290 + if filter.Type == "dropdown" {
  291 + f.DropdownConfig, err = getFilterDropdownConfig(db, field)
  292 + if err != nil {
  293 + return nil, err
  294 + }
  295 + }
  296 + resp = append(resp, f)
282 297 }
283 298 }
284 299  
... ... @@ -351,6 +366,37 @@ func getFiltersByFilterField(db *ora.Ses, filtersField string) ([]TempFilter, er
351 366 return resp, nil
352 367 }
353 368  
  369 +func getFilterDropdownConfig(db *ora.Ses, filtersField string) (Dropdown, error) {
  370 + var resp Dropdown
  371 + var err error
  372 + var stmt *ora.Stmt
  373 + query := `SELECT
  374 + FILTERS_FIELD, OBJECT_TYPE, ID_FIELD, LABEL_FIELD
  375 + FROM LIST_DROPDOWN_FILTER
  376 + WHERE FILTERS_FIELD = '` + filtersField + `'`
  377 +
  378 + stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S)
  379 + if err != nil {
  380 + return resp, err
  381 + }
  382 + defer stmt.Close()
  383 +
  384 + rset, err := stmt.Qry()
  385 + if err != nil {
  386 + return resp, err
  387 + }
  388 + if rset.Next() {
  389 + resp.FiltersField = rset.Row[0].(string)
  390 + resp.ObjectType = rset.Row[1].(string)
  391 + resp.IdField = rset.Row[2].(string)
  392 + resp.LabelField = rset.Row[3].(string)
  393 + }
  394 + if rset.Err != nil {
  395 + return resp, rset.Err
  396 + }
  397 + return resp, nil
  398 +}
  399 +
354 400 func getListGraph(db *ora.Ses, objType string) ([]ListGraph, error) {
355 401 resp := make([]ListGraph, 0)
356 402 var err error
... ...