Im Grunde nach MVC umstrukturiert und Funktionen in Handlers, die eine Session als Parameter erhalten haben zu Sessionmethoden umgewandelt
This commit is contained in:
parent
5049db064c
commit
c7bb630043
43
main.go
43
main.go
@ -6,25 +6,24 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/db"
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/server"
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/types"
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/session"
|
||||
)
|
||||
|
||||
func handleParticipants(mux *http.ServeMux, db *db.DB, cp <-chan *types.Participant, s *types.Session) {
|
||||
func handleParticipants(mux *http.ServeMux, db *data.DB, cp <-chan *data.Participant, s *session.Session) {
|
||||
for participant := range cp {
|
||||
mux.HandleFunc("/submit-participant/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/", server.HandleParticipant(s, participant, &s.Questions, db))
|
||||
mux.HandleFunc("/submit-participant/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/", s.HandleParticipant(participant, &s.Questions, db))
|
||||
for i := range s.Questions {
|
||||
mux.HandleFunc("/submit-answer/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/"+fmt.Sprint(i+1)+"/", server.HandleAnswer(s, db, participant, &s.Questions, int64(i+1)))
|
||||
mux.HandleFunc("/submit-answer/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/"+fmt.Sprint(i+1)+"/", s.HandleAnswer(db, participant, &s.Questions, int64(i+1)))
|
||||
}
|
||||
mux.HandleFunc("/retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/", server.HandleRetry(s, participant, &s.Questions))
|
||||
mux.HandleFunc("/retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/", s.HandleRetry(participant, &s.Questions))
|
||||
}
|
||||
}
|
||||
|
||||
func handleSessions(mux *http.ServeMux, db *db.DB, cs <-chan *types.Session, ss *[]*types.Session) {
|
||||
for session := range cs {
|
||||
(*ss) = append(*ss, session)
|
||||
participantChan := make(chan *types.Participant)
|
||||
func handleSessions(mux *http.ServeMux, db *data.DB, cs <-chan *session.Session, ss *[]*session.Session) {
|
||||
for s := range cs {
|
||||
(*ss) = append(*ss, s)
|
||||
participantChan := make(chan *data.Participant)
|
||||
questionIDs := make([]string, 4)
|
||||
|
||||
for i := 0; i < len(questionIDs); i++ {
|
||||
@ -32,36 +31,36 @@ func handleSessions(mux *http.ServeMux, db *db.DB, cs <-chan *types.Session, ss
|
||||
}
|
||||
|
||||
var err error
|
||||
session.Questions, err = db.GetQuestions(questionIDs)
|
||||
s.Questions, err = db.GetQuestions(questionIDs)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
mux.HandleFunc("/new-briefing/", server.HandleNewBriefing(session))
|
||||
mux.HandleFunc("/new-participant/"+fmt.Sprint(session.ID)+"/", server.HandleNewParticipant(session, participantChan))
|
||||
mux.HandleFunc("/submit-form/"+fmt.Sprint(session.ID)+"/", server.HandleBriefingForm(session, db))
|
||||
mux.HandleFunc("/new-briefing/", s.HandleNewBriefing())
|
||||
mux.HandleFunc("/new-participant/"+fmt.Sprint(s.ID)+"/", s.HandleNewParticipant(participantChan))
|
||||
mux.HandleFunc("/submit-form/"+fmt.Sprint(s.ID)+"/", s.HandleBriefingForm(db))
|
||||
|
||||
go handleParticipants(mux, db, participantChan, session)
|
||||
go handleParticipants(mux, db, participantChan, s)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
db, err := db.Open("sicherheitsunterweisung")
|
||||
db, err := data.OpenDB("sicherheitsunterweisung")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
sessions := make([]*types.Session, 0)
|
||||
sessionChan := make(chan *types.Session)
|
||||
sessions := make([]*session.Session, 0)
|
||||
sessionChan := make(chan *session.Session)
|
||||
|
||||
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", "templates/login.html")).Execute(w, nil)
|
||||
})
|
||||
mux.HandleFunc("/internal-login/", server.HandleInternalLogin(&sessions, sessionChan, db))
|
||||
mux.HandleFunc("/external-login/", server.HandleExternalLogin(&sessions))
|
||||
mux.HandleFunc("/search/", server.HandleSearch(db))
|
||||
mux.HandleFunc("/internal-login/", session.HandleInternalLogin(&sessions, sessionChan, db))
|
||||
mux.HandleFunc("/external-login/", session.HandleExternalLogin(&sessions))
|
||||
mux.HandleFunc("/search/", session.HandleSearch(db))
|
||||
|
||||
go handleSessions(mux, db, sessionChan, &sessions)
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
package db
|
||||
package data
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
@ -10,7 +10,6 @@ import (
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
"golang.org/x/term"
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/types"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
@ -59,7 +58,7 @@ func getCredentials() (string, string, error) {
|
||||
return user, pass, nil
|
||||
}
|
||||
|
||||
func Open(dbName string) (*DB, error) {
|
||||
func OpenDB(dbName string) (*DB, error) {
|
||||
var err error
|
||||
db := new(DB)
|
||||
|
||||
@ -82,7 +81,7 @@ func Open(dbName string) (*DB, error) {
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func (db *DB) WriteBriefing(b *types.Briefing) error {
|
||||
func (db *DB) WriteBriefing(b *Briefing) error {
|
||||
result, err := db.Exec(`
|
||||
INSERT INTO briefings
|
||||
(date, time, location, document_name, as_of, instructor_id)
|
||||
@ -101,7 +100,7 @@ func (db *DB) WriteBriefing(b *types.Briefing) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) WriteParticipant(p *types.Participant) error {
|
||||
func (db *DB) WriteParticipant(p *Participant) error {
|
||||
result, err := db.Exec(`
|
||||
INSERT INTO participants
|
||||
(first_name, last_name, company)
|
||||
@ -120,7 +119,7 @@ func (db *DB) WriteParticipant(p *types.Participant) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) WriteGivenAnswer(b *types.Briefing, p *types.Participant, q *types.Question, g int) error {
|
||||
func (db *DB) WriteGivenAnswer(b *Briefing, p *Participant, q *Question, g int) error {
|
||||
_, err := db.Exec(`
|
||||
INSERT INTO given_answers
|
||||
(briefing_id, participant_id, question_id, given_answer)
|
||||
@ -134,7 +133,7 @@ func (db *DB) WriteGivenAnswer(b *types.Briefing, p *types.Participant, q *types
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) WriteAllDataOfBriefing(b *types.Briefing, sp *[]*types.Participant, sq *[]*types.Question, sg *[]*types.GivenAnswer) error {
|
||||
func (db *DB) WriteAllDataOfBriefing(b *Briefing, sp *[]*Participant, sq *[]*Question, sg *[]*GivenAnswer) error {
|
||||
if err := db.WriteBriefing(b); err != nil {
|
||||
return fmt.Errorf("*DB.WriteAllDataOfBriefing: db.writeBriefing(): %v\n", err)
|
||||
}
|
||||
@ -154,7 +153,7 @@ func (db *DB) WriteAllDataOfBriefing(b *types.Briefing, sp *[]*types.Participant
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) GetAllOverviewTableData() ([]*types.OverviewTableData, error) {
|
||||
func (db *DB) GetAllOverviewTableData() ([]*OverviewTableData, error) {
|
||||
rows, err := db.Query(`
|
||||
SELECT
|
||||
i.first_name,
|
||||
@ -187,9 +186,9 @@ func (db *DB) GetAllOverviewTableData() ([]*types.OverviewTableData, error) {
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
data := make([]*types.OverviewTableData, 0)
|
||||
data := make([]*OverviewTableData, 0)
|
||||
for rows.Next() {
|
||||
otd := new(types.OverviewTableData)
|
||||
otd := new(OverviewTableData)
|
||||
|
||||
err := rows.Scan(
|
||||
&otd.InstructorFirstName,
|
||||
@ -213,7 +212,7 @@ func (db *DB) GetAllOverviewTableData() ([]*types.OverviewTableData, error) {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (db *DB) GetOverviewTableDataByName(n string) (*[]*types.OverviewTableData, error) {
|
||||
func (db *DB) GetOverviewTableDataByName(n string) (*[]*OverviewTableData, error) {
|
||||
rows, err := db.Query(`
|
||||
SELECT
|
||||
i.first_name,
|
||||
@ -248,9 +247,9 @@ func (db *DB) GetOverviewTableDataByName(n string) (*[]*types.OverviewTableData,
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
data := make([]*types.OverviewTableData, 0)
|
||||
data := make([]*OverviewTableData, 0)
|
||||
for rows.Next() {
|
||||
otd := new(types.OverviewTableData)
|
||||
otd := new(OverviewTableData)
|
||||
|
||||
err := rows.Scan(
|
||||
&otd.InstructorFirstName,
|
||||
@ -291,7 +290,7 @@ func (db *DB) GetLastID(table string) (int, error) {
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (db *DB) GetInstructors() ([]*types.Instructor, error) {
|
||||
func (db *DB) GetInstructors() ([]*Instructor, error) {
|
||||
rows, err := db.Query(`
|
||||
SELECT *
|
||||
FROM instructors
|
||||
@ -304,9 +303,9 @@ func (db *DB) GetInstructors() ([]*types.Instructor, error) {
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
instructors := make([]*types.Instructor, 0)
|
||||
instructors := make([]*Instructor, 0)
|
||||
for rows.Next() {
|
||||
instructor := new(types.Instructor)
|
||||
instructor := new(Instructor)
|
||||
if err = rows.Scan(&instructor.ID, &instructor.FirstName, &instructor.LastName); err != nil {
|
||||
return nil, fmt.Errorf("*DB.GetInstructors: rows.Scan(): %v\n", err)
|
||||
}
|
||||
@ -316,7 +315,7 @@ func (db *DB) GetInstructors() ([]*types.Instructor, error) {
|
||||
return instructors, nil
|
||||
}
|
||||
|
||||
func (db *DB) GetQuestions(nums []string) ([]types.Question, error) {
|
||||
func (db *DB) GetQuestions(nums []string) ([]Question, error) {
|
||||
rows, err := db.Query(`
|
||||
SELECT *
|
||||
FROM questions
|
||||
@ -327,13 +326,13 @@ func (db *DB) GetQuestions(nums []string) ([]types.Question, error) {
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
questions := make([]types.Question, 0)
|
||||
questions := make([]Question, 0)
|
||||
for rows.Next() {
|
||||
q := new(types.Question)
|
||||
a1 := new(types.Answer)
|
||||
a2 := new(types.Answer)
|
||||
a3 := new(types.Answer)
|
||||
a4 := new(types.Answer)
|
||||
q := new(Question)
|
||||
a1 := new(Answer)
|
||||
a2 := new(Answer)
|
||||
a3 := new(Answer)
|
||||
a4 := new(Answer)
|
||||
|
||||
a1.ID = 1
|
||||
a2.ID = 2
|
||||
@ -355,7 +354,7 @@ func (db *DB) GetQuestions(nums []string) ([]types.Question, error) {
|
||||
return questions, nil
|
||||
}
|
||||
|
||||
func (db *DB) GetGivenAnswers(bid, pid int64, sq []types.Question) ([]int, error) {
|
||||
func (db *DB) GetGivenAnswers(bid, pid int64, sq []Question) ([]int, error) {
|
||||
answers := make([]int, 0)
|
||||
query := `
|
||||
SELECT given_answer
|
@ -1,6 +1,4 @@
|
||||
package types
|
||||
|
||||
import "github.com/google/uuid"
|
||||
package data
|
||||
|
||||
type Person struct {
|
||||
ID int64
|
||||
@ -57,10 +55,3 @@ type OverviewTableData struct {
|
||||
ParticipantLastName string
|
||||
ParticipantCompany string
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
ID uuid.UUID
|
||||
*Briefing
|
||||
Participants []*Participant
|
||||
Questions []Question
|
||||
}
|
@ -1,29 +1,17 @@
|
||||
package server
|
||||
package session
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/db"
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/types"
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
||||
)
|
||||
|
||||
func displayTable(w http.ResponseWriter, db *db.DB) {
|
||||
bs, err := db.GetAllOverviewTableData()
|
||||
if err != nil {
|
||||
http.Error(w, "displayTable: *DB.GetAllOverviewTableData(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
}
|
||||
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", bs)
|
||||
}
|
||||
|
||||
func HandleInternalLogin(ss *[]*types.Session, cs chan<- *types.Session, db *db.DB) http.HandlerFunc {
|
||||
func HandleInternalLogin(ss *[]*Session, cs chan<- *Session, db *data.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
instructors, err := db.GetInstructors()
|
||||
if err != nil {
|
||||
@ -33,9 +21,9 @@ func HandleInternalLogin(ss *[]*types.Session, cs chan<- *types.Session, db *db.
|
||||
|
||||
for _, i := range instructors {
|
||||
if r.PostFormValue("login") == fmt.Sprint(i.ID) {
|
||||
session := new(types.Session)
|
||||
session := new(Session)
|
||||
session.ID = uuid.New()
|
||||
session.Briefing = new(types.Briefing)
|
||||
session.Briefing = new(data.Briefing)
|
||||
session.Briefing.InstructorID = i.ID
|
||||
(*ss) = append((*ss), session)
|
||||
cs <- session
|
||||
@ -48,7 +36,7 @@ func HandleInternalLogin(ss *[]*types.Session, cs chan<- *types.Session, db *db.
|
||||
}
|
||||
}
|
||||
|
||||
func HandleSearch(db *db.DB) http.HandlerFunc {
|
||||
func HandleSearch(db *data.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
bs, err := db.GetOverviewTableDataByName(r.PostFormValue("search"))
|
||||
if err != nil {
|
||||
@ -58,40 +46,18 @@ func HandleSearch(db *db.DB) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func HandleNewBriefing(s *types.Session) http.HandlerFunc {
|
||||
func (s *Session) HandleNewBriefing() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
type httpData struct {
|
||||
SessionID uuid.UUID
|
||||
}
|
||||
|
||||
data := new(httpData)
|
||||
data := new(briefingHTMLData)
|
||||
data.SessionID = s.ID
|
||||
|
||||
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", data)
|
||||
}
|
||||
}
|
||||
|
||||
func generateLogin() (string, error) {
|
||||
bs := make([]byte, 4)
|
||||
|
||||
if _, err := rand.Read(bs); err != nil {
|
||||
return "", fmt.Errorf("generateLogin: rand.Read(bs): %v\n", err)
|
||||
}
|
||||
|
||||
return hex.EncodeToString(bs), nil
|
||||
}
|
||||
|
||||
func HandleNewParticipant(s *types.Session, cp chan<- *types.Participant) http.HandlerFunc {
|
||||
func (s *Session) HandleNewParticipant(cp chan<- *data.Participant) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
type httpData struct {
|
||||
SessionID uuid.UUID
|
||||
Login string
|
||||
}
|
||||
|
||||
data := new(httpData)
|
||||
var err error
|
||||
|
||||
p := new(types.Participant)
|
||||
p := new(data.Participant)
|
||||
p.Login, err = generateLogin()
|
||||
if err != nil {
|
||||
http.Error(w, "AddParticipant: generateLogin(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
@ -99,6 +65,7 @@ func HandleNewParticipant(s *types.Session, cp chan<- *types.Participant) http.H
|
||||
s.Participants = append(s.Participants, p)
|
||||
cp <- p
|
||||
|
||||
data := new(briefingHTMLData)
|
||||
data.SessionID = s.ID
|
||||
data.Login = p.Login
|
||||
if err != nil {
|
||||
@ -109,7 +76,7 @@ func HandleNewParticipant(s *types.Session, cp chan<- *types.Participant) http.H
|
||||
}
|
||||
}
|
||||
|
||||
func HandleBriefingForm(s *types.Session, db *db.DB) http.HandlerFunc {
|
||||
func (s *Session) HandleBriefingForm(db *data.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
now := time.Now()
|
||||
var err error
|
||||
@ -130,40 +97,11 @@ func HandleBriefingForm(s *types.Session, db *db.DB) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Make it only serve one purpose
|
||||
func findCorrectLogin(l string, ss *[]*types.Session) (*types.Session, *types.Participant, bool) {
|
||||
for _, session := range *ss {
|
||||
for _, p := range session.Participants {
|
||||
if l == p.Login {
|
||||
return session, p, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, nil, false
|
||||
}
|
||||
|
||||
func newParticipant(l string) (*types.Participant, error) {
|
||||
var err error
|
||||
p := new(types.Participant)
|
||||
|
||||
p.ID, err = strconv.ParseInt(l, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("newParticipant: strconv.Atoi(idString): %v\n", err)
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func HandleExternalLogin(ss *[]*types.Session) http.HandlerFunc {
|
||||
func HandleExternalLogin(ss *[]*Session) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
type httpData struct {
|
||||
SessionID uuid.UUID
|
||||
Login string
|
||||
}
|
||||
|
||||
session, participant, loginCorrect := findCorrectLogin(r.PostFormValue("login"), ss)
|
||||
if loginCorrect {
|
||||
data := new(httpData)
|
||||
data := new(participantHTMLData)
|
||||
data.SessionID = session.ID
|
||||
data.Login = participant.Login
|
||||
|
||||
@ -174,28 +112,8 @@ func HandleExternalLogin(ss *[]*types.Session) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func handleGivenAnswer(s *types.Session, p *types.Participant, i int64, r *http.Request, db *db.DB) error {
|
||||
answer, err := strconv.Atoi(r.PostFormValue("answer"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("handleGivenAnswer: strconv.Atoi(): %v\n", err)
|
||||
}
|
||||
|
||||
if err := db.WriteGivenAnswer(s.Briefing, p, &s.Questions[i], answer); err != nil {
|
||||
return fmt.Errorf("handleGivenAnswer: db.WriteGivenAnswer(): %v\n", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func HandleParticipant(s *types.Session, p *types.Participant, sq *[]types.Question, db *db.DB) http.HandlerFunc {
|
||||
func (s *Session) HandleParticipant(p *data.Participant, sq *[]data.Question, db *data.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
type httpData struct {
|
||||
SessionID uuid.UUID
|
||||
Login string
|
||||
Question types.Question
|
||||
QuestionID int64
|
||||
}
|
||||
|
||||
p.FirstName = r.PostFormValue("first-" + fmt.Sprint(p.Login))
|
||||
p.LastName = r.PostFormValue("last-" + fmt.Sprint(p.Login))
|
||||
p.Company = r.PostFormValue("company-" + fmt.Sprint(p.Login))
|
||||
@ -206,7 +124,7 @@ func HandleParticipant(s *types.Session, p *types.Participant, sq *[]types.Quest
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
data := new(httpData)
|
||||
data := new(questionHTMLData)
|
||||
data.SessionID = s.ID
|
||||
data.Login = p.Login
|
||||
data.Question = (*sq)[0]
|
||||
@ -216,23 +134,16 @@ func HandleParticipant(s *types.Session, p *types.Participant, sq *[]types.Quest
|
||||
}
|
||||
}
|
||||
|
||||
func HandleAnswer(s *types.Session, db *db.DB, p *types.Participant, sq *[]types.Question, i int64) http.HandlerFunc {
|
||||
func (s *Session) HandleAnswer(db *data.DB, p *data.Participant, sq *[]data.Question, i int64) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println(i, len(*sq))
|
||||
if i < int64(len(*sq)) {
|
||||
type httpData struct {
|
||||
SessionID uuid.UUID
|
||||
Login string
|
||||
Question types.Question
|
||||
QuestionID int64
|
||||
}
|
||||
|
||||
if err := handleGivenAnswer(s, p, i-1, r, db); err != nil {
|
||||
http.Error(w, "DisplayQuestion: handleGivenAnswer(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
data := new(httpData)
|
||||
data := new(questionHTMLData)
|
||||
data.SessionID = s.ID
|
||||
data.Login = p.Login
|
||||
data.Question = (*sq)[i]
|
||||
@ -240,24 +151,6 @@ func HandleAnswer(s *types.Session, db *db.DB, p *types.Participant, sq *[]types
|
||||
|
||||
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
||||
} else {
|
||||
type answer struct {
|
||||
Text string
|
||||
Correct bool
|
||||
Chosen bool
|
||||
}
|
||||
|
||||
type question struct {
|
||||
Text string
|
||||
Answers []answer
|
||||
}
|
||||
|
||||
type httpData struct {
|
||||
SessionID uuid.UUID
|
||||
Login string
|
||||
Questions []question
|
||||
Incorrect int
|
||||
}
|
||||
|
||||
if err := handleGivenAnswer(s, p, i-1, r, db); err != nil {
|
||||
http.Error(w, "DisplayTestResults: handleGivenAnswer(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
log.Panicln(err)
|
||||
@ -269,19 +162,19 @@ func HandleAnswer(s *types.Session, db *db.DB, p *types.Participant, sq *[]types
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
data := new(httpData)
|
||||
data := new(resultHTMLData)
|
||||
data.SessionID = s.ID
|
||||
data.Login = p.Login
|
||||
data.Incorrect = 0
|
||||
|
||||
data.Questions = make([]question, 0)
|
||||
data.Questions = make([]htmlQuestion, 0)
|
||||
for i, q := range s.Questions {
|
||||
question := new(question)
|
||||
question := new(htmlQuestion)
|
||||
question.Text = q.Text
|
||||
|
||||
question.Answers = make([]answer, 0)
|
||||
question.Answers = make([]htmlAnswer, 0)
|
||||
for j, a := range q.Answers {
|
||||
answer := new(answer)
|
||||
answer := new(htmlAnswer)
|
||||
answer.Text = a.Text
|
||||
|
||||
if j+1 == q.Correct {
|
||||
@ -304,21 +197,14 @@ func HandleAnswer(s *types.Session, db *db.DB, p *types.Participant, sq *[]types
|
||||
}
|
||||
}
|
||||
|
||||
template.Must(template.ParseFiles("templates/results.html")).ExecuteTemplate(w, "content", data)
|
||||
template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func HandleRetry(s *types.Session, p *types.Participant, sq *[]types.Question) http.HandlerFunc {
|
||||
func (s *Session) HandleRetry(p *data.Participant, sq *[]data.Question) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
type httpData struct {
|
||||
SessionID uuid.UUID
|
||||
Login string
|
||||
Question types.Question
|
||||
QuestionID int64
|
||||
}
|
||||
|
||||
data := new(httpData)
|
||||
data := new(questionHTMLData)
|
||||
data.SessionID = s.ID
|
||||
data.Login = p.Login
|
||||
data.Question = (*sq)[0]
|
66
packages/session/helperFuncs.go
Normal file
66
packages/session/helperFuncs.go
Normal file
@ -0,0 +1,66 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
||||
)
|
||||
|
||||
func displayTable(w http.ResponseWriter, db *data.DB) {
|
||||
bs, err := db.GetAllOverviewTableData()
|
||||
if err != nil {
|
||||
http.Error(w, "displayTable: *DB.GetAllOverviewTableData(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
}
|
||||
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", bs)
|
||||
}
|
||||
|
||||
func generateLogin() (string, error) {
|
||||
bs := make([]byte, 4)
|
||||
|
||||
if _, err := rand.Read(bs); err != nil {
|
||||
return "", fmt.Errorf("generateLogin: rand.Read(bs): %v\n", err)
|
||||
}
|
||||
|
||||
return hex.EncodeToString(bs), nil
|
||||
}
|
||||
|
||||
func findCorrectLogin(l string, ss *[]*Session) (*Session, *data.Participant, bool) {
|
||||
for _, session := range *ss {
|
||||
for _, p := range session.Participants {
|
||||
if l == p.Login {
|
||||
return session, p, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, nil, false
|
||||
}
|
||||
|
||||
func newParticipant(l string) (*data.Participant, error) {
|
||||
var err error
|
||||
p := new(data.Participant)
|
||||
|
||||
p.ID, err = strconv.ParseInt(l, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("newParticipant: strconv.Atoi(idString): %v\n", err)
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func handleGivenAnswer(s *Session, p *data.Participant, i int64, r *http.Request, db *data.DB) error {
|
||||
answer, err := strconv.Atoi(r.PostFormValue("answer"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("handleGivenAnswer: strconv.Atoi(): %v\n", err)
|
||||
}
|
||||
|
||||
if err := db.WriteGivenAnswer(s.Briefing, p, &s.Questions[i], answer); err != nil {
|
||||
return fmt.Errorf("handleGivenAnswer: db.WriteGivenAnswer(): %v\n", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
48
packages/session/htmlStructs.go
Normal file
48
packages/session/htmlStructs.go
Normal file
@ -0,0 +1,48 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
||||
)
|
||||
|
||||
type Session struct {
|
||||
ID uuid.UUID
|
||||
*data.Briefing
|
||||
Participants []*data.Participant
|
||||
Questions []data.Question
|
||||
}
|
||||
|
||||
type briefingHTMLData struct {
|
||||
SessionID uuid.UUID
|
||||
Login string
|
||||
}
|
||||
|
||||
type participantHTMLData struct {
|
||||
SessionID uuid.UUID
|
||||
Login string
|
||||
}
|
||||
|
||||
type questionHTMLData struct {
|
||||
SessionID uuid.UUID
|
||||
Login string
|
||||
Question data.Question
|
||||
QuestionID int64
|
||||
}
|
||||
|
||||
type htmlAnswer struct {
|
||||
Text string
|
||||
Correct bool
|
||||
Chosen bool
|
||||
}
|
||||
|
||||
type htmlQuestion struct {
|
||||
Text string
|
||||
Answers []htmlAnswer
|
||||
}
|
||||
|
||||
type resultHTMLData struct {
|
||||
SessionID uuid.UUID
|
||||
Login string
|
||||
Questions []htmlQuestion
|
||||
Incorrect int
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user