Versuch, eine übersichtlichere Struktur einzuführen
This commit is contained in:
72
packages/db/db.go
Normal file
72
packages/db/db.go
Normal file
@ -0,0 +1,72 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/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')
|
||||
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) (*DB, error) {
|
||||
var db *DB
|
||||
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.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)
|
||||
}
|
||||
if err := db.Ping(); err != nil {
|
||||
return nil, fmt.Errorf("newDB: db.Ping(): %v\n", err)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
49
packages/server/server.go
Normal file
49
packages/server/server.go
Normal file
@ -0,0 +1,49 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/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
packages/types/types.go
Normal file
23
packages/types/types.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user