250 lines
7.0 KiB
Go
250 lines
7.0 KiB
Go
package server
|
|
|
|
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"
|
|
)
|
|
|
|
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 DisplayTable(db *db.DB) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
displayTable(w, db)
|
|
}
|
|
}
|
|
|
|
func DisplaySearchResults(db *db.DB) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
bs, err := db.GetOverviewTableDataByName(r.PostFormValue("search"))
|
|
if err != nil {
|
|
http.Error(w, "DisplayResults: db.ReadByName(r.PostFormValue()): "+fmt.Sprint(err), http.StatusInternalServerError)
|
|
}
|
|
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "rows", bs)
|
|
}
|
|
}
|
|
|
|
func DisplayInstructorForm(db *db.DB, cs chan<- *types.Session) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
type option struct {
|
|
ID int64
|
|
String string
|
|
}
|
|
|
|
type htmlData struct {
|
|
SessionID uuid.UUID
|
|
Options []option
|
|
}
|
|
|
|
session := new(types.Session)
|
|
session.ID = uuid.New()
|
|
cs <- session
|
|
|
|
instructors, err := db.GetInstructors()
|
|
if err != nil {
|
|
http.Error(w, "DisplayInstructorForm: db.GetInstructors(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
|
log.Panicln(err)
|
|
}
|
|
|
|
data := new(htmlData)
|
|
data.SessionID = session.ID
|
|
for _, instructor := range instructors {
|
|
option := new(option)
|
|
option.ID = instructor.ID
|
|
option.String = instructor.LastName + ", " + instructor.FirstName + ": " + fmt.Sprint(instructor.PersonnelID)
|
|
data.Options = append(data.Options, *option)
|
|
}
|
|
|
|
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", data)
|
|
}
|
|
}
|
|
|
|
func generateUUID() (string, error) {
|
|
bs := make([]byte, 4)
|
|
|
|
if _, err := rand.Read(bs); err != nil {
|
|
return "", fmt.Errorf("GenerateUUID: rand.Read(bs): %v\n", err)
|
|
}
|
|
|
|
return hex.EncodeToString(bs), nil
|
|
}
|
|
|
|
func AddParticipant(s *types.Session) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
type httpData struct {
|
|
SessionID uuid.UUID
|
|
Login string
|
|
}
|
|
|
|
data := new(httpData)
|
|
var err error
|
|
|
|
data.SessionID = s.ID
|
|
data.Login, err = generateUUID()
|
|
if err != nil {
|
|
http.Error(w, "AddParticipant: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
|
}
|
|
|
|
s.Logins = append(s.Logins, data.Login)
|
|
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", data)
|
|
}
|
|
}
|
|
|
|
func SubmitBriefingForm(s *types.Session, db *db.DB) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
now := time.Now()
|
|
briefing := new(types.Briefing)
|
|
var err error
|
|
|
|
briefing.Date = now.Format("2006-01-02")
|
|
briefing.Time = now.Format("15:04:05")
|
|
briefing.Location = r.PostFormValue("location")
|
|
briefing.DocumentName = r.PostFormValue("document-name")
|
|
briefing.AsOf = r.PostFormValue("as-of")
|
|
|
|
briefing.InstructorID, err = strconv.ParseInt(r.PostFormValue("instructor"), 10, 64)
|
|
if err != nil {
|
|
http.Error(w, "SubmitBriefingForm: strconv.ParseInt(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
|
log.Panicln(err)
|
|
}
|
|
|
|
err = db.WriteBriefing(briefing)
|
|
if err != nil {
|
|
http.Error(w, "SubmitBriefingForm: db.WriteBriefing(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
|
log.Panicln(err)
|
|
}
|
|
|
|
s.BriefingID = briefing.ID
|
|
s.InstructorID = briefing.InstructorID
|
|
|
|
displayTable(w, db)
|
|
}
|
|
}
|
|
|
|
// TODO: Make it only serve one purpose
|
|
func loginIsCorrect(l string, ss []*types.Session) bool {
|
|
for _, session := range ss {
|
|
for i, v := range session.Logins {
|
|
if l == v {
|
|
session.Logins = append(session.Logins[:i], session.Logins[i+1:]...)
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return 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 DisplayParticipantForm(ss []*types.Session) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
type httpData struct {
|
|
SessionID uuid.UUID
|
|
UUID string
|
|
}
|
|
|
|
if loginIsCorrect(r.PostFormValue("login"), ss) {
|
|
data := new(httpData)
|
|
var err error
|
|
|
|
data.UUID, err = generateUUID()
|
|
if err != nil {
|
|
http.Error(w, "DisplayParticipantForm: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
|
}
|
|
|
|
template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", data)
|
|
} else {
|
|
template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
|
|
}
|
|
}
|
|
}
|
|
|
|
// func readAnswer(r *http.Request, p *types.Participant, i int) error {
|
|
// v, err := strconv.Atoi(r.PostFormValue("answer"))
|
|
// if err != nil {
|
|
// return fmt.Errorf("readAnswer: strconv.Atoi(): %v\n", err)
|
|
// }
|
|
//
|
|
// p.Questions[i].Chosen = v
|
|
//
|
|
// return nil
|
|
// }
|
|
//
|
|
// func DisplayQuestion(i int, p *types.Participant) http.HandlerFunc {
|
|
// return func(w http.ResponseWriter, r *http.Request) {
|
|
// if i == 0 {
|
|
// p.FirstName = r.PostFormValue("participant-first-" + fmt.Sprintf("%d", p.ID))
|
|
// p.LastName = r.PostFormValue("participant-last-" + fmt.Sprintf("%d", p.ID))
|
|
// p.Company = r.PostFormValue("participant-company-" + fmt.Sprintf("%d", p.ID))
|
|
// } else {
|
|
// if err := readAnswer(r, p, i-1); err != nil {
|
|
// http.Error(w, "DisplayQuestion: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError)
|
|
// }
|
|
// }
|
|
//
|
|
// data := new(questionData)
|
|
// data.ID = p.ID
|
|
// data.Q = p.Questions[i]
|
|
// data.I = i
|
|
// data.J = i + 1
|
|
//
|
|
// template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
|
// }
|
|
// }
|
|
//
|
|
// func DisplayTestResults(b *types.Briefing, p *types.Participant) http.HandlerFunc {
|
|
// return func(w http.ResponseWriter, r *http.Request) {
|
|
// numQuestions := len(p.Questions)
|
|
// wrongAnswers := make([]int, 0)
|
|
// fmt.Println(wrongAnswers)
|
|
//
|
|
// if err := readAnswer(r, p, numQuestions-1); err != nil {
|
|
// http.Error(w, "DisplayTestResults: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError)
|
|
// }
|
|
//
|
|
// for i, q := range p.Questions {
|
|
// if q.Chosen != q.Correct {
|
|
// wrongAnswers = append(wrongAnswers, i)
|
|
// }
|
|
// }
|
|
//
|
|
// if wrongAnswers == nil {
|
|
// b.Participants = append(b.Participants, p)
|
|
// } else {
|
|
// data := new(questionData)
|
|
// data.ID = p.ID
|
|
// data.Q = p.Questions[0]
|
|
// data.I = 0
|
|
// data.J = data.I + 1
|
|
// template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
|
// }
|
|
//
|
|
// template.Must(template.ParseFiles("templates/results.html")).ExecuteTemplate(w, "content", nil)
|
|
// }
|
|
// }
|