PromucFlow_constructor/app/server/api/query.go
Arpit Mohan 1133b53437 Adding Google authentication via Goth. All endpoints can now be authenticated.
Other changes include:
* Also removing httprouter mux in favour of gorilla for being more mature and having more integrations and resources available for debugging.
* Adding http middlewares for logging req processing time and handling authentication.

TODO: Need to add context in the middleware as well. Will be useful for logging and debugging.
2019-03-16 15:47:47 +05:30

83 lines
2.0 KiB
Go

package api
import (
"encoding/json"
"fmt"
"net/http"
"gitlab.com/mobtools/internal-tools-server/models"
"gitlab.com/mobtools/internal-tools-server/services"
)
/*
This file contains the APIs for the client to invoke in order to fetch data or perform an action
on the middleware server
*/
// PostQuery executes a custom sql query on the client database
func PostQuery(w http.ResponseWriter, r *http.Request) {
queryBody := models.ExecQuery{}
err := json.NewDecoder(r.Body).Decode(&queryBody)
if err != nil {
HandleAPIError(w, r, err)
return
}
var mapArray []map[string]interface{}
mapArray, err = services.ExecuteQuery(queryBody)
if err != nil {
HandleAPIError(w, r, err)
return
}
// Write content-type, statuscode, payload
mapJSON, _ := json.Marshal(mapArray)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
fmt.Fprintf(w, "%s", mapJSON)
}
// CreateQuery creates a new query for the user in the table
func CreateQuery(w http.ResponseWriter, r *http.Request) {
queryBody := models.Query{}
err := json.NewDecoder(r.Body).Decode(&queryBody)
if err != nil {
HandleAPIError(w, r, err)
return
}
queryBody, err = services.CreateQuery(queryBody)
if err != nil {
HandleAPIError(w, r, err)
return
}
// Write content-type, statuscode, payload
queryJSON, _ := json.Marshal(queryBody)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
fmt.Fprintf(w, "%s", queryJSON)
}
// UpdateQuery updates a given query in the database for a given account
func UpdateQuery(w http.ResponseWriter, r *http.Request) {
queryBody := models.Query{}
err := json.NewDecoder(r.Body).Decode(&queryBody)
if err != nil {
HandleAPIError(w, r, err)
return
}
queryBody, err = services.UpdateQuery(queryBody)
if err != nil {
HandleAPIError(w, r, err)
return
}
// Write content-type, statuscode, payload
queryJSON, _ := json.Marshal(queryBody)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
fmt.Fprintf(w, "%s", queryJSON)
}