Sprechstunden löschen und Raumbeschränkungen beachten

This commit is contained in:
Gonne 2022-09-07 18:26:05 +02:00
parent 2ce7a1fae1
commit 56b4a3ab34
7 changed files with 88 additions and 10 deletions

View file

@ -91,6 +91,7 @@ func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Requ
errors = append(errors, fmt.Sprintf("Sprechstunden dürfen nur alle %d Minuten starten.", models.MinuteGranularity))
}
}
date := models.Date{week, day, hour, minute}
duration, err := strconv.Atoi(req.FormValue("dauer"))
if err != nil {
errors = append(errors, "Die Dauer muss eine ganze Zahl sein.")
@ -113,6 +114,13 @@ func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Requ
}
info := req.FormValue("info")
allowed, err := b.officeHourRepo.AllowedAt(date, duration, room, true)
fmt.Println(allowed)
if err != nil {
errors = append(errors, fmt.Sprintf("Fehler beim Abfragen der Raumkapazität: %s", err.Error()))
} else if !allowed {
errors = append(errors, "In dem Raum muss noch Platz für weitere Sprechstunden sein.")
}
if len(errors) != 0 {
var data maskData = maskData{
courses,
@ -135,7 +143,7 @@ func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Requ
} else {
officeHour := models.OfficeHour{0,
models.Tutor{0, name, email.Address},
models.Date{week, day, hour, minute},
date,
room,
course,
info,

View file

@ -13,20 +13,20 @@ func GetTimetable(officeHours []models.OfficeHour) (timetable map[models.Date]ma
for _, officeHour := range officeHours {
var slot int = 0
for minute := 0; minute < officeHour.Duration; minute += models.MinuteGranularity { // find slot id
_, exists := fullTimetable[models.Date{0, officeHour.Day, officeHour.Hour + (officeHour.Minute+minute)/60, (officeHour.Minute + minute) % 60}]
_, exists := fullTimetable[models.GetEndDate(officeHour.Date, minute, true)]
if exists {
_, exists := fullTimetable[models.Date{0, officeHour.Day, officeHour.Hour + (officeHour.Minute+minute)/60, (officeHour.Minute + minute) % 60}][slot]
_, exists := fullTimetable[models.GetEndDate(officeHour.Date, minute, true)][slot]
if exists {
slot += 1
minute = 0
continue
}
} else {
fullTimetable[models.Date{0, officeHour.Day, officeHour.Hour + (officeHour.Minute+minute)/60, (officeHour.Minute + minute) % 60}] = make(map[int]models.OfficeHour)
fullTimetable[models.GetEndDate(officeHour.Date, minute, true)] = make(map[int]models.OfficeHour)
}
}
for minute := 0; minute < officeHour.Duration; minute += models.MinuteGranularity { // write officeHour id to timetable
fullTimetable[models.Date{0, officeHour.Day, officeHour.Hour + (officeHour.Minute+minute)/60, (officeHour.Minute + minute) % 60}][slot] = officeHour
fullTimetable[models.GetEndDate(officeHour.Date, minute, true)][slot] = officeHour
}
}
slots = []int{1, 1, 1, 1, 1}

View file

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

View file

@ -26,3 +26,34 @@ func DayName(day int) string {
return ""
}
}
func DateLess(first Date, second Date) bool {
if first.Day < second.Day {
return true
} else if first.Day > second.Day {
return false
}
if first.Hour < second.Hour {
return true
} else if first.Hour > second.Hour {
return false
}
if first.Minute < second.Minute {
return true
} else if first.Minute > second.Minute {
return false
}
return false
}
func GetEndDate(date Date, duration int, ignoreWeek bool) Date {
var endDate Date
if ignoreWeek {
endDate = Date{0, date.Day, date.Hour, date.Minute} // deep copy
} else {
endDate = Date{date.Week, date.Day, date.Hour, date.Minute}
}
endDate.Hour = endDate.Hour + (endDate.Minute+duration)/60
endDate.Minute = (endDate.Minute + duration) % 60
return endDate
}

View file

@ -19,4 +19,5 @@ type OfficeHourRepository interface {
GetAll(activatedOnly bool) ([]OfficeHour, error)
Delete(officeHour OfficeHour) error
Add(officeHour OfficeHour) (int, error)
AllowedAt(date Date, duration int, room Room, activeOnly bool) (bool, error)
}

View file

@ -69,7 +69,6 @@ func (r *OfficeHourRepo) FindByRoom(room models.Room, activeOnly bool) ([]models
func (r *OfficeHourRepo) FindById(id int) (models.OfficeHour, error) {
return r.getFromRow(r.db.QueryRow("SELECT * FROM officeHour WHERE id=?", id))
}
func (r *OfficeHourRepo) Add(officeHour models.OfficeHour) (id int, err error) {
@ -113,7 +112,8 @@ func (r *OfficeHourRepo) Add(officeHour models.OfficeHour) (id int, err error) {
}
func (r *OfficeHourRepo) Delete(officeHour models.OfficeHour) error {
return nil
_, err := r.db.Exec("DELETE FROM officeHour WHERE id=?", officeHour.Id)
return err
}
func (r *OfficeHourRepo) getFromRow(row *sql.Row) (models.OfficeHour, error) {
@ -147,3 +147,40 @@ func (r *OfficeHourRepo) getFromRows(rows *sql.Rows) ([]models.OfficeHour, error
}
return officeHours, nil
}
func (r *OfficeHourRepo) NumberByTimeSpanAndRoom(date models.Date, duration int, room models.Room, activeOnly bool) (int, error) {
var rows *sql.Rows
var err error
if activeOnly {
rows, err = r.db.Query("SELECT * FROM officeHour WHERE room=? AND day =? AND active", room.Id, date.Day)
} else {
rows, err = r.db.Query("SELECT * FROM officeHour WHERE room=?", room.Id)
}
if err != nil {
return 0, err
}
defer rows.Close()
officeHours, err := r.getFromRows(rows)
if err != nil {
return 0, err
}
var count int
for _, officeHour := range officeHours {
if models.DateLess(models.GetEndDate(officeHour.Date, officeHour.Duration, false), date) || models.GetEndDate(officeHour.Date, officeHour.Duration, false) == date {
continue
}
if models.DateLess(models.GetEndDate(date, duration, false), officeHour.Date) || models.GetEndDate(date, duration, false) == officeHour.Date {
continue
}
count += 1
}
return count, nil
}
func (r *OfficeHourRepo) AllowedAt(date models.Date, duration int, room models.Room, activeOnly bool) (bool, error) {
numberOfOfficeHours, err := r.NumberByTimeSpanAndRoom(date, duration, room, activeOnly)
if err != nil {
return false, err
}
return numberOfOfficeHours >= room.MaxOccupy, nil
}

View file

@ -94,10 +94,10 @@ func (r *RequestRepo) Execute(request models.Request) error {
switch request.Action {
case models.RequestActivate:
_, err = r.db.Exec("UPDATE officeHour SET active=true WHERE id=?", request.OfficeHour.Id)
r.db.Exec("DELETE FROM request WHERE id=?", request.Id)
r.db.Exec("DELETE FROM request WHERE officeHour=?", request.OfficeHour.Id)
case models.RequestDelete:
_, err = r.db.Exec("DELETE FROM officeHour WHERE id=?", request.OfficeHour.Id)
r.db.Exec("DELETE FROM request WHERE id=?", request.Id)
r.officeHourRepo.Delete(request.OfficeHour)
r.db.Exec("DELETE FROM request WHERE officeHour=?", request.OfficeHour.Id)
default:
r.db.Exec("DELETE FROM request WHERE id=?", request.Id)
}