Compare commits

..

No commits in common. "1bcfbfd325ff4acd34c49fdbdfc2fde690468f4d" and "fdf68adb0d8bcf3d42f3fad1cc18474b82e030a2" have entirely different histories.

4 changed files with 54 additions and 109 deletions

View File

@ -1,4 +1,4 @@
package db package sicherheitsunterweisung
import ( import (
"bufio" "bufio"
@ -8,17 +8,10 @@ import (
"strings" "strings"
"syscall" "syscall"
"streifling.com/jason/sicherheitsunterweisung/types"
"github.com/go-sql-driver/mysql" "github.com/go-sql-driver/mysql"
"golang.org/x/term" "golang.org/x/term"
) )
type DB struct {
*sql.DB
Name string
}
func getCredentials() (string, string, error) { func getCredentials() (string, string, error) {
fmt.Printf("DB Benutzer: ") fmt.Printf("DB Benutzer: ")
user, err := bufio.NewReader(os.Stdin).ReadString('\n') user, err := bufio.NewReader(os.Stdin).ReadString('\n')
@ -37,8 +30,7 @@ func getCredentials() (string, string, error) {
return strings.TrimSpace(user), strings.TrimSpace(pass), nil return strings.TrimSpace(user), strings.TrimSpace(pass), nil
} }
func OpenDB(dbName string) (*DB, error) { func OpenDB(dbName string) (*sql.DB, error) {
var db *DB
var err error var err error
cfg := mysql.NewConfig() cfg := mysql.NewConfig()
@ -48,8 +40,7 @@ func OpenDB(dbName string) (*DB, error) {
return nil, fmt.Errorf("OpenDB: getCredentials(): %v\n", err) return nil, fmt.Errorf("OpenDB: getCredentials(): %v\n", err)
} }
db.Name = dbName db, err := sql.Open("mysql", cfg.FormatDSN())
db.DB, err = sql.Open("mysql", cfg.FormatDSN())
if err != nil { if err != nil {
return nil, fmt.Errorf("newDB: sql.Open(\"mysql\", cfg.FormatDSN()): %v\n", err) return nil, fmt.Errorf("newDB: sql.Open(\"mysql\", cfg.FormatDSN()): %v\n", err)
} }
@ -59,14 +50,3 @@ func OpenDB(dbName string) (*DB, error) {
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)
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,36 +1,73 @@
package main package sicherheitsunterweisung
import ( import (
"fmt"
"html/template"
"log" "log"
"net/http" "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() { func main() {
var i, j int64 var i, j int64
var b Briefing
i, j = 1, 1 i, j = 1, 1
mux := http.NewServeMux() mux := http.NewServeMux()
cb := make(chan *types.Briefing)
db, err := db.OpenDB("sicherheitsunterweisung") db, err := OpenDB("sicherheitsunterweisung")
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
mux.HandleFunc("/", server.DisplayForm(&i)) mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/add-participant/", server.AddParticipant(&i)) template.Must(template.ParseFiles("templates/index.html")).Execute(w, i)
mux.HandleFunc("/submit/", server.SubmitForm(cb, &i, &j)) })
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")
go func() { for ; j <= i; j++ {
for b := range cb { b.Participants = append(b.Participants, Participant{
db.WriteBriefing(b) 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)) log.Fatalln(http.ListenAndServe(":8080", mux))
} }

View File

@ -1,49 +0,0 @@
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
}
}

View File

@ -1,23 +0,0 @@
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
}