Dodano metodę odczytującą dane z wskazanej tabeli w bazie.

Dodano polimorficzny interfejs aby odpowiedzialność za pobranie danych leżała po stronie elementu zależnego, a nie nadrzędnego.
Dodano metody rozszerzające do kolekcji modeli, które umożliwiają łatwe tworzenie modeli i dodawanie ich do kolekcji.
Pobieranie modeli jeszcze do poprawki.
This commit is contained in:
Marcel 2018-12-08 18:01:33 +01:00
parent 34df1ce682
commit fa9d4c0fc6
8 changed files with 192 additions and 60 deletions

BIN
admissionServer.exe Normal file

Binary file not shown.

View File

@ -1,6 +1,9 @@
package main package main
import "time" import (
"database/sql"
"time"
)
type patient struct { type patient struct {
Pesel string Pesel string
@ -20,7 +23,7 @@ type diagnosis struct {
} }
type doctor struct { type doctor struct {
DoctorID int DoctorID int64
Name string Name string
Surname string Surname string
AcademicDegree academicDegree AcademicDegree academicDegree
@ -30,12 +33,12 @@ type doctor struct {
} }
type operation struct { type operation struct {
OperationID int OperationID int64
Name string Name string
averageTime time.Time averageTime string
operationType operationTypes operationType operationTypes
cost float32 cost []uint8
refoundation int refoundation int64
} }
type room struct { type room struct {
@ -66,3 +69,9 @@ type diagnoses []diagnosis
type admissions []admission type admissions []admission
type operations []operation type operations []operation
type rooms []room type rooms []room
type iDatabaseModel interface{}
type iDatabaseModels []iDatabaseModel
type iStoreModels interface {
readModels(rows *sql.Rows) iDatabaseModels
}

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"context"
"database/sql" "database/sql"
"fmt" "fmt"
"log" "log"
@ -26,5 +27,41 @@ func testConnection() {
log.Fatal(err.Error()) log.Fatal(err.Error())
} }
fmt.Printf("Connected!\n") fmt.Printf("Connected!\n")
fmt.Println("..I..") }
type iHasValue interface{}
type values []iHasValue
func readDatabase(tableName string) (iDatabaseModels, error) {
ctx := context.Background()
checkIsDatabaseAlive()
tsql := "SELECT * FROM " + tableName
rows, _ := db.QueryContext(ctx, tsql)
defer rows.Close()
var models iDatabaseModels
models = nil
switch tableName {
case "Lekarze":
var x doctors
models = x.readModels(rows)
case "Pacjenci":
var x patients
models = x.readModels(rows)
case "Operacje":
var x operations
models = x.readModels(rows)
case "Diagnozy":
var x diagnoses
models = x.readModels(rows)
}
return models, nil
}
func checkIsDatabaseAlive() error {
err = db.Ping()
if err != nil {
log.Fatal(err.Error())
}
return nil
} }

View File

@ -1,12 +1,12 @@
package main package main
type patientStates string type patientStates string
type sex int type sex string
type surgeryField int type surgeryField string
type academicDegree int type academicDegree string
type specialization int type specialization string
type jobPosition int type jobPosition string
type operationTypes int type operationTypes string
const ( const (
critical patientStates = "KRYTYCZNY" //KRYTYCZNY critical patientStates = "KRYTYCZNY" //KRYTYCZNY
@ -17,57 +17,57 @@ const (
) )
const ( const (
k sex = 0 k sex = "K"
m sex = 1 m sex = "M"
) )
const ( const (
ogolna surgeryField = 0 ogolna surgeryField = "Ogólna"
klatkiPiersiowej surgeryField = 1 klatkiPiersiowej surgeryField = "Klatki piersiowej"
sercowoNaczyniowa surgeryField = 2 sercowoNaczyniowa surgeryField = "Sercowo - naczyniowa"
ukladuNerwowego surgeryField = 3 ukladuNerwowego surgeryField = "Układu nerwowego"
urologia surgeryField = 4 urologia surgeryField = "Urologia"
szczekowoTwarzowa surgeryField = 5 szczekowoTwarzowa surgeryField = "Szczękowo - twarzowa"
urazowa surgeryField = 6 urazowa surgeryField = "Urazowa"
inne surgeryField = 7 inne surgeryField = "NULL"
) )
const ( const (
lekMed academicDegree = 0 lekMed academicDegree = "lek. med."
lekRez academicDegree = 1 lekRez academicDegree = "lek. rez."
lekSpec academicDegree = 2 lekSpec academicDegree = "lek. spec."
dr academicDegree = 3 dr academicDegree = "dr"
drHab academicDegree = 4 drHab academicDegree = "dr hab."
prof academicDegree = 5 prof academicDegree = "prof."
) )
const ( const (
chirOgolna specialization = 0 chirOgolna specialization = "Chirurgia ogólna"
chirKlatkiPiers specialization = 1 chirKlatkiPiers specialization = "Chirurgia klatki piersiowej"
chirSercowoNaczyn specialization = 2 chirSercowoNaczyn specialization = "Chirurgia sercowo - naczyniowa"
chirUkNerwowego specialization = 3 chirUkNerwowego specialization = "Chirurgia układu nerwowego"
chirUrologiczna specialization = 4 chirUrologiczna specialization = "Urologia"
chirSzczekTwarz specialization = 5 chirSzczekTwarz specialization = "Chirurgia szczękowo - twarzowa"
chirUrazowa specialization = 6 chirUrazowa specialization = "Chirurgia urazowa"
) )
const ( const (
generalPracticioner jobPosition = 0 generalPracticioner jobPosition = "Lekarz ogólny"
mainDoctor jobPosition = 1 mainDoctor jobPosition = "Lekarz prowadzący"
viceManager jobPosition = 2 viceManager jobPosition = "Zastępca kierownika"
manager jobPosition = 3 manager jobPosition = "Kierownik"
hospitalHead jobPosition = 4 hospitalHead jobPosition = "Ordynator"
) )
const ( const (
szycie operationTypes = 0 szycie operationTypes = "Szycie"
resekcja operationTypes = 1 resekcja operationTypes = "Resekcja"
amputacja operationTypes = 2 amputacja operationTypes = "Amputacja"
drenaz operationTypes = 3 drenaz operationTypes = "Drenaż"
nastawienieZlaman operationTypes = 4 nastawienieZlaman operationTypes = "Nastawianie złamań"
operacjaKlatkiPiers operationTypes = 5 operacjaKlatkiPiers operationTypes = "Ingerencja wewnątrz klatki piersiowej"
operacjaSerca operationTypes = 6 operacjaSerca operationTypes = "Operacja serca"
operacjaUkNerwow operationTypes = 7 operacjaUkNerwow operationTypes = "Operacja układu nerwowego"
przeszczep operationTypes = 8 przeszczep operationTypes = "Przeszczep"
inneOperacje operationTypes = 9 inneOperacje operationTypes = "Inne"
) )

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"html" "html"
"net/http" "net/http"
"time"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@ -14,10 +13,9 @@ func index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
} }
func getAll(w http.ResponseWriter, r *http.Request) { func getAll(w http.ResponseWriter, r *http.Request) {
patientsList := patients{ args := mux.Vars(r)
patient{"kss", "dasda", "asdasd a", time.Now(), patientStates(critical), sex(m), "xxx@yyy.zz"}, table := args["tableName"]
patient{"00112245789", "Adam", "Marcel", time.Now(), patientStates(stable), sex(k), "xxxx@yyy.zz"}, patientsList, _ := readDatabase(table)
}
json.NewEncoder(w).Encode(patientsList) json.NewEncoder(w).Encode(patientsList)
} }
func getIndex(w http.ResponseWriter, r *http.Request) { func getIndex(w http.ResponseWriter, r *http.Request) {

View File

@ -7,10 +7,11 @@ import (
) )
// Zrobione ostatnio: // Zrobione ostatnio:
// prosta refraktoryzacja;
// Zrobione:
// Podstawowy serwer; Podstawowe struktury modeli; niezbędne enumy;
// //
// Zrobione:
// Podstawowy serwer; Podstawowe struktury modeli; niezbędne enumy; prosta refraktoryzacja;
// Utworzenie połączenia z bazą danych
// Do zrobienia: // Do zrobienia:
// Funkcje modeli; Interfejsy json; łączenie z bazą danych; Rozne metody do tworzenia komend sql; // Funkcje modeli; Interfejsy json; łączenie z bazą danych; Rozne metody do tworzenia komend sql;
// Komunikacja z aplikacja desktopowa; Walidacja danych!! ; // Komunikacja z aplikacja desktopowa; Walidacja danych!! ;

87
modelsMethods.go Normal file
View File

@ -0,0 +1,87 @@
package main
import (
"database/sql"
"time"
)
func (doc doctors) readModels(rows *sql.Rows) iDatabaseModels {
var models iDatabaseModels
for rows.Next() {
var valueList [7]iHasValue
rows.Scan(&valueList[0], &valueList[1], &valueList[2], &valueList[3], &valueList[4],
&valueList[5], &valueList[6])
models = append(models, doctor{
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
}
func (pat patients) readModels(rows *sql.Rows) iDatabaseModels {
var models iDatabaseModels
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 {
patientSta = patientStates("NULL")
} else {
patientSta = patientStates(valueList[4].(string))
}
var se sex
if valueList[5] == nil {
se = sex("M")
} else {
se = sex(valueList[5].(string))
}
models = append(models, patient{
valueList[0].(string), valueList[1].(string), valueList[2].(string), valueList[3].(time.Time),
patientSta, se, ""})
}
return models
}
func (dia diagnoses) readModels(rows *sql.Rows) iDatabaseModels {
var models iDatabaseModels
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})
}
return models
}
func (ope operations) readModels(rows *sql.Rows) iDatabaseModels {
var models iDatabaseModels
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)})
}
return models
}
/*func (doc admissions) readModels(rows *sql.Rows) iDatabaseModels {
var models iDatabaseModels
for rows.Next() {
var valueList [7]iHasValue
rows.Scan(&valueList[0], &valueList[1], &valueList[2], &valueList[3], &valueList[4],
&valueList[5], &valueList[6])
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))})
}
return models
}*/

View File

@ -16,7 +16,7 @@ type routes []route
var registredRoutes = routes{ var registredRoutes = routes{
route{"Index", "/", index}, route{"Index", "/", index},
route{"GetAll", "/getfromdb", getAll}, route{"GetAll", "/getfromdb/{tableName}", getAll},
route{"GetOne", "/getfromdb/{primaryKey}", getIndex}, route{"GetOne", "/getfromdb/{primaryKey}", getIndex},
} }