Templates verbessert

This commit is contained in:
Gonne 2022-09-07 21:48:40 +02:00
parent 56b4a3ab34
commit e9e0cbf382
11 changed files with 53 additions and 42 deletions

View file

@ -16,10 +16,7 @@ type maskData struct {
MinuteGranularity int MinuteGranularity int
SelectedCourse int SelectedCourse int
SelectedRoom int SelectedRoom int
Week int Date models.Date
Day int
Hour int
Minute int
Duration int Duration int
Roomname string Roomname string
Name string Name string
@ -115,7 +112,6 @@ func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Requ
info := req.FormValue("info") info := req.FormValue("info")
allowed, err := b.officeHourRepo.AllowedAt(date, duration, room, true) allowed, err := b.officeHourRepo.AllowedAt(date, duration, room, true)
fmt.Println(allowed)
if err != nil { if err != nil {
errors = append(errors, fmt.Sprintf("Fehler beim Abfragen der Raumkapazität: %s", err.Error())) errors = append(errors, fmt.Sprintf("Fehler beim Abfragen der Raumkapazität: %s", err.Error()))
} else if !allowed { } else if !allowed {
@ -128,10 +124,7 @@ func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Requ
models.MinuteGranularity, models.MinuteGranularity,
courseid, courseid,
roomid, roomid,
week, date,
day,
hour,
minute,
duration, duration,
roomname, roomname,
name, name,

View file

@ -18,7 +18,14 @@ func (b *BaseHandler) ConfirmRequestHandler(w http.ResponseWriter, req *http.Req
err = b.requestRepo.Execute(request) err = b.requestRepo.Execute(request)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
tmpl, err := template.ParseFiles("templates/executeFailure.html") tmpl, _ := template.ParseFiles("templates/executeFailure.html")
tmpl.Execute(w, err) tmpl.Execute(w, err.Error())
return
} }
tmpl, err := template.ParseFiles("templates/executeSuccess.html")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
tmpl.Execute(w, struct{}{})
} }

View file

