server weitgehend auf Sessions umgestellt

This commit is contained in:
Jason Streifling 2023-10-19 20:05:26 +02:00
parent 35d565ec7d
commit 4de7d36385

View File

@ -9,18 +9,12 @@ import (
"strconv"
"time"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
"github.com/google/uuid"
"streifling.com/jason/sicherheitsunterweisung/packages/db"
"streifling.com/jason/sicherheitsunterweisung/packages/types"
)
// type questionData struct {
// ID int64
// Q types.Question
// I int
// J int
// }
func DisplayTable(db *data.DB) http.HandlerFunc {
func DisplayTable(db *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
bs, err := db.GetAllOverviewTableData()
if err != nil {
@ -30,7 +24,7 @@ func DisplayTable(db *data.DB) http.HandlerFunc {
}
}
func DisplaySearchResults(db *data.DB) http.HandlerFunc {
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 {
@ -40,9 +34,13 @@ func DisplaySearchResults(db *data.DB) http.HandlerFunc {
}
}
func DisplayInstructorForm() http.HandlerFunc {
func DisplayInstructorForm(cs chan<- *types.Session) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", nil)
session := new(types.Session)
session.ID = uuid.New()
cs <- session
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", session.ID)
}
}
@ -56,21 +54,21 @@ func generateUUID() (string, error) {
return hex.EncodeToString(bs), nil
}
func AddParticipant(sl *[]string) http.HandlerFunc {
func AddParticipant(s *types.Session) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
login, err := generateUUID()
if err != nil {
http.Error(w, "AddParticipant: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError)
}
(*sl) = append(*sl, login)
s.Logins = append(s.Logins, login)
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", login)
}
}
// 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
func SubmitBriefingForm(db *data.DB, sl *[]string) http.HandlerFunc {
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)
@ -87,17 +85,20 @@ func SubmitBriefingForm(db *data.DB, sl *[]string) http.HandlerFunc {
// briefing.InstructorID = r.PostFormValue("instructor-id") // TODO: aus Dropdown holen
db.WriteBriefing(briefing)
s.BriefingID = briefing.ID
}
}
// TODO: Make it only serve one purpose
func loginIsCorrect(l string, logins *[]string) bool {
for i, v := range *logins {
func loginIsCorrect(l string, ss []*types.Session) bool {
for _, session := range ss {
for i, v := range session.Logins {
if l == v {
(*logins) = append((*logins)[:i], (*logins)[i+1:]...)
session.Logins = append(session.Logins[:i], session.Logins[i+1:]...)
return true
}
}
}
return false
}
@ -113,15 +114,23 @@ func newParticipant(l string) (*types.Participant, error) {
return p, nil
}
func DisplayParticipantForm(sl *[]string) http.HandlerFunc {
func DisplayParticipantForm(ss []*types.Session) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if loginIsCorrect(r.PostFormValue("login"), sl) {
uuid, err := generateUUID()
type data struct {
SessionID uuid.UUID
UUID string
}
if loginIsCorrect(r.PostFormValue("login"), ss) {
data := new(data)
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", uuid)
template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", data)
} else {
template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
}