Der Umstieg auf Sessions ist weitgehend geglückt

This commit is contained in:
2023-10-28 08:01:34 +02:00
parent aded71394d
commit 5049db064c
11 changed files with 396 additions and 225 deletions

View File

@ -23,13 +23,32 @@ func displayTable(w http.ResponseWriter, db *db.DB) {
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", bs)
}
func DisplayTable(db *db.DB) http.HandlerFunc {
func HandleInternalLogin(ss *[]*types.Session, cs chan<- *types.Session, db *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
displayTable(w, db)
instructors, err := db.GetInstructors()
if err != nil {
http.Error(w, "HandleInternalLogin: db.GetInstructors(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err)
}
for _, i := range instructors {
if r.PostFormValue("login") == fmt.Sprint(i.ID) {
session := new(types.Session)
session.ID = uuid.New()
session.Briefing = new(types.Briefing)
session.Briefing.InstructorID = i.ID
(*ss) = append((*ss), session)
cs <- session
displayTable(w, db)
return
}
}
template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
}
}
func DisplaySearchResults(db *db.DB) http.HandlerFunc {
func HandleSearch(db *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
bs, err := db.GetOverviewTableDataByName(r.PostFormValue("search"))
if err != nil {
@ -39,52 +58,30 @@ func DisplaySearchResults(db *db.DB) http.HandlerFunc {
}
}
func DisplayInstructorForm(db *db.DB, cs chan<- *types.Session) http.HandlerFunc {
func HandleNewBriefing(s *types.Session) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
type option struct {
ID int64
String string
}
type htmlData struct {
type httpData 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)
}
data := new(httpData)
data.SessionID = s.ID
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", data)
}
}
func generateUUID() (string, error) {
func generateLogin() (string, error) {
bs := make([]byte, 4)
if _, err := rand.Read(bs); err != nil {
return "", fmt.Errorf("GenerateUUID: rand.Read(bs): %v\n", err)
return "", fmt.Errorf("generateLogin: rand.Read(bs): %v\n", err)
}
return hex.EncodeToString(bs), nil
}
func AddParticipant(s *types.Session) http.HandlerFunc {
func HandleNewParticipant(s *types.Session, cp chan<- *types.Participant) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
type httpData struct {
SessionID uuid.UUID
@ -94,59 +91,55 @@ func AddParticipant(s *types.Session) http.HandlerFunc {
data := new(httpData)
var err error
data.SessionID = s.ID
data.Login, err = generateUUID()
p := new(types.Participant)
p.Login, err = generateLogin()
if err != nil {
http.Error(w, "AddParticipant: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError)
http.Error(w, "AddParticipant: generateLogin(): "+fmt.Sprint(err), http.StatusInternalServerError)
}
s.Participants = append(s.Participants, p)
cp <- p
data.SessionID = s.ID
data.Login = p.Login
if err != nil {
http.Error(w, "AddParticipant: generateLogin(): "+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 {
func HandleBriefingForm(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")
s.Briefing.Date = now.Format("2006-01-02")
s.Briefing.Time = now.Format("15:04:05")
s.Briefing.Location = r.PostFormValue("location")
s.Briefing.DocumentName = r.PostFormValue("document")
s.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)
err = db.WriteBriefing(s.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
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 false
return nil, nil, false
}
func newParticipant(l string) (*types.Participant, error) {
@ -161,21 +154,18 @@ func newParticipant(l string) (*types.Participant, error) {
return p, nil
}
func DisplayParticipantForm(ss []*types.Session) http.HandlerFunc {
func HandleExternalLogin(ss *[]*types.Session) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
type httpData struct {
SessionID uuid.UUID
UUID string
Login string
}
if loginIsCorrect(r.PostFormValue("login"), ss) {
session, participant, loginCorrect := findCorrectLogin(r.PostFormValue("login"), ss)
if loginCorrect {
data := new(httpData)
var err error
data.UUID, err = generateUUID()
if err != nil {
http.Error(w, "DisplayParticipantForm: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError)
}
data.SessionID = session.ID
data.Login = participant.Login
template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", data)
} else {
@ -184,66 +174,156 @@ func DisplayParticipantForm(ss []*types.Session) http.HandlerFunc {
}
}
// 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)
// }
// }
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 {
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))
err := db.WriteParticipant(p)
if err != nil {
http.Error(w, "DisplayQuestion: db.WriteParticipant(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err)
}
data := new(httpData)
data.SessionID = s.ID
data.Login = p.Login
data.Question = (*sq)[0]
data.QuestionID = 1
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
}
}
func HandleAnswer(s *types.Session, db *db.DB, p *types.Participant, sq *[]types.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.SessionID = s.ID
data.Login = p.Login
data.Question = (*sq)[i]
data.QuestionID = i + 1
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)
}
givenAnswers, err := db.GetGivenAnswers(s.Briefing.ID, p.ID, s.Questions)
if err != nil {
http.Error(w, "DisplayTestResults: db.GetGivenAnswers(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err)
}
data := new(httpData)
data.SessionID = s.ID
data.Login = p.Login
data.Incorrect = 0
data.Questions = make([]question, 0)
for i, q := range s.Questions {
question := new(question)
question.Text = q.Text
question.Answers = make([]answer, 0)
for j, a := range q.Answers {
answer := new(answer)
answer.Text = a.Text
if j+1 == q.Correct {
answer.Correct = true
} else {
answer.Correct = false
}
if j+1 == givenAnswers[i] {
answer.Chosen = true
} else {
answer.Chosen = false
}
question.Answers = append(question.Answers, *answer)
}
data.Questions = append(data.Questions, *question)
if givenAnswers[i] != q.Correct {
data.Incorrect++
}
}
template.Must(template.ParseFiles("templates/results.html")).ExecuteTemplate(w, "content", data)
}
}
}
func HandleRetry(s *types.Session, p *types.Participant, sq *[]types.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.SessionID = s.ID
data.Login = p.Login
data.Question = (*sq)[0]
data.QuestionID = 1
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
}
}