@ -50,10 +50,7 @@ func GetTimetable(officeHours []models.OfficeHour) (timetable map[models.Date]ma
func printTimetable(timetable map[models.Date]map[int]models.OfficeHour, slots []int, deleteIcons bool) template.HTML { func printTimetable(timetable map[models.Date]map[int]models.OfficeHour, slots []int, deleteIcons bool) template.HTML {
var tableBody string var tableBody string
tableCell, _ := template.New("td.html"). tableCell, _ := template.New("td.html").Funcs(template.FuncMap{"divide": func(i int, j int) int { return i / j }}).ParseFiles("templates/td.html")
Funcs(template.FuncMap{"mod": func(i int, j int) int { return i % j },
"add": func(i int, j int) int { return i + j },
"divide": func(i int, j int) int { return i / j }}).ParseFiles("templates/td.html")
for hour := 8; hour < 19; hour += 1 { for hour := 8; hour < 19; hour += 1 {
for minute := 0; minute < 60; minute += models.MinuteGranularity { for minute := 0; minute < 60; minute += models.MinuteGranularity {
tableBody += "<tr>" tableBody += "<tr>"

View file

@ -26,5 +26,4 @@ func main() {
http.HandleFunc("/", h.RootHandler) http.HandleFunc("/", h.RootHandler)
http.ListenAndServe(":8080", nil) http.ListenAndServe(":8080", nil)
} }

View file

@ -21,3 +21,7 @@ type OfficeHourRepository interface {
Add(officeHour OfficeHour) (int, error) Add(officeHour OfficeHour) (int, error)
AllowedAt(date Date, duration int, room Room, activeOnly bool) (bool, error) AllowedAt(date Date, duration int, room Room, activeOnly bool) (bool, error)
} }
func (officeHour OfficeHour) EndDate() Date {
return GetEndDate(officeHour.Date, officeHour.Duration, false)
}

View file

@ -3,6 +3,7 @@ package repositories
import ( import (
"database/sql" "database/sql"
"fmt"
"sprechstundentool/models" "sprechstundentool/models"
) )
@ -31,7 +32,7 @@ func (r *OfficeHourRepo) GetAll(activeOnly bool) ([]models.OfficeHour, error) {
rows, err = r.db.Query("SELECT * FROM officeHour") rows, err = r.db.Query("SELECT * FROM officeHour")
} }
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("Error getting all officeHours from database: %s", err.Error())
} }
defer rows.Close() defer rows.Close()
return r.getFromRows(rows) return r.getFromRows(rows)
@ -46,7 +47,7 @@ func (r *OfficeHourRepo) FindByCourse(course models.Course, activeOnly bool) ([]
rows, err = r.db.Query("SELECT * FROM officeHour WHERE course=?", course.Id) rows, err = r.db.Query("SELECT * FROM officeHour WHERE course=?", course.Id)
} }
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("Error getting officeHours by course from database: %s", err.Error())
} }
defer rows.Close() defer rows.Close()
return r.getFromRows(rows) return r.getFromRows(rows)
@ -61,7 +62,7 @@ func (r *OfficeHourRepo) FindByRoom(room models.Room, activeOnly bool) ([]models
rows, err = r.db.Query("SELECT * FROM officeHour WHERE room=?", room.Id) rows, err = r.db.Query("SELECT * FROM officeHour WHERE room=?", room.Id)
} }
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("Error getting officeHours by room from database: %s", err.Error())
} }
defer rows.Close() defer rows.Close()
return r.getFromRows(rows) return r.getFromRows(rows)
@ -92,7 +93,7 @@ func (r *OfficeHourRepo) Add(officeHour models.OfficeHour) (id int, err error) {
officeHour.Info == oldOfficeHour.Info && officeHour.Info == oldOfficeHour.Info &&
officeHour.Active == oldOfficeHour.Active && officeHour.Active == oldOfficeHour.Active &&
officeHour.Duration == oldOfficeHour.Duration { officeHour.Duration == oldOfficeHour.Duration {
return officeHour.Id, nil return oldOfficeHour.Id, nil
} }
} }
@ -113,7 +114,7 @@ func (r *OfficeHourRepo) Add(officeHour models.OfficeHour) (id int, err error) {
func (r *OfficeHourRepo) Delete(officeHour models.OfficeHour) error { func (r *OfficeHourRepo) Delete(officeHour models.OfficeHour) error {
_, err := r.db.Exec("DELETE FROM officeHour WHERE id=?", officeHour.Id) _, err := r.db.Exec("DELETE FROM officeHour WHERE id=?", officeHour.Id)
return err return fmt.Errorf("Error deleting officeHour from database: %s", err.Error())
} }
func (r *OfficeHourRepo) getFromRow(row *sql.Row) (models.OfficeHour, error) { func (r *OfficeHourRepo) getFromRow(row *sql.Row) (models.OfficeHour, error) {
@ -122,7 +123,7 @@ func (r *OfficeHourRepo) getFromRow(row *sql.Row) (models.OfficeHour, error) {
var roomName, courseName string var roomName, courseName string
err := row.Scan(&officeHour.Id, &tutorid, &day, &hour, &minute, &roomName, &courseName, &week, &officeHour.Info, &officeHour.Active, &officeHour.Duration) err := row.Scan(&officeHour.Id, &tutorid, &day, &hour, &minute, &roomName, &courseName, &week, &officeHour.Info, &officeHour.Active, &officeHour.Duration)
if err != nil { if err != nil {
return models.OfficeHour{}, err return models.OfficeHour{}, fmt.Errorf("Error getting single officeHours row from database: %s", err.Error())
} }
officeHour.Date = models.Date{week, day, hour, minute} officeHour.Date = models.Date{week, day, hour, minute}
officeHour.Room, _ = r.roomRepo.FindByName(roomName) officeHour.Room, _ = r.roomRepo.FindByName(roomName)
@ -137,7 +138,7 @@ func (r *OfficeHourRepo) getFromRows(rows *sql.Rows) ([]models.OfficeHour, error
var officeHour models.OfficeHour var officeHour models.OfficeHour
var week, day, hour, minute, tutorId, roomId, courseId int var week, day, hour, minute, tutorId, roomId, courseId int
if err := rows.Scan(&officeHour.Id, &tutorId, &day, &hour, &minute, &roomId, &courseId, &week, &officeHour.Info, &officeHour.Active, &officeHour.Duration); err != nil { if err := rows.Scan(&officeHour.Id, &tutorId, &day, &hour, &minute, &roomId, &courseId, &week, &officeHour.Info, &officeHour.Active, &officeHour.Duration); err != nil {
return officeHours, err return officeHours, fmt.Errorf("Error getting multiple officeHour rows from database: %s", err.Error())
} }
officeHour.Date = models.Date{week, day, hour, minute} officeHour.Date = models.Date{week, day, hour, minute}
officeHour.Room, _ = r.roomRepo.FindById(roomId) officeHour.Room, _ = r.roomRepo.FindById(roomId)
@ -157,7 +158,7 @@ func (r *OfficeHourRepo) NumberByTimeSpanAndRoom(date models.Date, duration int,
rows, err = r.db.Query("SELECT * FROM officeHour WHERE room=?", room.Id) rows, err = r.db.Query("SELECT * FROM officeHour WHERE room=?", room.Id)
} }
if err != nil { if err != nil {
return 0, err return 0, fmt.Errorf("Error getting officeHours by timespan and room from database: %s", err.Error())
} }
defer rows.Close() defer rows.Close()
officeHours, err := r.getFromRows(rows) officeHours, err := r.getFromRows(rows)
@ -182,5 +183,5 @@ func (r *OfficeHourRepo) AllowedAt(date models.Date, duration int, room models.R
if err != nil { if err != nil {
return false, err return false, err
} }
return numberOfOfficeHours >= room.MaxOccupy, nil return numberOfOfficeHours < room.MaxOccupy, nil
} }

View file

@ -81,7 +81,6 @@ func (r *RequestRepo) Add(officeHour models.OfficeHour, action int) (int, error)
if err != nil { if err != nil {
return 0, err return 0, err
} }
request, err = r.FindBySecret(secret) request, err = r.FindBySecret(secret)
if err != nil { if err != nil {
return request.Id, err return request.Id, err

View file

@ -14,27 +14,27 @@
</select><br /> </select><br />
<label for="woche">Woche</label>: <label for="woche">Woche</label>:
<select name="woche"> <select name="woche">
<option value="0"{{if eq 0 $.Week}} selected{{end}}>Jede</option> <option value="0"{{if eq 0 $.Date.Week}} selected{{end}}>Jede</option>
<option value="1"{{if eq 1 $.Week}} selected{{end}}>Gerade</option> <option value="1"{{if eq 1 $.Date.Week}} selected{{end}}>Gerade</option>
<option value="2"{{if eq 2 $.Week}} selected{{end}}>Ungerade</option> <option value="2"{{if eq 2 $.Date.Week}} selected{{end}}>Ungerade</option>
</select><br /> </select><br />
<label for="tag">Tag</label>: <select name="tag"> <label for="tag">Tag</label>: <select name="tag">
<option value="0"{{if eq 0 $.Day}} selected{{end}}>Montag</option> <option value="0"{{if eq 0 $.Date.Day}} selected{{end}}>Montag</option>
<option value="1"{{if eq 1 $.Day}} selected{{end}}>Dienstag</option> <option value="1"{{if eq 1 $.Date.Day}} selected{{end}}>Dienstag</option>
<option value="2"{{if eq 2 $.Day}} selected{{end}}>Mittwoch</option> <option value="2"{{if eq 2 $.Date.Day}} selected{{end}}>Mittwoch</option>
<option value="3"{{if eq 3 $.Day}} selected{{end}}>Donnerstag</option> <option value="3"{{if eq 3 $.Date.Day}} selected{{end}}>Donnerstag</option>
<option value="4"{{if eq 4 $.Day}} selected{{end}}>Freitag</option> <option value="4"{{if eq 4 $.Date.Day}} selected{{end}}>Freitag</option>
</select><br /> </select><br />
<label for="startzeit">Startzeit</label>: <input type="time" name="startzeit" min="08:00" max="17:30" /><br /> <label for="startzeit">Startzeit</label>: <input type="time" name="startzeit" min="08:00" max="17:30" {{if gt $.Date.Hour 7}}value="{{printf "%02d" $.Date.Hour}}:{{printf "%02d" $.Date.Minute}}"{{end}} required/><br />
<label for="dauer">Dauer in Minuten</label>: <input name="dauer" type="number" min="{{.MinuteGranularity}}" max="120" step="{{.MinuteGranularity}}" value="{{.Duration}}"/><br /> <label for="dauer">Dauer in Minuten</label>: <input name="dauer" type="number" min="{{.MinuteGranularity}}" max="120" step="{{.MinuteGranularity}}" value="{{.Duration}}" required/><br />
<label for="raum">Raum</label>: <label for="raum">Raum</label>:
<select name="raum">{{range $room := .Rooms}} <select name="raum">{{range $room := .Rooms}}
<option value="{{$room.Id}}"{{if eq $room.Id $.SelectedRoom}} selected{{end}}>{{$room.Name}}</option>{{end}} <option value="{{$room.Id}}"{{if eq $room.Id $.SelectedRoom}} selected{{end}}>{{$room.Name}}</option>{{end}}
</select><br /> </select><br />
<label for="raumname">Raumname (für Sonderräume)</label>: <input type="text" name="raumname" value="{{.Roomname}}"/><br /> <label for="raumname">Raumname (für Sonderräume)</label>: <input type="text" name="raumname" value="{{.Roomname}}"/><br />
<label for="name">Name</label>: <input name="name" type="text" size="50" value="{{.Name}}"/><br /> <label for="name">Name</label>: <input name="name" type="text" size="50" value="{{.Name}}" required/><br />
<label for="email">Email-Adresse</label>: <label for="email">Email-Adresse</label>:
<input name="email" type="email" size="50" value="{{.Email}}"/><br /> <input name="email" type="email" size="50" value="{{.Email}}" required/><br />
<label for="info">Info</label>: <input name="info" type="text" size="50" value="{{.Info}}"/><br /> <label for="info">Info</label>: <input name="info" type="text" size="50" value="{{.Info}}"/><br />
<input type="submit"> <input type="submit">
</form> </form>

View file

@ -7,11 +7,10 @@ mit deiner Emailadresse soll eine Sprechstunde mit folgenden Daten {{if eq .Acti
{{.OfficeHour.Course.Name}} {{.OfficeHour.Course.Name}}
{{DayName .OfficeHour.Date.Day}} {{DayName .OfficeHour.Date.Day}}
ab {{.OfficeHour.Date.Hour}}:{{printf "%02d" .OfficeHour.Date.Minute}} Uhr {{printf "%02d".OfficeHour.Date.Hour}}:{{printf "%02d" .OfficeHour.Date.Minute}} Uhr bis {{printf "%02d" .OfficeHour.EndDate.Hour}}:{{printf "%02d" .OfficeHour.EndDate.Minute}} Uhr
{{.OfficeHour.Duration}} Minuten
{{.OfficeHour.Tutor.Name}} {{.OfficeHour.Tutor.Name}}
Wenn dies richtig ist, so bestätige die Sprechstunde durch Abrufen der folgenden URL: Falls dies richtig ist, so bestätige die Sprechstunde durch Abrufen der folgenden URL:
https://sprechstunden.mathebau.de/confirmRequest?code={{.Secret}} https://sprechstunden.mathebau.de/confirmRequest?code={{.Secret}}
Solltest du diese Email nicht erwartet haben, so kannst du sie einfach ignorieren. Solltest du diese Email nicht erwartet haben, so kannst du sie einfach ignorieren.

View file

@ -0,0 +1,12 @@
<html>
<head>
<title>Anfrage ausgeführt</title>
</head>
<body>
Deine Anfrage wurde ausgeführt.
<footer>
<a href="/addOfficeHour">Sprechstunde anlegen</a><br />
Technische Fragen an <a href="mailto:sprechstundentool@mathebau.de">sprechstundentool@mathebau.de</a>
</footer>
</body>
</html>

View file

@ -1,6 +1,6 @@
<td rowspan="{{divide .OfficeHour.Duration .MinuteGranularity}}" style="border: 1px solid"> <td rowspan="{{divide .OfficeHour.Duration .MinuteGranularity}}" style="border: 1px solid">
{{if .DeleteIcons}}<div style="text-align: right;"><a href="/deleteOfficeHour?id={{.OfficeHour.Id}}"></a></div>{{end}} {{if .DeleteIcons}}<div style="text-align: right;"><a href="/deleteOfficeHour?id={{.OfficeHour.Id}}"></a></div>{{end}}
{{printf "%02d" .OfficeHour.Date.Hour}}:{{printf "%02d" .OfficeHour.Date.Minute}} - {{printf "%02d" (add .OfficeHour.Date.Hour (divide .OfficeHour.Duration 60))}}:{{printf "%02d" (mod (add .OfficeHour.Date.Minute .OfficeHour.Duration) 60)}}<br /> {{printf "%02d" .OfficeHour.Date.Hour}}:{{printf "%02d" .OfficeHour.Date.Minute}} - {{printf "%02d" .OfficeHour.EndDate.Hour}}:{{printf "%02d" .OfficeHour.EndDate.Minute}}<br />
{{.OfficeHour.Course.Name}}<br /> {{.OfficeHour.Course.Name}}<br />
{{.OfficeHour.Tutor.Name}}<br /> {{.OfficeHour.Tutor.Name}}<br />
{{.OfficeHour.Room.Name}} {{.OfficeHour.Room.Name}}