From 61c895d53f01d19bd262cf0b12113745644fcf93 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Thu, 5 Oct 2023 16:50:23 +0200 Subject: [PATCH 1/9] Eingabemaske funktioniert soweit i und j sollten jetzt richtig verwendet werden --- main.go | 28 ++++++++++++++++++++++++++-- templates/index.html | 14 +++++++++++--- templates/participant.html | 6 +++--- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index 7de6c65..26a361d 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "html/template" "log" "net/http" @@ -14,7 +15,7 @@ type Person struct { type Instructor Person type Participant struct { - id int64 + ID int64 Person Company string } @@ -29,8 +30,11 @@ type Briefing struct { } func main() { + var i, j int64 + var b Briefing + mux := http.NewServeMux() - i := 1 + i, j = 1, 1 mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { @@ -38,8 +42,28 @@ func main() { }) mux.HandleFunc("/add-participant/", func(w http.ResponseWriter, r *http.Request) { i++ + j = i template.Must(template.ParseFiles("templates/index.html", "templates/participant.html")).ExecuteTemplate(w, "participant", i) }) + mux.HandleFunc("/submit/", func(w http.ResponseWriter, r *http.Request) { + b.Instructor.FirstName = r.PostFormValue("instructor-first") + b.Instructor.LastName = r.PostFormValue("instructor-last") + b.Date = r.PostFormValue("date") + b.Time = r.PostFormValue("time") + b.State = r.PostFormValue("state") + b.Location = r.PostFormValue("location") + + for ; j <= i; j++ { + b.Participants = append(b.Participants, Participant{ + ID: j, + Person: Person{ + FirstName: r.PostFormValue("participant-first-" + fmt.Sprint(j)), + LastName: r.PostFormValue(("participant-last-" + fmt.Sprint(j))), + }, + Company: r.PostFormValue(("participant-company-" + fmt.Sprint(j))), + }) + } + }) log.Fatalln(http.ListenAndServe(":8080", mux)) } diff --git a/templates/index.html b/templates/index.html index b78607d..452bf83 100644 --- a/templates/index.html +++ b/templates/index.html @@ -12,6 +12,14 @@

Sicherheitsunterweisung

+
+ + + + + +
+
@@ -40,13 +48,13 @@
- + - + - +
diff --git a/templates/participant.html b/templates/participant.html index 382f2af..9ed45a6 100644 --- a/templates/participant.html +++ b/templates/participant.html @@ -1,12 +1,12 @@ {{ define "participant" }}
- + - + - +
{{ end }} From ea0fdee0e0f4dd129033a61598bc236d1dbfcf3c Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Thu, 5 Oct 2023 17:01:13 +0200 Subject: [PATCH 2/9] i und j sollten jetzt korrekt verwendet werden --- main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/main.go b/main.go index 26a361d..d03651c 100644 --- a/main.go +++ b/main.go @@ -42,7 +42,6 @@ func main() { }) mux.HandleFunc("/add-participant/", func(w http.ResponseWriter, r *http.Request) { i++ - j = i template.Must(template.ParseFiles("templates/index.html", "templates/participant.html")).ExecuteTemplate(w, "participant", i) }) mux.HandleFunc("/submit/", func(w http.ResponseWriter, r *http.Request) { From fdf68adb0d8bcf3d42f3fad1cc18474b82e030a2 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Thu, 5 Oct 2023 17:42:47 +0200 Subject: [PATCH 3/9] =?UTF-8?q?getCredentials()=20und=20OpenDB()=20zum=20?= =?UTF-8?q?=C3=B6ffnen=20einer=20sql.DB=20erstellt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 7 +++++++ go.sum | 6 ++++++ main.go | 9 +++++++-- 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 db.go create mode 100644 go.sum diff --git a/db.go b/db.go new file mode 100644 index 0000000..3f0d155 --- /dev/null +++ b/db.go @@ -0,0 +1,52 @@ +package sicherheitsunterweisung + +import ( + "bufio" + "database/sql" + "fmt" + "os" + "strings" + "syscall" + + "github.com/go-sql-driver/mysql" + "golang.org/x/term" +) + +func getCredentials() (string, string, error) { + fmt.Printf("DB Benutzer: ") + user, err := bufio.NewReader(os.Stdin).ReadString('\n') + if err != nil { + return "", "", fmt.Errorf("getCredentials: bufio.NewReader(os.Stdin).ReadString('\n'): %v", err) + } + + fmt.Printf("DB Passwort: ") + bytePass, err := term.ReadPassword(int(syscall.Stdin)) + if err != nil { + return "", "", fmt.Errorf("getCredentials: term.ReadPassword(int(syscall.Stdin)): %v", err) + } + fmt.Println() + pass := string(bytePass) + + return strings.TrimSpace(user), strings.TrimSpace(pass), nil +} + +func OpenDB(dbName string) (*sql.DB, error) { + var err error + + cfg := mysql.NewConfig() + cfg.DBName = dbName + cfg.User, cfg.Passwd, err = getCredentials() + if err != nil { + return nil, fmt.Errorf("OpenDB: getCredentials(): %v\n", err) + } + + db, err := sql.Open("mysql", cfg.FormatDSN()) + if err != nil { + return nil, fmt.Errorf("newDB: sql.Open(\"mysql\", cfg.FormatDSN()): %v\n", err) + } + if err := db.Ping(); err != nil { + return nil, fmt.Errorf("newDB: db.Ping(): %v\n", err) + } + + return db, nil +} diff --git a/go.mod b/go.mod index 159d227..729edcb 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,10 @@ module streifling.com/jason/sicherheitsunterweisung go 1.21.1 + +require ( + github.com/go-sql-driver/mysql v1.7.1 + golang.org/x/term v0.13.0 +) + +require golang.org/x/sys v0.13.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..009f59d --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= +github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= +golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= diff --git a/main.go b/main.go index d03651c..f0006f6 100644 --- a/main.go +++ b/main.go @@ -1,4 +1,4 @@ -package main +package sicherheitsunterweisung import ( "fmt" @@ -33,8 +33,13 @@ func main() { var i, j int64 var b Briefing - mux := http.NewServeMux() i, j = 1, 1 + mux := http.NewServeMux() + + db, err := OpenDB("sicherheitsunterweisung") + if err != nil { + log.Fatalln(err) + } mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { From 1c39b1e471be693f3ec4eed5bf180759e616d581 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Thu, 5 Oct 2023 18:08:44 +0200 Subject: [PATCH 4/9] Code in Pakete aufgeteilt --- db.go => db/db.go | 2 +- main.go | 61 ++++++-------------------------------------- server/server.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 54 deletions(-) rename db.go => db/db.go (97%) create mode 100644 server/server.go diff --git a/db.go b/db/db.go similarity index 97% rename from db.go rename to db/db.go index 3f0d155..0566930 100644 --- a/db.go +++ b/db/db.go @@ -1,4 +1,4 @@ -package sicherheitsunterweisung +package db import ( "bufio" diff --git a/main.go b/main.go index f0006f6..1f7cc69 100644 --- a/main.go +++ b/main.go @@ -1,73 +1,28 @@ -package sicherheitsunterweisung +package main import ( - "fmt" - "html/template" "log" "net/http" + "streifling.com/jason/sicherheitsunterweisung/db" + "streifling.com/jason/sicherheitsunterweisung/server" ) -type Person struct { - FirstName string - LastName string -} - -type Instructor Person - -type Participant struct { - ID int64 - Person - Company string -} - -type Briefing struct { - Instructor - Date string - Time string - State string - Location string - Participants []Participant -} - func main() { var i, j int64 - var b Briefing + var b server.Briefing i, j = 1, 1 mux := http.NewServeMux() - db, err := OpenDB("sicherheitsunterweisung") + _, err := db.OpenDB("sicherheitsunterweisung") if err != nil { log.Fatalln(err) } mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - template.Must(template.ParseFiles("templates/index.html")).Execute(w, i) - }) - mux.HandleFunc("/add-participant/", func(w http.ResponseWriter, r *http.Request) { - i++ - template.Must(template.ParseFiles("templates/index.html", "templates/participant.html")).ExecuteTemplate(w, "participant", i) - }) - mux.HandleFunc("/submit/", func(w http.ResponseWriter, r *http.Request) { - b.Instructor.FirstName = r.PostFormValue("instructor-first") - b.Instructor.LastName = r.PostFormValue("instructor-last") - b.Date = r.PostFormValue("date") - b.Time = r.PostFormValue("time") - b.State = r.PostFormValue("state") - b.Location = r.PostFormValue("location") - - for ; j <= i; j++ { - b.Participants = append(b.Participants, Participant{ - ID: j, - Person: Person{ - FirstName: r.PostFormValue("participant-first-" + fmt.Sprint(j)), - LastName: r.PostFormValue(("participant-last-" + fmt.Sprint(j))), - }, - Company: r.PostFormValue(("participant-company-" + fmt.Sprint(j))), - }) - } - }) + mux.HandleFunc("/", server.DisplayForm(i)) + mux.HandleFunc("/add-participant/", server.AddParticipant(i)) + mux.HandleFunc("/submit/", server.SubmitForm(b, i, j)) log.Fatalln(http.ListenAndServe(":8080", mux)) } diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..cda63fa --- /dev/null +++ b/server/server.go @@ -0,0 +1,64 @@ +package server + +import ( + "fmt" + "html/template" + "net/http" +) + +type Person struct { + FirstName string + LastName string +} + +type Instructor Person + +type Participant struct { + ID int64 + Person + Company string +} + +type Briefing struct { + Instructor + Date string + Time string + State string + Location string + Participants []Participant +} + +func DisplayForm(i int64) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + template.Must(template.ParseFiles("templates/index.html")).Execute(w, i) + } +} + +func AddParticipant(i int64) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + i++ + template.Must(template.ParseFiles("templates/index.html", "templates/participant.html")).ExecuteTemplate(w, "participant", i) + } +} + +func SubmitForm(b Briefing, i, j int64) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + b.Instructor.FirstName = r.PostFormValue("instructor-first") + b.Instructor.LastName = r.PostFormValue("instructor-last") + b.Date = r.PostFormValue("date") + b.Time = r.PostFormValue("time") + b.State = r.PostFormValue("state") + b.Location = r.PostFormValue("location") + + for ; j <= i; j++ { + b.Participants = append(b.Participants, Participant{ + ID: j, + Person: Person{ + FirstName: r.PostFormValue("participant-first-" + fmt.Sprint(j)), + LastName: r.PostFormValue(("participant-last-" + fmt.Sprint(j))), + }, + Company: r.PostFormValue(("participant-company-" + fmt.Sprint(j))), + }) + } + } +} From fcb509c9fe847d966361a1c19329a5a06e565f97 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Thu, 5 Oct 2023 18:27:21 +0200 Subject: [PATCH 5/9] Verwendung von Pointern, um Vermischung der Pakete zu vermeiden --- main.go | 12 ++++++------ server/server.go | 25 ++++++++++++++----------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index 1f7cc69..08ef011 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ package main import ( "log" "net/http" - "streifling.com/jason/sicherheitsunterweisung/db" + // "streifling.com/jason/sicherheitsunterweisung/db" "streifling.com/jason/sicherheitsunterweisung/server" ) @@ -14,15 +14,15 @@ func main() { i, j = 1, 1 mux := http.NewServeMux() - _, err := db.OpenDB("sicherheitsunterweisung") + /* _, err := db.OpenDB("sicherheitsunterweisung") if err != nil { log.Fatalln(err) - } + } */ mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) - mux.HandleFunc("/", server.DisplayForm(i)) - mux.HandleFunc("/add-participant/", server.AddParticipant(i)) - mux.HandleFunc("/submit/", server.SubmitForm(b, i, j)) + mux.HandleFunc("/", server.DisplayForm(&i)) + mux.HandleFunc("/add-participant/", server.AddParticipant(&i)) + mux.HandleFunc("/submit/", server.SubmitForm(&b, &i, &j)) log.Fatalln(http.ListenAndServe(":8080", mux)) } diff --git a/server/server.go b/server/server.go index cda63fa..119ac72 100644 --- a/server/server.go +++ b/server/server.go @@ -3,6 +3,7 @@ package server import ( "fmt" "html/template" + "log" "net/http" ) @@ -28,37 +29,39 @@ type Briefing struct { Participants []Participant } -func DisplayForm(i int64) http.HandlerFunc { +func DisplayForm(i *int64) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { template.Must(template.ParseFiles("templates/index.html")).Execute(w, i) } } -func AddParticipant(i int64) http.HandlerFunc { +func AddParticipant(i *int64) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - i++ + *i++ template.Must(template.ParseFiles("templates/index.html", "templates/participant.html")).ExecuteTemplate(w, "participant", i) } } -func SubmitForm(b Briefing, i, j int64) http.HandlerFunc { +func SubmitForm(b *Briefing, i, j *int64) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - b.Instructor.FirstName = r.PostFormValue("instructor-first") - b.Instructor.LastName = r.PostFormValue("instructor-last") + b.FirstName = r.PostFormValue("instructor-first") + b.LastName = r.PostFormValue("instructor-last") b.Date = r.PostFormValue("date") b.Time = r.PostFormValue("time") b.State = r.PostFormValue("state") b.Location = r.PostFormValue("location") - for ; j <= i; j++ { + for ; *j <= *i; *j++ { b.Participants = append(b.Participants, Participant{ - ID: j, + ID: *j, Person: Person{ - FirstName: r.PostFormValue("participant-first-" + fmt.Sprint(j)), - LastName: r.PostFormValue(("participant-last-" + fmt.Sprint(j))), + FirstName: r.PostFormValue("participant-first-" + fmt.Sprint(*j)), + LastName: r.PostFormValue(("participant-last-" + fmt.Sprint(*j))), }, - Company: r.PostFormValue(("participant-company-" + fmt.Sprint(j))), + Company: r.PostFormValue(("participant-company-" + fmt.Sprint(*j))), }) } + + log.Println("Received ", *b) } } From 1bcfbfd325ff4acd34c49fdbdfc2fde690468f4d Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Thu, 5 Oct 2023 19:52:11 +0200 Subject: [PATCH 6/9] Verschiedene Typen in package types ausgelagert und Channels zum asynchronen Datentransport eingesetzt --- db/db.go | 24 ++++++++++++++++++++++-- main.go | 18 +++++++++++++----- server/server.go | 34 ++++++++-------------------------- types/types.go | 23 +++++++++++++++++++++++ 4 files changed, 66 insertions(+), 33 deletions(-) create mode 100644 types/types.go diff --git a/db/db.go b/db/db.go index 0566930..65cfa25 100644 --- a/db/db.go +++ b/db/db.go @@ -8,10 +8,17 @@ import ( "strings" "syscall" + "streifling.com/jason/sicherheitsunterweisung/types" + "github.com/go-sql-driver/mysql" "golang.org/x/term" ) +type DB struct { + *sql.DB + Name string +} + func getCredentials() (string, string, error) { fmt.Printf("DB Benutzer: ") user, err := bufio.NewReader(os.Stdin).ReadString('\n') @@ -30,7 +37,8 @@ func getCredentials() (string, string, error) { return strings.TrimSpace(user), strings.TrimSpace(pass), nil } -func OpenDB(dbName string) (*sql.DB, error) { +func OpenDB(dbName string) (*DB, error) { + var db *DB var err error cfg := mysql.NewConfig() @@ -40,7 +48,8 @@ func OpenDB(dbName string) (*sql.DB, error) { return nil, fmt.Errorf("OpenDB: getCredentials(): %v\n", err) } - db, err := sql.Open("mysql", cfg.FormatDSN()) + db.Name = dbName + db.DB, err = sql.Open("mysql", cfg.FormatDSN()) if err != nil { return nil, fmt.Errorf("newDB: sql.Open(\"mysql\", cfg.FormatDSN()): %v\n", err) } @@ -50,3 +59,14 @@ func OpenDB(dbName string) (*sql.DB, error) { return db, nil } + +func (db *DB) WriteBriefing(b *types.Briefing) error { + for i := 1; i < len(b.Participants); i++ { + _, err := db.Exec("INSERT INTO ? (instructor_first, instructor_last, date, time, state, location, participant_first, participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", db.Name, b.FirstName, b.LastName, b.Date, b.Time, b.State, b.Location, b.Participants[i].FirstName, b.Participants[i].LastName, b.Participants[i].Company) + if err != nil { + return fmt.Errorf("*DB.WriteBriefing: db.Exec(\"INSERT INTO ? (instructor_first, instructor_last, date, time, state, location, participant_first, participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)\", db.Name, b.FirstName, b.LastName, b.Date, b.Time, b.State, b.Location, b.Participants[i].FirstName, b.Participants[i].LastName, b.Participants[i].Company): %v\n", err) + } + } + + return nil +} diff --git a/main.go b/main.go index 08ef011..f2212a5 100644 --- a/main.go +++ b/main.go @@ -3,26 +3,34 @@ package main import ( "log" "net/http" - // "streifling.com/jason/sicherheitsunterweisung/db" + + "streifling.com/jason/sicherheitsunterweisung/db" "streifling.com/jason/sicherheitsunterweisung/server" + "streifling.com/jason/sicherheitsunterweisung/types" ) func main() { var i, j int64 - var b server.Briefing i, j = 1, 1 mux := http.NewServeMux() + cb := make(chan *types.Briefing) - /* _, err := db.OpenDB("sicherheitsunterweisung") + db, err := db.OpenDB("sicherheitsunterweisung") if err != nil { log.Fatalln(err) - } */ + } mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) mux.HandleFunc("/", server.DisplayForm(&i)) mux.HandleFunc("/add-participant/", server.AddParticipant(&i)) - mux.HandleFunc("/submit/", server.SubmitForm(&b, &i, &j)) + mux.HandleFunc("/submit/", server.SubmitForm(cb, &i, &j)) + + go func() { + for b := range cb { + db.WriteBriefing(b) + } + }() log.Fatalln(http.ListenAndServe(":8080", mux)) } diff --git a/server/server.go b/server/server.go index 119ac72..8531aa3 100644 --- a/server/server.go +++ b/server/server.go @@ -5,30 +5,9 @@ import ( "html/template" "log" "net/http" + "streifling.com/jason/sicherheitsunterweisung/types" ) -type Person struct { - FirstName string - LastName string -} - -type Instructor Person - -type Participant struct { - ID int64 - Person - Company string -} - -type Briefing struct { - Instructor - Date string - Time string - State string - Location string - Participants []Participant -} - func DisplayForm(i *int64) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { template.Must(template.ParseFiles("templates/index.html")).Execute(w, i) @@ -42,8 +21,10 @@ func AddParticipant(i *int64) http.HandlerFunc { } } -func SubmitForm(b *Briefing, i, j *int64) http.HandlerFunc { +func SubmitForm(ch chan<- *types.Briefing, i, j *int64) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + var b types.Briefing + b.FirstName = r.PostFormValue("instructor-first") b.LastName = r.PostFormValue("instructor-last") b.Date = r.PostFormValue("date") @@ -52,9 +33,9 @@ func SubmitForm(b *Briefing, i, j *int64) http.HandlerFunc { b.Location = r.PostFormValue("location") for ; *j <= *i; *j++ { - b.Participants = append(b.Participants, Participant{ + b.Participants = append(b.Participants, types.Participant{ ID: *j, - Person: Person{ + Person: types.Person{ FirstName: r.PostFormValue("participant-first-" + fmt.Sprint(*j)), LastName: r.PostFormValue(("participant-last-" + fmt.Sprint(*j))), }, @@ -62,6 +43,7 @@ func SubmitForm(b *Briefing, i, j *int64) http.HandlerFunc { }) } - log.Println("Received ", *b) + log.Println(b) + ch <- &b } } diff --git a/types/types.go b/types/types.go new file mode 100644 index 0000000..baf87ee --- /dev/null +++ b/types/types.go @@ -0,0 +1,23 @@ +package types + +type Person struct { + FirstName string + LastName string +} + +type Instructor Person + +type Participant struct { + ID int64 + Person + Company string +} + +type Briefing struct { + Instructor + Date string + Time string + State string + Location string + Participants []Participant +} From 5749739761d62749b5636ef6be72e4461d473d67 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Thu, 5 Oct 2023 19:56:18 +0200 Subject: [PATCH 7/9] =?UTF-8?q?Versuch,=20eine=20=C3=BCbersichtlichere=20S?= =?UTF-8?q?truktur=20einzuf=C3=BChren?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 6 +++--- {db => packages/db}/db.go | 2 +- {server => packages/server}/server.go | 2 +- {types => packages/types}/types.go | 0 4 files changed, 5 insertions(+), 5 deletions(-) rename {db => packages/db}/db.go (97%) rename {server => packages/server}/server.go (95%) rename {types => packages/types}/types.go (100%) diff --git a/main.go b/main.go index f2212a5..f16135c 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,9 @@ import ( "log" "net/http" - "streifling.com/jason/sicherheitsunterweisung/db" - "streifling.com/jason/sicherheitsunterweisung/server" - "streifling.com/jason/sicherheitsunterweisung/types" + "streifling.com/jason/sicherheitsunterweisung/packages/db" + "streifling.com/jason/sicherheitsunterweisung/packages/server" + "streifling.com/jason/sicherheitsunterweisung/packages/types" ) func main() { diff --git a/db/db.go b/packages/db/db.go similarity index 97% rename from db/db.go rename to packages/db/db.go index 65cfa25..2b8c8d0 100644 --- a/db/db.go +++ b/packages/db/db.go @@ -8,7 +8,7 @@ import ( "strings" "syscall" - "streifling.com/jason/sicherheitsunterweisung/types" + "streifling.com/jason/sicherheitsunterweisung/packages/types" "github.com/go-sql-driver/mysql" "golang.org/x/term" diff --git a/server/server.go b/packages/server/server.go similarity index 95% rename from server/server.go rename to packages/server/server.go index 8531aa3..3b2af7d 100644 --- a/server/server.go +++ b/packages/server/server.go @@ -5,7 +5,7 @@ import ( "html/template" "log" "net/http" - "streifling.com/jason/sicherheitsunterweisung/types" + "streifling.com/jason/sicherheitsunterweisung/packages/types" ) func DisplayForm(i *int64) http.HandlerFunc { diff --git a/types/types.go b/packages/types/types.go similarity index 100% rename from types/types.go rename to packages/types/types.go From f5709504253978a7ffd7cfdee4631a4a4c78d14a Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Fri, 6 Oct 2023 19:07:56 +0200 Subject: [PATCH 8/9] In Datenbank schreiben und nach Namen suchen implementiert --- .gitignore | 1 + main.go | 12 +++++++----- packages/db/db.go | 48 ++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c8a398a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +create_tables.sql diff --git a/main.go b/main.go index f16135c..3b0bd1f 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,12 @@ import ( "streifling.com/jason/sicherheitsunterweisung/packages/types" ) +func writeBriefing(ch chan *types.Briefing, db *db.DB) { + for b := range ch { + db.WriteBriefing(b) + } +} + func main() { var i, j int64 @@ -26,11 +32,7 @@ func main() { mux.HandleFunc("/add-participant/", server.AddParticipant(&i)) mux.HandleFunc("/submit/", server.SubmitForm(cb, &i, &j)) - go func() { - for b := range cb { - db.WriteBriefing(b) - } - }() + go writeBriefing(cb, db) log.Fatalln(http.ListenAndServe(":8080", mux)) } diff --git a/packages/db/db.go b/packages/db/db.go index 2b8c8d0..94c1ef7 100644 --- a/packages/db/db.go +++ b/packages/db/db.go @@ -38,7 +38,7 @@ func getCredentials() (string, string, error) { } func OpenDB(dbName string) (*DB, error) { - var db *DB + var db DB var err error cfg := mysql.NewConfig() @@ -57,16 +57,54 @@ func OpenDB(dbName string) (*DB, error) { return nil, fmt.Errorf("newDB: db.Ping(): %v\n", err) } - return db, nil + return &db, nil } func (db *DB) WriteBriefing(b *types.Briefing) error { - for i := 1; i < len(b.Participants); i++ { - _, err := db.Exec("INSERT INTO ? (instructor_first, instructor_last, date, time, state, location, participant_first, participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", db.Name, b.FirstName, b.LastName, b.Date, b.Time, b.State, b.Location, b.Participants[i].FirstName, b.Participants[i].LastName, b.Participants[i].Company) + for i := 0; i < len(b.Participants); i++ { + result, err := db.Exec("INSERT INTO "+db.Name+" (instructor_first,"+ + "instructor_last, date, time, state, location, participant_first,"+ + "participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", + b.FirstName, b.LastName, b.Date, b.Time, b.State, b.Location, + b.Participants[i].FirstName, b.Participants[i].LastName, + b.Participants[i].Company) if err != nil { - return fmt.Errorf("*DB.WriteBriefing: db.Exec(\"INSERT INTO ? (instructor_first, instructor_last, date, time, state, location, participant_first, participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)\", db.Name, b.FirstName, b.LastName, b.Date, b.Time, b.State, b.Location, b.Participants[i].FirstName, b.Participants[i].LastName, b.Participants[i].Company): %v\n", err) + return fmt.Errorf("*DB.WriteBriefing: db.Exec(\"INSERT INTO"+ + "\"+db.Name+\" (instructor_first, instructor_last, date, time, state,"+ + "location, participant_first, participant_last, company) VALUES (?, ?,"+ + "?, ?, ?, ?, ?, ?, ?)\", b.FirstName, b.LastName, b.Date, b.Time,"+ + "b.State, b.Location, b.Participants[i].FirstName,"+ + "b.Participants[i].LastName, b.Participants[i].Company): %v\n", err) + } + + _, err = result.LastInsertId() + if err != nil { + return fmt.Errorf("*DB.WriteBriefing: result.LastInsertId(): %v\n", err) } } return nil } + +func (db *DB) ReadByName(name string) (*[]types.Briefing, error) { + var b types.Briefing + var bs []types.Briefing + + rows, err := db.Query("SELECT * FROM " + db.Name + " WHERE" + + " instructor_first LIKE " + name + " OR instructor_last LIKE " + name + + " participant_first LIKE " + name + " participant_last LIKE " + name) + if err != nil { + return nil, fmt.Errorf("*DB.ReadByName: db.Query(\"SELECT * FROM \"+db.Name+\" WHERE" + + " instructor_first LIKE \"+name+\" OR instructor_last LIKE \"+name+" + + " participant_first LIKE \"+name+\" participant_last LIKE \"+name)") + } + + for rows.Next() { + if err := rows.Scan(&b.FirstName, &b.LastName, &b.Date, &b.Time, &b.State, &b.Location, &b.Participants[0].FirstName, &b.Participants[0].LastName, &b.Participants[0].Company); err != nil { + return nil, fmt.Errorf("*DB.ReadByName: rows.Scan(&b.FirstName, &b.LastName, &b.Date, &b.Time, &b.State, &b.Location, &b.Participants[0].FirstName, &b.Participants[0].LastName, &b.Participants[0].Company)") + } + bs = append(bs, b) + } + + return &bs, nil +} From 9acc6711fc4b10bc3e3d447f88ec2174f0498095 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sat, 7 Oct 2023 16:58:34 +0200 Subject: [PATCH 9/9] =?UTF-8?q?Nach=20Suchbegriff=20aus=20Datenbank=20lese?= =?UTF-8?q?n=20implementiert,=20au=C3=9Ferdem=20structs=20mit=20new()=20in?= =?UTF-8?q?stanziiert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 2 +- packages/db/db.go | 48 +++++++++++++++++++++++++-------------- packages/server/server.go | 4 ++-- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/main.go b/main.go index 3b0bd1f..946afbd 100644 --- a/main.go +++ b/main.go @@ -22,7 +22,7 @@ func main() { mux := http.NewServeMux() cb := make(chan *types.Briefing) - db, err := db.OpenDB("sicherheitsunterweisung") + db, err := db.Open("sicherheitsunterweisung") if err != nil { log.Fatalln(err) } diff --git a/packages/db/db.go b/packages/db/db.go index 94c1ef7..4a82091 100644 --- a/packages/db/db.go +++ b/packages/db/db.go @@ -37,27 +37,27 @@ func getCredentials() (string, string, error) { return strings.TrimSpace(user), strings.TrimSpace(pass), nil } -func OpenDB(dbName string) (*DB, error) { - var db DB +func Open(dbName string) (*DB, error) { var err error + db := new(DB) cfg := mysql.NewConfig() cfg.DBName = dbName cfg.User, cfg.Passwd, err = getCredentials() if err != nil { - return nil, fmt.Errorf("OpenDB: getCredentials(): %v\n", err) + return nil, fmt.Errorf("Open: getCredentials(): %v\n", err) } db.Name = dbName db.DB, err = sql.Open("mysql", cfg.FormatDSN()) if err != nil { - return nil, fmt.Errorf("newDB: sql.Open(\"mysql\", cfg.FormatDSN()): %v\n", err) + return nil, fmt.Errorf("Open: sql.Open(\"mysql\", cfg.FormatDSN()): %v\n", err) } if err := db.Ping(); err != nil { - return nil, fmt.Errorf("newDB: db.Ping(): %v\n", err) + return nil, fmt.Errorf("Open: db.Ping(): %v\n", err) } - return &db, nil + return db, nil } func (db *DB) WriteBriefing(b *types.Briefing) error { @@ -87,23 +87,37 @@ func (db *DB) WriteBriefing(b *types.Briefing) error { } func (db *DB) ReadByName(name string) (*[]types.Briefing, error) { - var b types.Briefing - var bs []types.Briefing + bs := make([]types.Briefing, 0) - rows, err := db.Query("SELECT * FROM " + db.Name + " WHERE" + - " instructor_first LIKE " + name + " OR instructor_last LIKE " + name + - " participant_first LIKE " + name + " participant_last LIKE " + name) + rows, err := db.Query("SELECT *"+ + " FROM "+db.Name+ + " WHERE instructor_first LIKE ?"+ + " OR instructor_last LIKE ?"+ + " OR participant_first LIKE ?"+ + " OR participant_last LIKE ?", + "%"+name+"%", "%"+name+"%", "%"+name+"%", "%"+name+"%") if err != nil { - return nil, fmt.Errorf("*DB.ReadByName: db.Query(\"SELECT * FROM \"+db.Name+\" WHERE" + - " instructor_first LIKE \"+name+\" OR instructor_last LIKE \"+name+" + - " participant_first LIKE \"+name+\" participant_last LIKE \"+name)") + return nil, fmt.Errorf("*DB.ReadByName: db.Query(\"SELECT *"+ + " FROM \"+db.Name+"+ + " WHERE instructor_first LIKE ?"+ + " OR instructor_last LIKE ?"+ + " OR participant_first LIKE ?"+ + " OR participant_last LIKE ?\"): %v\n", err) } + defer rows.Close() for rows.Next() { - if err := rows.Scan(&b.FirstName, &b.LastName, &b.Date, &b.Time, &b.State, &b.Location, &b.Participants[0].FirstName, &b.Participants[0].LastName, &b.Participants[0].Company); err != nil { - return nil, fmt.Errorf("*DB.ReadByName: rows.Scan(&b.FirstName, &b.LastName, &b.Date, &b.Time, &b.State, &b.Location, &b.Participants[0].FirstName, &b.Participants[0].LastName, &b.Participants[0].Company)") + b := new(types.Briefing) + p := new(types.Participant) + + if err := rows.Scan(&p.ID, &b.FirstName, &b.LastName, &b.Date, &b.Time, &b.State, + &b.Location, &p.FirstName, &p.LastName, &p.Company); err != nil { + return nil, fmt.Errorf("*DB.ReadByName: rows.Scan(&p.ID, &b.FirstName,"+ + " &b.LastName, &b.Date, &b.Time, &b.State, &b.Location, &p.FirstName,"+ + " &p.LastName, &p.Company): %v\n", err) } - bs = append(bs, b) + b.Participants = append(b.Participants, *p) + bs = append(bs, *b) } return &bs, nil diff --git a/packages/server/server.go b/packages/server/server.go index 3b2af7d..dbab18e 100644 --- a/packages/server/server.go +++ b/packages/server/server.go @@ -23,7 +23,7 @@ func AddParticipant(i *int64) http.HandlerFunc { func SubmitForm(ch chan<- *types.Briefing, i, j *int64) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var b types.Briefing + b := new(types.Briefing) b.FirstName = r.PostFormValue("instructor-first") b.LastName = r.PostFormValue("instructor-last") @@ -44,6 +44,6 @@ func SubmitForm(ch chan<- *types.Briefing, i, j *int64) http.HandlerFunc { } log.Println(b) - ch <- &b + ch <- b } }