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"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
_ "github.com/denisenkom/go-mssqldb"
|
_ "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 connectionString = "server=MARCEL\\SQLEXPRESS;user id=;password="
|
||||||
var db *sql.DB
|
var db *sql.DB
|
||||||
@ -53,9 +54,37 @@ func readDatabase(tableName string) (iDatabaseModels, error) {
|
|||||||
case "Diagnozy":
|
case "Diagnozy":
|
||||||
var x diagnoses
|
var x diagnoses
|
||||||
models = x.readModels(rows)
|
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
|
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 {
|
func checkIsDatabaseAlive() error {
|
||||||
err = db.Ping()
|
err = db.Ping()
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DOCTOR:
|
||||||
func (doc doctors) readModels(rows *sql.Rows) iDatabaseModels {
|
func (doc doctors) readModels(rows *sql.Rows) iDatabaseModels {
|
||||||
var models iDatabaseModels
|
var models iDatabaseModels
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
@ -18,14 +19,21 @@ func (doc doctors) readModels(rows *sql.Rows) iDatabaseModels {
|
|||||||
}
|
}
|
||||||
return models
|
return models
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//DO POPRAWY:
|
||||||
func (pat patients) readModels(rows *sql.Rows) iDatabaseModels {
|
func (pat patients) readModels(rows *sql.Rows) iDatabaseModels {
|
||||||
var models iDatabaseModels
|
var models iDatabaseModels
|
||||||
|
var pati patient
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var valueList [7]iHasValue
|
//var valueList [7]iHasValue
|
||||||
rows.Scan(&valueList[0], &valueList[1], &valueList[2], &valueList[3], &valueList[4],
|
pati = patient{}
|
||||||
&valueList[5])
|
rows.Scan(&pati.Pesel, &pati.Name, &pati.Surname, &pati.BirthDate, &pati.PatientState,
|
||||||
var patientSta patientStates
|
&pati.PatientSex)
|
||||||
if valueList[4] == nil {
|
/*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")
|
patientSta = patientStates("NULL")
|
||||||
} else {
|
} else {
|
||||||
patientSta = patientStates(valueList[4].(string))
|
patientSta = patientStates(valueList[4].(string))
|
||||||
@ -38,50 +46,54 @@ func (pat patients) readModels(rows *sql.Rows) iDatabaseModels {
|
|||||||
}
|
}
|
||||||
models = append(models, patient{
|
models = append(models, patient{
|
||||||
valueList[0].(string), valueList[1].(string), valueList[2].(string), valueList[3].(time.Time),
|
valueList[0].(string), valueList[1].(string), valueList[2].(string), valueList[3].(time.Time),
|
||||||
patientSta, se, ""})
|
patientSta, se, ""})*/
|
||||||
}
|
}
|
||||||
return models
|
return models
|
||||||
}
|
}
|
||||||
func (dia diagnoses) readModels(rows *sql.Rows) iDatabaseModels {
|
func (dia diagnoses) readModels(rows *sql.Rows) iDatabaseModels {
|
||||||
var models iDatabaseModels
|
var models iDatabaseModels
|
||||||
|
var diag diagnosis
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var valueList [4]iHasValue
|
diag = diagnosis{}
|
||||||
rows.Scan(&valueList[0], &valueList[1], &valueList[2], &valueList[3])
|
rows.Scan(&diag.IcdSymbol, &diag.Name, &diag.FieldOfSurgery, &diag.Description)
|
||||||
var opis string
|
models = append(models, diag)
|
||||||
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})
|
|
||||||
}
|
}
|
||||||
return models
|
return models
|
||||||
}
|
}
|
||||||
func (ope operations) readModels(rows *sql.Rows) iDatabaseModels {
|
func (ope operations) readModels(rows *sql.Rows) iDatabaseModels {
|
||||||
var models iDatabaseModels
|
var models iDatabaseModels
|
||||||
|
var oper operation
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var valueList [6]iHasValue
|
oper = operation{}
|
||||||
rows.Scan(&valueList[0], &valueList[1], &valueList[2], &valueList[3], &valueList[4],
|
var dummy interface{}
|
||||||
&valueList[5])
|
rows.Scan(&oper.OperationID, &oper.Name, &dummy, &oper.operationType,
|
||||||
|
&oper.cost, &oper.refoundation)
|
||||||
models = append(models, operation{
|
models = append(models, oper)
|
||||||
valueList[0].(int64), valueList[1].(string), "5:00", operationTypes(valueList[3].(string)),
|
|
||||||
valueList[4].([]uint8), valueList[5].(int64)})
|
|
||||||
}
|
}
|
||||||
return models
|
return models
|
||||||
}
|
}
|
||||||
|
|
||||||
/*func (doc admissions) readModels(rows *sql.Rows) iDatabaseModels {
|
func (doc admissions) readModels(rows *sql.Rows) iDatabaseModels {
|
||||||
var models iDatabaseModels
|
var models iDatabaseModels
|
||||||
|
var admiss admission
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var valueList [7]iHasValue
|
admiss = admission{}
|
||||||
rows.Scan(&valueList[0], &valueList[1], &valueList[2], &valueList[3], &valueList[4],
|
rows.Scan(&admiss.AdmissionID, &admiss.AdmissionDate, &admiss.EndDate,
|
||||||
&valueList[5], &valueList[6])
|
&admiss.PatientPesel, &admiss.MainDoctor, &admiss.PlanedOperation,
|
||||||
|
&admiss.RoomNumber, &admiss.IsPlanned)
|
||||||
|
|
||||||
models = append(models, admission{
|
models = append(models, admiss)
|
||||||
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))})
|
|
||||||
}
|
}
|
||||||
return models
|
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"
|
"fmt"
|
||||||
"html"
|
"html"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
@ -18,6 +19,33 @@ func getAll(w http.ResponseWriter, r *http.Request) {
|
|||||||
patientsList, _ := readDatabase(table)
|
patientsList, _ := readDatabase(table)
|
||||||
json.NewEncoder(w).Encode(patientsList)
|
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) {
|
func getIndex(w http.ResponseWriter, r *http.Request) {
|
||||||
args := mux.Vars(r)
|
args := mux.Vars(r)
|
||||||
pk := args["primaryKey"]
|
pk := args["primaryKey"]
|
||||||
|
3
index.go
3
index.go
@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Zrobione ostatnio:
|
// Zrobione ostatnio:
|
||||||
@ -23,7 +22,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getXxx() patient {
|
func getXxx() patient {
|
||||||
pat := patient{"kss", "dasda", "asdasd a", time.Now(), patientStates(critical), sex(m), "xxx@yyy.zz"}
|
pat := patient{}
|
||||||
|
|
||||||
return pat
|
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{"Index", "/", index},
|
||||||
route{"GetAll", "/getfromdb/{tableName}", getAll},
|
route{"GetAll", "/getfromdb/{tableName}", getAll},
|
||||||
route{"GetOne", "/getfromdb/{primaryKey}", getIndex},
|
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 {
|
func newRouter() *mux.Router {
|
||||||
|
Loading…
Reference in New Issue
Block a user