Kleine Änderungen für Sessions

This commit is contained in:
Jason Streifling 2023-10-26 10:05:21 +02:00
parent d054b3644b
commit aded71394d
2 changed files with 54 additions and 31 deletions

View File

@ -15,13 +15,17 @@ import (
"streifling.com/jason/sicherheitsunterweisung/packages/types" "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 { func DisplayTable(db *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
bs, err := db.GetAllOverviewTableData() displayTable(w, db)
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)
} }
} }
@ -82,36 +86,53 @@ func generateUUID() (string, error) {
func AddParticipant(s *types.Session) http.HandlerFunc { func AddParticipant(s *types.Session) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
login, err := generateUUID() 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 { if err != nil {
http.Error(w, "AddParticipant: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError) http.Error(w, "AddParticipant: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError)
} }
s.Logins = append(s.Logins, login) s.Logins = append(s.Logins, data.Login)
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", login) template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", data)
} }
} }
// 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(s *types.Session, db *db.DB) http.HandlerFunc { func SubmitBriefingForm(s *types.Session, db *db.DB) 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)
var err error
// TODO: Dropdownmenü
// instructorFirstName := r.PostFormValue("instructor-first")
// instructorLastName := r.PostFormValue("instructor-last")
briefing.Date = now.Format("2006-01-02") briefing.Date = now.Format("2006-01-02")
briefing.Time = now.Format("15:04:05") briefing.Time = now.Format("15:04:05")
briefing.Location = r.PostFormValue("location") briefing.Location = r.PostFormValue("location")
briefing.DocumentName = r.PostFormValue("document") // TODO: in HTML einfügen briefing.DocumentName = r.PostFormValue("document-name")
briefing.AsOf = r.PostFormValue("state") // TODO: Umbenennen briefing.AsOf = r.PostFormValue("as-of")
// briefing.InstructorID = r.PostFormValue("instructor-id") // TODO: aus Dropdown holen
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)
}
db.WriteBriefing(briefing)
s.BriefingID = briefing.ID s.BriefingID = briefing.ID
s.InstructorID = briefing.InstructorID
displayTable(w, db)
} }
} }
@ -142,13 +163,13 @@ func newParticipant(l string) (*types.Participant, error) {
func DisplayParticipantForm(ss []*types.Session) http.HandlerFunc { func DisplayParticipantForm(ss []*types.Session) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
type data struct { type httpData struct {
SessionID uuid.UUID SessionID uuid.UUID
UUID string UUID string
} }
if loginIsCorrect(r.PostFormValue("login"), ss) { if loginIsCorrect(r.PostFormValue("login"), ss) {
data := new(data) data := new(httpData)
var err error var err error
data.UUID, err = generateUUID() data.UUID, err = generateUUID()

View File

@ -1,12 +1,18 @@
{{ define "add-button" }} {{ define "add-buttons" }}
<button type="button" hx-post="/add-participant/" hx-target="this" hx-swap="outerHTML"> <div id="briefing-buttons">
Neuer Teilnehmer <button type="button" hx-post="/add-participant-{{ .SessionID }}/" hx-target="#briefing-buttons" hx-swap="outerHTML">
</button> Neuer Teilnehmer
</button>
<button type="submit" hx-post="/submit-form-{{ .SessionID }}/" hx-target="#content" hx-swap="innerHTML">
Fertig
</button>
</div>
{{ end }} {{ end }}
{{ define "new" }} {{ define "new" }}
<span>{{ . }}</span> {{ template "add-buttons" . }}
{{ template "add-button" . }} <p>{{ .Login }}</p>
{{ end }} {{ end }}
{{ define "content" }} {{ define "content" }}
@ -35,10 +41,6 @@
<input type="date" name="as-of" id="as-of" /> <input type="date" name="as-of" id="as-of" />
</div> </div>
{{ template "add-button" . }} {{ template "add-buttons" . }}
<button type="submit" hx-post="/submit-form-{{ .SessionID }}/" hx-target="#content" hx-swap="innerHTML">
OK
</button>
</form> </form>
{{ end }} {{ end }}