59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"html"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
func index(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
|
|
}
|
|
func getAll(w http.ResponseWriter, r *http.Request) {
|
|
args := mux.Vars(r)
|
|
table := args["tableName"]
|
|
patientsList, _ := readDatabase(table)
|
|
json.NewEncoder(w).Encode(patientsList)
|
|
}
|
|
func update(w http.ResponseWriter, r *http.Request) {
|
|
args := mux.Vars(r)
|
|
tableName := args["tableName"]
|
|
primaryKey := args["primaryKey"]
|
|
primaryKeyName := args["primaryKeyName"]
|
|
fieldToUpdate := args["fieldToUpdate"]
|
|
valueToInsert := args["valueToInsert"]
|
|
|
|
rowsAffected :=
|
|
updateRecord(tableName, primaryKey, primaryKeyName, fieldToUpdate, valueToInsert)
|
|
fmt.Fprint(w, rowsAffected)
|
|
}
|
|
|
|
func insert(w http.ResponseWriter, r *http.Request) {
|
|
args := mux.Vars(r)
|
|
// TODO: Dodac walidacje primary key
|
|
tsqlCommand := "INSERT INTO " + args["tableName"] + " VALUES ('" + args["primaryKey"]
|
|
max, _ := strconv.Atoi(args["count"])
|
|
for i := 1; i < max; i++ {
|
|
key := "val" + strconv.Itoa(i)
|
|
tsqlCommand += "', '" + args[key]
|
|
}
|
|
tsqlCommand += "')"
|
|
rowsAffected := insertRecord(tsqlCommand)
|
|
fmt.Fprint(w, rowsAffected)
|
|
}
|
|
func columnNames(w http.ResponseWriter, r *http.Request) {
|
|
args := mux.Vars(r)
|
|
table := args["tableName"]
|
|
colNames, _ := getColumnNames(table)
|
|
fmt.Fprint(w, colNames)
|
|
}
|
|
func getIndex(w http.ResponseWriter, r *http.Request) {
|
|
args := mux.Vars(r)
|
|
pk := args["primaryKey"]
|
|
fmt.Fprintf(w, "Tutaj bedzie wynik dla PK = %s", pk)
|
|
}
|