diff --git a/README.md b/README.md index cd207eb..9cabd73 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ TODO: * http utility: - 1. add parameters to the ProcessHeaders to enable/disable token/role-access-rights checks - 2. check for Content-Type header, if clients expects something other than JSON respond with appropriate HTTP code +1. add parameters to the ProcessHeaders to enable/disable token/role-access-rights checks +2. check for Content-Type header, if clients expects something other than JSON respond with appropriate HTTP code diff --git a/http_utility.go b/http_utility.go index f6e14d3..f4ba826 100644 --- a/http_utility.go +++ b/http_utility.go @@ -5,8 +5,8 @@ import ( "encoding/json" ) -var _apiVersion = "" -var _authEndPoint = "" +var _apiVersion = "/api/v1" +var _authEndPoint = "/token" func SetApiVersion(ver string) string { _apiVersion = ver @@ -17,10 +17,6 @@ func SetAuthEndpoint(ep string) { _authEndPoint = ep } -//// -//// ERROR UTILITY -//// - const templateHttpErr500_EN = "An internal server error has occurred." const templateHttpErr500_RS = "Došlo je do greške na serveru." const templateHttpErr400_EN = "Bad request: invalid request body." @@ -63,13 +59,9 @@ func RespondWithHttpError500(w http.ResponseWriter, req *http.Request) { }) } -//// -//// HANDLER FUNC WRAPPER -//// - -//TODO: Add parameters to enable/disable token and roles authorization checks +//TODO: Add parameters to enable/disable roles authorization checks // Sets common headers and checks for token validity. -func ProcessHeaders(fn http.HandlerFunc, authEnabled bool) http.HandlerFunc { +func ProcessHeaders(handlerFunc http.HandlerFunc, authEnabled bool) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { // @TODO: check Content-type header (must be application/json) // ctype := w.Header.Get("Content-Type") @@ -114,23 +106,15 @@ func ProcessHeaders(fn http.HandlerFunc, authEnabled bool) http.HandlerFunc { err := req.ParseForm() if err != nil { - RespondWithHttpError(w, req, http.StatusBadRequest, - []HttpErrorDesc{ - {Lang: "en", Desc: templateHttpErr400_EN}, - {Lang: "rs", Desc: templateHttpErr400_RS}, - }) + RespondWithHttpError400(w, req) return } // execute HandlerFunc - fn(w, req) + handlerFunc(w, req) } } -//// -//// NOT FOUND HANDLER -//// - func NotFoundHandler(w http.ResponseWriter, req *http.Request) { RespondWithHttpError(w, req, http.StatusNotFound, []HttpErrorDesc{ {Lang: "en", Desc: "Not found."}, diff --git a/json_utility.go b/json_utility.go index 6372ce6..8de8565 100644 --- a/json_utility.go +++ b/json_utility.go @@ -58,11 +58,14 @@ func NewPayload(r *http.Request, table string) Payload { var pload Payload pload.Method = r.Method + " " + r.URL.Path - pload.Params = make(map[string]string, 0) - pload.Lang = loadTranslations(table) - pload.Fields = loadFields(table) - pload.IdField = loadIdField(table) - pload.Correlations = loadCorrelations(table) + + if table != "" { + pload.Params = make(map[string]string, 0) + pload.Lang = loadTranslations(table) + pload.Fields = loadFields(table) + pload.IdField = loadIdField(table) + pload.Correlations = loadCorrelations(table) + } return pload } diff --git a/list_config.go b/list_config.go index b75a24f..e105379 100644 --- a/list_config.go +++ b/list_config.go @@ -15,11 +15,20 @@ type ListOptions struct { } type ListFilter struct { - ObjectType string `json:"-"` - FiltersField string `json:"filtersField"` - DefaultValues string `json:"defaultValues"` - FiltersType string `json:"filtersType"` - FiltersLabel string `json:"filtersLabel"` + ObjectType string `json:"-"` + FiltersField string `json:"filtersField"` + DefaultValues string `json:"defaultValues"` + FiltersType string `json:"filtersType"` + FiltersLabel string `json:"filtersLabel"` + DropdownConfig Dropdown `json:"dropdownConfig"` +} + +type Dropdown struct { + ObjectType string `json:"objectType"` + FiltersField string `json:"filtersField"` + IdField string `json:"idField"` + LabelField string `json:"labelField"` + } type ListGraph struct { @@ -80,6 +89,27 @@ type ListConfig struct { Details ListDetails `json:"details"` } +func GetListConfig(db *ora.Ses, objType string) (ListConfig, error) { + resp := NewDefaultList(objType) + var err error + + err = setListParams(db, &resp, objType) + resp.Children, err = getListChildren(db, objType) + resp.Actions, err = getListActions(db, objType) + resp.Filters, err = getListFilters(db, objType) + resp.Options, err = getListOptions(db, objType) + resp.Parent, err = getListParent(db, objType) + resp.Graphs, err = getListGraph(db, objType) + resp.Pivots, err = getListPivot(db, objType) + resp.Details, err = getListDetails(db, objType) + + if err != nil { + return ListConfig{}, err + } + + return resp, nil +} + func GetListConfigObjectIdField(db *ora.Ses, otype string) string { var resp string var err error @@ -137,27 +167,6 @@ func NewDefaultList(objType string) ListConfig { return list } -func GetListConfig(db *ora.Ses, objType string) (ListConfig, error) { - resp := NewDefaultList(objType) - var err error - - err = setListParams(db, &resp, objType) - resp.Children, err = getListChildren(db, objType) - resp.Actions, err = getListActions(db, objType) - resp.Filters, err = getListFilters(db, objType) - resp.Options, err = getListOptions(db, objType) - resp.Parent, err = getListParent(db, objType) - resp.Graphs, err = getListGraph(db, objType) - resp.Pivots, err = getListPivot(db, objType) - resp.Details, err = getListDetails(db, objType) - - if err != nil { - return ListConfig{}, err - } - - return resp, nil -} - func setListParams(db *ora.Ses, list *ListConfig, objType string) error { var err error var stmt *ora.Stmt @@ -272,13 +281,19 @@ func getListFilters(db *ora.Ses, objType string) ([]ListFilter, error) { for _, field := range filtersField { filters, _ := getFiltersByFilterField(db, field) for _, filter := range filters { - resp = append(resp, ListFilter{ - ObjectType: objType, - FiltersField: field, - DefaultValues: filter.DefaultValues, - FiltersLabel: filter.Label, - FiltersType: filter.Type, - }) + var f ListFilter + f.ObjectType = objType + f.FiltersField = field + f.DefaultValues = filter.DefaultValues + f.FiltersLabel = filter.Label + f.FiltersType = filter.Type + if filter.Type == "dropdown" { + f.DropdownConfig, err = getFilterDropdownConfig(db, field) + if err != nil { + return nil, err + } + } + resp = append(resp, f) } } @@ -351,6 +366,37 @@ func getFiltersByFilterField(db *ora.Ses, filtersField string) ([]TempFilter, er return resp, nil } +func getFilterDropdownConfig(db *ora.Ses, filtersField string) (Dropdown, error) { + var resp Dropdown + var err error + var stmt *ora.Stmt + query := `SELECT + FILTERS_FIELD, OBJECT_TYPE, ID_FIELD, LABEL_FIELD + FROM LIST_DROPDOWN_FILTER + WHERE FILTERS_FIELD = '` + filtersField + `'` + + stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S) + if err != nil { + return resp, err + } + defer stmt.Close() + + rset, err := stmt.Qry() + if err != nil { + return resp, err + } + if rset.Next() { + resp.FiltersField = rset.Row[0].(string) + resp.ObjectType = rset.Row[1].(string) + resp.IdField = rset.Row[2].(string) + resp.LabelField = rset.Row[3].(string) + } + if rset.Err != nil { + return resp, rset.Err + } + return resp, nil +} + func getListGraph(db *ora.Ses, objType string) ([]ListGraph, error) { resp := make([]ListGraph, 0) var err error