Dodawanie dodatkowych funkcji obsługujących bazę danych.
- dodano UpdateRecord i wstępnie InsertRecord + powiązane handlery i potrzebne dodatkowe funkcje. - Dodano metody do modeli zwracające primarykey
This commit is contained in:
parent
fa9d4c0fc6
commit
3977c3e9fe
@ -5,11 +5,12 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
_ "github.com/denisenkom/go-mssqldb"
|
||||
)
|
||||
|
||||
var connectionString = "server=192.168.1.3;Port=62262;database=DB_s439397;trusted_connection=yes;encrypt=disable"
|
||||
var connectionString = "server=192.168.43.241;Port=62262;database=DB_s439397;trusted_connection=yes;encrypt=disable"
|
||||
|
||||
//var connectionString = "server=MARCEL\\SQLEXPRESS;user id=;password="
|
||||
var db *sql.DB
|
||||
@ -53,9 +54,37 @@ func readDatabase(tableName string) (iDatabaseModels, error) {
|
||||
case "Diagnozy":
|
||||
var x diagnoses
|
||||
models = x.readModels(rows)
|
||||
case "Przyjecia":
|
||||
var x admissions
|
||||
models = x.readModels(rows)
|
||||
case "Sale":
|
||||
var x rooms
|
||||
models = x.readModels(rows)
|
||||
}
|
||||
return models, nil
|
||||
}
|
||||
func updateRecord(tableName string, primaryKey string,
|
||||
primaryKeyName string, fieldToUpdate string, valueToInsert string) string {
|
||||
ctx := context.Background()
|
||||
checkIsDatabaseAlive()
|
||||
tsql := "UPDATE " + tableName + " SET " + fieldToUpdate + " = '" + valueToInsert +
|
||||
"' WHERE " + primaryKeyName + " = '" + primaryKey + "'"
|
||||
result, _ := db.ExecContext(ctx, tsql)
|
||||
toReturn, _ := result.RowsAffected()
|
||||
return (strconv.FormatInt(toReturn, 10))
|
||||
}
|
||||
|
||||
func insertRecord(command string) string {
|
||||
ctx := context.Background()
|
||||
checkIsDatabaseAlive()
|
||||
/*tsql := "INSERT " + tableName + " VALUES '" + primaryKey + "'"
|
||||
for i := 0; i<numberOfValues; i++ {
|
||||
tsql = tsql + ", '" + valuesList[i] + "'"
|
||||
}*/
|
||||
result, _ := db.ExecContext(ctx, command)
|
||||
toReturn, _ := result.RowsAffected()
|
||||
return (strconv.FormatInt(toReturn, 10))
|
||||
}
|
||||
|
||||
func checkIsDatabaseAlive() error {
|
||||
err = db.Ping()
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// DOCTOR:
|
||||
func (doc doctors) readModels(rows *sql.Rows) iDatabaseModels {
|
||||
var models iDatabaseModels
|
||||
for rows.Next() {
|
||||
@ -18,14 +19,21 @@ func (doc doctors) readModels(rows *sql.Rows) iDatabaseModels {
|
||||
}
|
||||
return models
|
||||
}
|
||||
|
||||
//DO POPRAWY:
|
||||
func (pat patients) readModels(rows *sql.Rows) iDatabaseModels {
|
||||
var models iDatabaseModels
|
||||
var pati patient
|
||||
for rows.Next() {
|
||||
var valueList [7]iHasValue
|
||||
rows.Scan(&valueList[0], &valueList[1], &valueList[2], &valueList[3], &valueList[4],
|
||||
&valueList[5])
|
||||
var patientSta patientStates
|
||||
if valueList[4] == nil {
|
||||
//var valueList [7]iHasValue
|
||||
pati = patient{}
|
||||
rows.Scan(&pati.Pesel, &pati.Name, &pati.Surname, &pati.BirthDate, &pati.PatientState,
|
||||
&pati.PatientSex)
|
||||
/*rows.Scan(&valueList[0], &valueList[1], &valueList[2], &valueList[3], &valueList[4],
|
||||
&valueList[5])*/
|
||||
//var patientSta patientStates
|
||||
models = append(models, pati)
|
||||
/*if valueList[4] == nil {
|
||||
patientSta = patientStates("NULL")
|
||||
} else {
|
||||
patientSta = patientStates(valueList[4].(string))
|
||||
@ -38,50 +46,54 @@ func (pat patients) readModels(rows *sql.Rows) iDatabaseModels {
|
||||
}
|
||||
models = append(models, patient{
|
||||
valueList[0].(string), valueList[1].(string), valueList[2].(string), valueList[3].(time.Time),
|
||||
patientSta, se, ""})
|
||||
patientSta, se, ""})*/
|
||||
}
|
||||
return models
|
||||
}
|
||||
func (dia diagnoses) readModels(rows *sql.Rows) iDatabaseModels {
|
||||
var models iDatabaseModels
|
||||
var diag diagnosis
|
||||
for rows.Next() {
|
||||
var valueList [4]iHasValue
|
||||
rows.Scan(&valueList[0], &valueList[1], &valueList[2], &valueList[3])
|
||||
var opis string
|
||||
if valueList[3] == nil {
|
||||
opis = "Brak"
|
||||
} else {
|
||||
opis = valueList[3].(string)
|
||||
}
|
||||
models = append(models, diagnosis{
|
||||
valueList[0].(string), valueList[1].(string), surgeryField(valueList[2].(string)), opis})
|
||||
diag = diagnosis{}
|
||||
rows.Scan(&diag.IcdSymbol, &diag.Name, &diag.FieldOfSurgery, &diag.Description)
|
||||
models = append(models, diag)
|
||||
}
|
||||
return models
|
||||
}
|
||||
func (ope operations) readModels(rows *sql.Rows) iDatabaseModels {
|
||||
var models iDatabaseModels
|
||||
var oper operation
|
||||
for rows.Next() {
|
||||
var valueList [6]iHasValue
|
||||
rows.Scan(&valueList[0], &valueList[1], &valueList[2], &valueList[3], &valueList[4],
|
||||
&valueList[5])
|
||||
|
||||
models = append(models, operation{
|
||||
valueList[0].(int64), valueList[1].(string), "5:00", operationTypes(valueList[3].(string)),
|
||||
valueList[4].([]uint8), valueList[5].(int64)})
|
||||
oper = operation{}
|
||||
var dummy interface{}
|
||||
rows.Scan(&oper.OperationID, &oper.Name, &dummy, &oper.operationType,
|
||||
&oper.cost, &oper.refoundation)
|
||||
models = append(models, oper)
|
||||
}
|
||||
return models
|
||||
}
|
||||
|
||||
/*func (doc admissions) readModels(rows *sql.Rows) iDatabaseModels {
|
||||
func (doc admissions) readModels(rows *sql.Rows) iDatabaseModels {
|
||||
var models iDatabaseModels
|
||||
var admiss admission
|
||||
for rows.Next() {
|
||||
var valueList [7]iHasValue
|
||||
rows.Scan(&valueList[0], &valueList[1], &valueList[2], &valueList[3], &valueList[4],
|
||||
&valueList[5], &valueList[6])
|
||||
admiss = admission{}
|
||||
rows.Scan(&admiss.AdmissionID, &admiss.AdmissionDate, &admiss.EndDate,
|
||||
&admiss.PatientPesel, &admiss.MainDoctor, &admiss.PlanedOperation,
|
||||
&admiss.RoomNumber, &admiss.IsPlanned)
|
||||
|
||||
models = append(models, admission{
|
||||
valueList[0].(int64), valueList[1].(string), valueList[2].(string), academicDegree(valueList[3].(string)),
|
||||
specialization(valueList[4].(string)), valueList[5].(time.Time), jobPosition(valueList[6].(string))})
|
||||
models = append(models, admiss)
|
||||
}
|
||||
return models
|
||||
}*/
|
||||
}
|
||||
|
||||
func (ro rooms) readModels(rows *sql.Rows) iDatabaseModels {
|
||||
var models iDatabaseModels
|
||||
var rom room
|
||||
for rows.Next() {
|
||||
rom = room{}
|
||||
rows.Scan(&rom.RoomNumber, &rom.NumberOfBeds, &rom.IncrasedCare)
|
||||
models = append(models, rom)
|
||||
}
|
||||
return models
|
||||
}
|
28
handlers.go
28
handlers.go
@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"html"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
@ -18,6 +19,33 @@ func getAll(w http.ResponseWriter, r *http.Request) {
|
||||
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)
|
||||
}
|
||||
|
||||
//DO POPRAWY
|
||||
func insert(w http.ResponseWriter, r *http.Request) {
|
||||
args := mux.Vars(r)
|
||||
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 getIndex(w http.ResponseWriter, r *http.Request) {
|
||||
args := mux.Vars(r)
|
||||
pk := args["primaryKey"]
|
||||
|
3
index.go
3
index.go
@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Zrobione ostatnio:
|
||||
@ -23,7 +22,7 @@ func main() {
|
||||
}
|
||||
|
||||
func getXxx() patient {
|
||||
pat := patient{"kss", "dasda", "asdasd a", time.Now(), patientStates(critical), sex(m), "xxx@yyy.zz"}
|
||||
pat := patient{}
|
||||
|
||||
return pat
|
||||
}
|
||||
|
52
modelMethods.go
Normal file
52
modelMethods.go
Normal file
@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import "strconv"
|
||||
|
||||
func (doc doctor) getPrimaryKey() string {
|
||||
var value interface{}
|
||||
value = doc.DoctorID
|
||||
return strconv.Itoa(value.(int))
|
||||
}
|
||||
func (doc doctor) getPrimaryKeyName() string {
|
||||
return "Id_Lekarza"
|
||||
}
|
||||
func (ope operation) getPrimaryKey() string {
|
||||
var value interface{}
|
||||
value = ope.OperationID
|
||||
return strconv.Itoa(value.(int))
|
||||
}
|
||||
func (ope operation) getPrimaryKeyName() string {
|
||||
return "Id_Operacji"
|
||||
}
|
||||
func (roo room) getPrimaryKey() string {
|
||||
var value interface{}
|
||||
value = roo.RoomNumber
|
||||
return strconv.Itoa(value.(int))
|
||||
}
|
||||
func (roo room) getPrimaryKeyName() string {
|
||||
return "Nr_sali"
|
||||
}
|
||||
func (diag diagnosis) getPrimaryKey() string {
|
||||
var value interface{}
|
||||
value = diag.IcdSymbol
|
||||
return value.(string)
|
||||
}
|
||||
func (diag diagnosis) getPrimaryKeyName() string {
|
||||
return "Symbol_ICD"
|
||||
}
|
||||
func (pat patient) getPrimaryKey() string {
|
||||
var value interface{}
|
||||
value = pat.Pesel
|
||||
return value.(string)
|
||||
}
|
||||
func (pat patient) getPrimaryKeyName() string {
|
||||
return "PESEL"
|
||||
}
|
||||
func (adm admission) getPrimaryKey() string {
|
||||
var value interface{}
|
||||
value = adm.AdmissionID
|
||||
return strconv.Itoa(value.(int))
|
||||
}
|
||||
func (adm admission) getPrimaryKeyName() string {
|
||||
return "PESEL"
|
||||
}
|
@ -18,6 +18,11 @@ var registredRoutes = routes{
|
||||
route{"Index", "/", index},
|
||||
route{"GetAll", "/getfromdb/{tableName}", getAll},
|
||||
route{"GetOne", "/getfromdb/{primaryKey}", getIndex},
|
||||
route{"UpdateRecord",
|
||||
"/updaterec/{tableName}/pk/{primaryKey}/pkn/{primaryKeyName}/ftu/{fieldToUpdate}/vti/{valueToInsert}",
|
||||
update},
|
||||
route{"InsertRecord", "/insertrec/{tableName}/nr/{count}/pk/{primaryKey}/{val1}/{val2}/{val3}/{val4}/{val5}/{val6}/{val7}",
|
||||
insert},
|
||||
}
|
||||
|
||||
func newRouter() *mux.Router {
|
||||
|
Loading…
Reference in New Issue
Block a user