Noch ein Bisschen aufgeräumt
This commit is contained in:
parent
39d8108521
commit
5019432b24
26
main.go
26
main.go
@ -7,39 +7,17 @@ import (
|
|||||||
|
|
||||||
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
||||||
"streifling.com/jason/sicherheitsunterweisung/packages/server"
|
"streifling.com/jason/sicherheitsunterweisung/packages/server"
|
||||||
"streifling.com/jason/sicherheitsunterweisung/packages/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// func waitForParticipants(sb []*types.Briefing, sp []*types.Participant, cp <-chan *types.Participant, m *http.ServeMux) {
|
|
||||||
// for p := range cp {
|
|
||||||
// sg := make([]*types.GivenAnswer, 4)
|
|
||||||
//
|
|
||||||
// p.Questions = data.InitQuestions()
|
|
||||||
// sp = append(sp, p)
|
|
||||||
//
|
|
||||||
// var i int
|
|
||||||
// for i = range p.Questions {
|
|
||||||
// m.HandleFunc("/display-question-"+fmt.Sprintf("%d", p.ID)+"-"+fmt.Sprintf("%d", i)+"/", server.DisplayQuestion(i, p))
|
|
||||||
// }
|
|
||||||
// m.HandleFunc("/display-question-"+fmt.Sprintf("%d", p.ID)+"-"+fmt.Sprintf("%d", i+1)+"/", server.DisplayTestResults(sb, p))
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
logins := make([]string, 0)
|
logins := make([]string, 0)
|
||||||
// participants := make([]*types.Participant, 0)
|
|
||||||
briefings := make([]*types.Briefing, 0)
|
|
||||||
mux := http.NewServeMux()
|
|
||||||
|
|
||||||
db, err := data.OpenDB("sicherheitsunterweisung")
|
db, err := data.OpenDB("sicherheitsunterweisung")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// participantChan := make(chan *types.Participant)
|
mux := http.NewServeMux()
|
||||||
// defer close(participantChan)
|
|
||||||
// go waitForParticipants(briefings, participants, participantChan, mux)
|
|
||||||
|
|
||||||
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
|
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
|
||||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
template.Must(template.ParseFiles("templates/index.html", "templates/login.html")).Execute(w, nil)
|
template.Must(template.ParseFiles("templates/index.html", "templates/login.html")).Execute(w, nil)
|
||||||
@ -47,7 +25,7 @@ func main() {
|
|||||||
mux.HandleFunc("/search/", server.DisplaySearchResults(db))
|
mux.HandleFunc("/search/", server.DisplaySearchResults(db))
|
||||||
mux.HandleFunc("/new-briefing/", server.DisplayInstructorForm())
|
mux.HandleFunc("/new-briefing/", server.DisplayInstructorForm())
|
||||||
mux.HandleFunc("/add-participant/", server.AddParticipant(&logins))
|
mux.HandleFunc("/add-participant/", server.AddParticipant(&logins))
|
||||||
mux.HandleFunc("/submit-form/", server.SubmitBriefingForm(&briefings))
|
mux.HandleFunc("/submit-form/", server.SubmitBriefingForm(db, &logins))
|
||||||
mux.HandleFunc("/internal-login/", server.DisplayTable(db))
|
mux.HandleFunc("/internal-login/", server.DisplayTable(db))
|
||||||
mux.HandleFunc("/external-login/", server.DisplayParticipantForm(&logins))
|
mux.HandleFunc("/external-login/", server.DisplayParticipantForm(&logins))
|
||||||
|
|
||||||
|
@ -60,12 +60,6 @@ func getCredentials() (string, string, error) {
|
|||||||
return user, pass, nil
|
return user, pass, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func reverseOrder(bs []*types.Briefing) {
|
|
||||||
for i, j := 0, len(bs)-1; i < j; i, j = i+1, j-1 {
|
|
||||||
bs[i], bs[j] = bs[j], bs[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func OpenDB(dbName string) (*DB, error) {
|
func OpenDB(dbName string) (*DB, error) {
|
||||||
var err error
|
var err error
|
||||||
db := new(DB)
|
db := new(DB)
|
||||||
@ -89,7 +83,7 @@ func OpenDB(dbName string) (*DB, error) {
|
|||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) writeBriefing(b *types.Briefing) error {
|
func (db *DB) WriteBriefing(b *types.Briefing) error {
|
||||||
result, err := db.Exec(`
|
result, err := db.Exec(`
|
||||||
INSERT INTO briefings
|
INSERT INTO briefings
|
||||||
(date, time, location, document_name, as_of, instructor_id)
|
(date, time, location, document_name, as_of, instructor_id)
|
||||||
@ -100,7 +94,7 @@ func (db *DB) writeBriefing(b *types.Briefing) error {
|
|||||||
return fmt.Errorf("*DB.writeBriefing: db.Exec(): %v\n", err)
|
return fmt.Errorf("*DB.writeBriefing: db.Exec(): %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = result.LastInsertId()
|
b.ID, err = result.LastInsertId()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("*DB.writeBriefing: result.LastInsertId(): %v\n", err)
|
return fmt.Errorf("*DB.writeBriefing: result.LastInsertId(): %v\n", err)
|
||||||
}
|
}
|
||||||
@ -108,61 +102,54 @@ func (db *DB) writeBriefing(b *types.Briefing) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) writeParticipants(sp *[]*types.Participant) error {
|
func (db *DB) WriteParticipant(p *types.Participant) error {
|
||||||
for _, p := range *sp {
|
result, err := db.Exec(`
|
||||||
result, err := db.Exec(`
|
INSERT INTO participants
|
||||||
INSERT INTO participants
|
(first_name, last_name, company)
|
||||||
(first_name, last_name, company)
|
VALUES
|
||||||
VALUES
|
(?, ?, ?)
|
||||||
(?, ?, ?)
|
`, p.FirstName, p.LastName, p.Company)
|
||||||
`, p.FirstName, p.LastName, p.Company)
|
if err != nil {
|
||||||
if err != nil {
|
return fmt.Errorf("*DB.writeParticipants: db.Exec(): %v\n", err)
|
||||||
return fmt.Errorf("*DB.writeParticipants: db.Exec(): %v\n", err)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
_, err = result.LastInsertId()
|
p.ID, err = result.LastInsertId()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("*DB.writeParticipants: result.LastInsertId(): %v\n", err)
|
return fmt.Errorf("*DB.writeParticipants: result.LastInsertId(): %v\n", err)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) writeGivenAnswers(b *types.Briefing, sp *[]*types.Participant, sq *[]*types.Question, sg *[]*types.GivenAnswer) error {
|
func (db *DB) WriteGivenAnswer(b *types.Briefing, p *types.Participant, q *types.Question, g *types.GivenAnswer) error {
|
||||||
for _, p := range *sp {
|
_, err := db.Exec(`
|
||||||
for i, q := range *sq {
|
INSERT INTO given_answers
|
||||||
result, err := db.Exec(`
|
(briefing_id, participant_id, question_id, given_answer)
|
||||||
INSERT INTO given_answers
|
VALUES
|
||||||
(briefing_id, participant_id, question_id, given_answer)
|
(?, ?, ?, ?)
|
||||||
VALUES
|
`, b.ID, p.ID, q.ID, g)
|
||||||
(?, ?, ?, ?)
|
if err != nil {
|
||||||
`, b.ID, p.ID, q.ID, (*sg)[i])
|
return fmt.Errorf("*DB.writeGivenAnswers: db.Exec(): %v\n", err)
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("*DB.writeGivenAnswers: db.Exec(): %v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = result.LastInsertId()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("*DB.writeGivenAnswers: result.LastInsertId(): %v\n", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) WriteAllDataOfBriefing(b *types.Briefing, sp *[]*types.Participant, sq *[]*types.Question, sg *[]*types.GivenAnswer) error {
|
func (db *DB) WriteAllDataOfBriefing(b *types.Briefing, sp *[]*types.Participant, sq *[]*types.Question, sg *[]*types.GivenAnswer) error {
|
||||||
if err := db.writeBriefing(b); err != nil {
|
if err := db.WriteBriefing(b); err != nil {
|
||||||
return fmt.Errorf("*DB.WriteAllDataOfBriefing: db.writeBriefing(): %v\n", err)
|
return fmt.Errorf("*DB.WriteAllDataOfBriefing: db.writeBriefing(): %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.writeParticipants(sp); err != nil {
|
for _, p := range *sp {
|
||||||
return fmt.Errorf("*DB.WriteAllDataOfBriefing: db.writeParticipants(): %v\n", err)
|
if err := db.WriteParticipant(p); err != nil {
|
||||||
|
return fmt.Errorf("*DB.WriteAllDataOfBriefing: db.writeParticipants(): %v\n", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.writeGivenAnswers(b, sp, sq, sg); err != nil {
|
for _, p := range *sp {
|
||||||
return fmt.Errorf("*DB.WriteAllDataOfBriefing: db.writeGivenAnswers(): %v\n", err)
|
for i, q := range *sq {
|
||||||
|
db.WriteGivenAnswer(b, p, q, (*sg)[i])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -320,22 +307,3 @@ func (db *DB) GetInstructors() (*[]*types.Instructor, error) {
|
|||||||
|
|
||||||
return &instructors, nil
|
return &instructors, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) WriteParticipant(p *types.Participant) error {
|
|
||||||
result, err := db.Exec(`
|
|
||||||
INSERT INTO participants
|
|
||||||
(first_name, last_name, company)
|
|
||||||
VALUES
|
|
||||||
(?, ?, ?)
|
|
||||||
`, p.FirstName, p.LastName, p.Company)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("*DB.writeParticipant: db.Exec(): %v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
p.ID, err = result.LastInsertId()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("*DB.writeParticipant: result.LastInsertId(): %v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
@ -7,19 +7,18 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
||||||
"streifling.com/jason/sicherheitsunterweisung/packages/types"
|
"streifling.com/jason/sicherheitsunterweisung/packages/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type questionData struct {
|
// type questionData struct {
|
||||||
ID int64
|
// ID int64
|
||||||
Q types.Question
|
// Q types.Question
|
||||||
I int
|
// I int
|
||||||
J int
|
// J int
|
||||||
}
|
// }
|
||||||
|
|
||||||
func DisplayTable(db *data.DB) http.HandlerFunc {
|
func DisplayTable(db *data.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -48,7 +47,7 @@ func DisplayInstructorForm() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func generateUUID() (string, error) {
|
func generateUUID() (string, error) {
|
||||||
bs := make([]byte, 2)
|
bs := make([]byte, 4)
|
||||||
|
|
||||||
if _, err := rand.Read(bs); err != nil {
|
if _, err := rand.Read(bs); err != nil {
|
||||||
return "", fmt.Errorf("GenerateUUID: rand.Read(bs): %v\n", err)
|
return "", fmt.Errorf("GenerateUUID: rand.Read(bs): %v\n", err)
|
||||||
@ -71,7 +70,7 @@ func AddParticipant(sl *[]string) http.HandlerFunc {
|
|||||||
|
|
||||||
// TODO: Hier weiter machen, irgendwie die b.ID herausgeben,
|
// TODO: Hier weiter machen, irgendwie die b.ID herausgeben,
|
||||||
// am besten hier auch die p.IDs rausgeben, damit diese später verknüpft werden können
|
// am besten hier auch die p.IDs rausgeben, damit diese später verknüpft werden können
|
||||||
func SubmitBriefingForm(sb *[]*types.Briefing) http.HandlerFunc {
|
func SubmitBriefingForm(db *data.DB, sl *[]string) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
briefing := new(types.Briefing)
|
briefing := new(types.Briefing)
|
||||||
@ -86,7 +85,8 @@ func SubmitBriefingForm(sb *[]*types.Briefing) http.HandlerFunc {
|
|||||||
briefing.DocumentName = r.PostFormValue("document") // TODO: in HTML einfügen
|
briefing.DocumentName = r.PostFormValue("document") // TODO: in HTML einfügen
|
||||||
briefing.AsOf = r.PostFormValue("state") // TODO: Umbenennen
|
briefing.AsOf = r.PostFormValue("state") // TODO: Umbenennen
|
||||||
// briefing.InstructorID = r.PostFormValue("instructor-id") // TODO: aus Dropdown holen
|
// briefing.InstructorID = r.PostFormValue("instructor-id") // TODO: aus Dropdown holen
|
||||||
(*sb) = append(*sb, briefing)
|
|
||||||
|
db.WriteBriefing(briefing)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,7 +105,7 @@ func newParticipant(l string) (*types.Participant, error) {
|
|||||||
var err error
|
var err error
|
||||||
p := new(types.Participant)
|
p := new(types.Participant)
|
||||||
|
|
||||||
p.ID, err = strconv.ParseInt(strings.Split(l, "-")[0], 10, 64)
|
p.ID, err = strconv.ParseInt(l, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("newParticipant: strconv.Atoi(idString): %v\n", err)
|
return nil, fmt.Errorf("newParticipant: strconv.Atoi(idString): %v\n", err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user