Refactor GetTimetable

Delete dead code and remove named return arguments to avoid confusion between them and the returned values.
Add documentation.
This commit is contained in:
Gonne 2023-07-12 08:35:05 +02:00
parent 5c68aea9a7
commit 4ea0471683

View file

@ -10,45 +10,42 @@ import (
"officeHours/templating"
)
func (b *BaseHandler) GetTimetable(officeHours []models.OfficeHour) (timetable map[models.Date]map[int]models.OfficeHour, slots []int) {
var fullTimetable = make(map[models.Date]map[int]models.OfficeHour)
// A function that takes a slice of office hours and
// constructs a timetable t such that t[date][slot] is an
// office hour that takes place at this time and no such cell has more than one
// office hour and each officehour is in exactly one slot.
// It also indicates how many slots exist per day.
func (b *BaseHandler) GetTimetable(officeHours []models.OfficeHour) (map[models.Date]map[int]models.OfficeHour, []int) {
var timetable = make(map[models.Date]map[int]models.OfficeHour)
for _, officeHour := range officeHours {
var slot int = 0
for minute := 0; minute < officeHour.Duration; minute += b.config.Date.MinuteGranularity { // find slot id
_, exists := fullTimetable[models.GetEndDate(officeHour.Date, minute, true)]
_, exists := timetable[models.GetEndDate(officeHour.Date, minute, true)]
if exists {
_, exists := fullTimetable[models.GetEndDate(officeHour.Date, minute, true)][slot]
_, exists := timetable[models.GetEndDate(officeHour.Date, minute, true)][slot]
if exists {
slot += 1
minute = 0
continue
}
} else {
fullTimetable[models.GetEndDate(officeHour.Date, minute, true)] = make(map[int]models.OfficeHour)
timetable[models.GetEndDate(officeHour.Date, minute, true)] = make(map[int]models.OfficeHour)
}
}
for minute := 0; minute < officeHour.Duration; minute += b.config.Date.MinuteGranularity { // write officeHour id to timetable
fullTimetable[models.GetEndDate(officeHour.Date, minute, true)][slot] = officeHour
timetable[models.GetEndDate(officeHour.Date, minute, true)][slot] = officeHour
}
}
slots = []int{1, 1, 1, 1, 1}
for date, _ := range fullTimetable {
if slots[date.Day] < len(fullTimetable[date]) {
slots[date.Day] = len(fullTimetable[date])
slots := []int{1, 1, 1, 1, 1}
for date, _ := range timetable {
if slots[date.Day] < len(timetable[date]) {
slots[date.Day] = len(timetable[date])
}
}
timetable = make(map[models.Date]map[int]models.OfficeHour)
for _, officeHour := range officeHours {
for slot := 0; slot < slots[officeHour.Date.Day]; slot += 1 {
if fullTimetable[officeHour.Date][slot] == officeHour {
timetable[officeHour.Date] = make(map[int]models.OfficeHour)
timetable[officeHour.Date][slot] = officeHour
}
}
}
return fullTimetable, slots
return timetable, slots
}
// A function to generate the HTML code for the table of office hours from a timetable.
func (b *BaseHandler) printTimetable(timetable map[models.Date]map[int]models.OfficeHour, slots []int, deleteIcons bool) template.HTML {
if len(timetable) == 0 { // no office hours to display
return template.HTML("<p class=\"text-center\">Aktuell sind keine passenden Sprechstunden eingetragen.</p>")