Compare commits

...

3 Commits

4 changed files with 109 additions and 54 deletions

View File

@ -1,4 +1,4 @@
package sicherheitsunterweisung
package db
import (
"bufio"
@ -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
}

65
main.go
View File

@ -1,73 +1,36 @@
package sicherheitsunterweisung
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"streifling.com/jason/sicherheitsunterweisung/db"
"streifling.com/jason/sicherheitsunterweisung/server"
"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 main() {
var i, j int64
var b Briefing
i, j = 1, 1
mux := http.NewServeMux()
cb := make(chan *types.Briefing)
db, err := 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("/", 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")
mux.HandleFunc("/", server.DisplayForm(&i))
mux.HandleFunc("/add-participant/", server.AddParticipant(&i))
mux.HandleFunc("/submit/", server.SubmitForm(cb, &i, &j))
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))),
})
go func() {
for b := range cb {
db.WriteBriefing(b)
}
})
}()
log.Fatalln(http.ListenAndServe(":8080", mux))
}

49
server/server.go Normal file
View File

@ -0,0 +1,49 @@
package server
import (
"fmt"
"html/template"
"log"
"net/http"
"streifling.com/jason/sicherheitsunterweisung/types"
)
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(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")
b.Time = r.PostFormValue("time")
b.State = r.PostFormValue("state")
b.Location = r.PostFormValue("location")
for ; *j <= *i; *j++ {
b.Participants = append(b.Participants, types.Participant{
ID: *j,
Person: types.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.Println(b)
ch <- &b
}
}

23
types/types.go Normal file
View File

@ -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
}