admissionServer/handlers.go

113 lines
3.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"]
tsql := "SELECT * FROM " + table
patientsList, _ := readDatabase(tsql, table)
x, _ := json.Marshal(patientsList)
fmt.Fprintf(w, string(x), html.EscapeString(r.URL.Path))
fmt.Println("POBRANO DANE" + table)
}
func sort(w http.ResponseWriter, r *http.Request) {
args := mux.Vars(r)
table := args["tableName"]
sortby := args["sortBy"]
sortType := args["type"]
tsql := "SELECT * FROM " + table
tsql += " ORDER BY " + sortby + " " + sortType
patientsList, _ := readDatabase(tsql, table)
json.NewEncoder(w).Encode(patientsList)
fmt.Println("POBRANO DANE" + table + " orderby " + sortby)
}
func search(w http.ResponseWriter, r *http.Request) {
args := mux.Vars(r)
table := args["tableName"]
sortby := args["sortBy"]
sortType := args["type"]
searchIn := args["columnName"]
searchVal := args["searchedValue"]
tsql := "SELECT * FROM " + table
tsql += " WHERE " + searchIn + " = '" + searchVal + "'"
tsql += " ORDER BY " + sortby + " " + sortType
patientsList, _ := readDatabase(tsql, table)
json.NewEncoder(w).Encode(patientsList)
fmt.Println("POBRANO DANE" + table + " orderby " + sortby)
}
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)
fmt.Println("Zaktualizowano " + rowsAffected + " z " + tableName)
}
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)
valueToInsert := args[key]
if valueToInsert == "null" {
valueToInsert = "NULL"
} else {
valueToInsert = "'" + valueToInsert + "'"
}
tsqlCommand += ", " + valueToInsert
}
tsqlCommand += ")"
rowsAffected := insertRecord(tsqlCommand)
fmt.Fprint(w, rowsAffected)
fmt.Println("Dodano " + rowsAffected + " " + tsqlCommand)
}
func delete(w http.ResponseWriter, r *http.Request) {
args := mux.Vars(r)
tableName := args["tableName"]
primaryKey := args["primaryKey"]
primaryKeyName := args["primaryKeyName"]
tsqlCommand := "DELETE " + tableName + " WHERE " + primaryKeyName + " = '" + primaryKey + "'"
rowsAffected := deleteRecord(tsqlCommand)
fmt.Fprint(w, rowsAffected)
fmt.Println("Usunięto " + rowsAffected + " " + tsqlCommand)
}
func columnNames(w http.ResponseWriter, r *http.Request) {
args := mux.Vars(r)
table := args["tableName"]
colNames, _ := getColumnNames(table)
json.NewEncoder(w).Encode(colNames)
fmt.Println("POBRANO COLUMN NAMES " + table)
}
func columnTypes(w http.ResponseWriter, r *http.Request) {
args := mux.Vars(r)
table := args["tableName"]
colNames, _ := getColumnTypes(table)
json.NewEncoder(w).Encode(colNames)
fmt.Println("POBRANO COLUMN TYPES " + table)
}
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)
}