2019-02-22 03:23:03 +00:00
|
|
|
package api
|
|
|
|
|
|
2019-02-28 04:11:35 +00:00
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2019-03-10 10:20:00 +00:00
|
|
|
"gitlab.com/mobtools/internal-tools-server/models"
|
|
|
|
|
"gitlab.com/mobtools/internal-tools-server/services"
|
2019-02-28 04:11:35 +00:00
|
|
|
)
|
|
|
|
|
|
2019-02-22 03:23:03 +00:00
|
|
|
/*
|
|
|
|
|
This file contains the APIs for the client to invoke in order to fetch data or perform an action
|
|
|
|
|
on the middleware server
|
|
|
|
|
*/
|
2019-02-28 04:11:35 +00:00
|
|
|
|
|
|
|
|
// PostQuery executes a custom sql query on the client database
|
2019-03-16 10:17:47 +00:00
|
|
|
func PostQuery(w http.ResponseWriter, r *http.Request) {
|
2019-02-28 04:11:35 +00:00
|
|
|
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
|
2019-03-16 10:17:47 +00:00
|
|
|
func CreateQuery(w http.ResponseWriter, r *http.Request) {
|
2019-02-28 04:11:35 +00:00
|
|
|
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
|
2019-03-16 10:17:47 +00:00
|
|
|
func UpdateQuery(w http.ResponseWriter, r *http.Request) {
|
2019-02-28 04:11:35 +00:00
|
|
|
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)
|
|
|
|
|
}
|