2022-08-29 20:58:19 +00:00
|
|
|
// officeHour
|
|
|
|
package repositories
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2022-09-21 20:24:08 +00:00
|
|
|
"errors"
|
2022-09-07 19:48:40 +00:00
|
|
|
"fmt"
|
2022-09-21 20:24:08 +00:00
|
|
|
"log"
|
2022-09-20 10:21:01 +00:00
|
|
|
"officeHours/config"
|
|
|
|
"officeHours/models"
|
2022-08-29 20:58:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OfficeHourRepo struct {
|
|
|
|
db *sql.DB
|
|
|
|
roomRepo *RoomRepo
|
|
|
|
tutorRepo *TutorRepo
|
|
|
|
courseRepo *CourseRepo
|
2022-09-19 16:32:15 +00:00
|
|
|
config config.Config
|
2022-08-29 20:58:19 +00:00
|
|
|
}
|
|
|
|
|
2022-09-19 16:32:15 +00:00
|
|
|
func NewOfficeHourRepo(db *sql.DB, roomRepo *RoomRepo, tutorRepo *TutorRepo, courseRepo *CourseRepo, conf config.Config) *OfficeHourRepo {
|
2022-08-29 20:58:19 +00:00
|
|
|
return &OfficeHourRepo{
|
|
|
|
db: db,
|
|
|
|
roomRepo: roomRepo,
|
|
|
|
tutorRepo: tutorRepo,
|
|
|
|
courseRepo: courseRepo,
|
2022-09-19 16:32:15 +00:00
|
|
|
config: conf,
|
2022-08-29 20:58:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 20:49:14 +00:00
|
|
|
func (r *OfficeHourRepo) GetAll(activeOnly bool) ([]models.OfficeHour, error) {
|
|
|
|
var rows *sql.Rows
|
|
|
|
var err error
|
|
|
|
if activeOnly {
|
|
|
|
rows, err = r.db.Query("SELECT * FROM officeHour WHERE active")
|
|
|
|
} else {
|
|
|
|
rows, err = r.db.Query("SELECT * FROM officeHour")
|
|
|
|
}
|
2022-08-29 20:58:19 +00:00
|
|
|
if err != nil {
|
2022-09-21 20:24:08 +00:00
|
|
|
err = fmt.Errorf("Error getting all officeHours from database: %w", err)
|
|
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
|
|
|
return nil, err
|
2022-08-29 20:58:19 +00:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
return r.getFromRows(rows)
|
|
|
|
}
|
|
|
|
|
2022-08-31 20:49:14 +00:00
|
|
|
func (r *OfficeHourRepo) FindByCourse(course models.Course, activeOnly bool) ([]models.OfficeHour, error) {
|
|
|
|
var rows *sql.Rows
|
|
|
|
var err error
|
|
|
|
if activeOnly {
|
|
|
|
rows, err = r.db.Query("SELECT * FROM officeHour WHERE course=? and active", course.Id)
|
|
|
|
} else {
|
|
|
|
rows, err = r.db.Query("SELECT * FROM officeHour WHERE course=?", course.Id)
|
|
|
|
}
|
2022-09-21 20:24:08 +00:00
|
|
|
defer rows.Close()
|
2022-08-29 20:58:19 +00:00
|
|
|
if err != nil {
|
2022-09-21 20:24:08 +00:00
|
|
|
err = fmt.Errorf("Error getting officeHours by course from database: %w", err)
|
|
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
|
|
|
return nil, err
|
2022-08-29 20:58:19 +00:00
|
|
|
}
|
|
|
|
return r.getFromRows(rows)
|
|
|
|
}
|
|
|
|
|
2022-08-31 20:49:14 +00:00
|
|
|
func (r *OfficeHourRepo) FindByRoom(room models.Room, activeOnly bool) ([]models.OfficeHour, error) {
|
|
|
|
var rows *sql.Rows
|
|
|
|
var err error
|
|
|
|
if activeOnly {
|
|
|
|
rows, err = r.db.Query("SELECT * FROM officeHour WHERE room=? AND active", room.Id)
|
|
|
|
} else {
|
|
|
|
rows, err = r.db.Query("SELECT * FROM officeHour WHERE room=?", room.Id)
|
|
|
|
}
|
2022-08-29 20:58:19 +00:00
|
|
|
if err != nil {
|
2022-09-21 20:24:08 +00:00
|
|
|
err = fmt.Errorf("Error getting officeHours by room from database: %w", err)
|
|
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
|
|
|
return nil, err
|
2022-08-29 20:58:19 +00:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
return r.getFromRows(rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *OfficeHourRepo) FindById(id int) (models.OfficeHour, error) {
|
|
|
|
return r.getFromRow(r.db.QueryRow("SELECT * FROM officeHour WHERE id=?", id))
|
|
|
|
}
|
|
|
|
|
2022-09-21 20:24:08 +00:00
|
|
|
func (r *OfficeHourRepo) Add(officeHour models.OfficeHour) (int, error) {
|
2022-08-31 20:49:14 +00:00
|
|
|
// Find correct tutor or add if not existent
|
2022-09-21 20:24:08 +00:00
|
|
|
_, err := r.tutorRepo.Add(officeHour.Tutor)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2022-08-31 20:49:14 +00:00
|
|
|
officeHour.Tutor, err = r.tutorRepo.FindByNameAndEmail(officeHour.Tutor.Name, officeHour.Tutor.Email)
|
|
|
|
if err != nil {
|
2022-09-21 20:24:08 +00:00
|
|
|
err = fmt.Errorf("Newly added tutor not found: %w", err)
|
|
|
|
log.Println(err.Error())
|
2022-09-05 15:55:08 +00:00
|
|
|
return 0, err
|
2022-08-31 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Don't add identical officeHours
|
|
|
|
officeHours, err := r.FindByCourse(officeHour.Course, false)
|
|
|
|
if err != nil {
|
2022-09-05 15:55:08 +00:00
|
|
|
return 0, err
|
2022-08-31 20:49:14 +00:00
|
|
|
}
|
|
|
|
for _, oldOfficeHour := range officeHours {
|
|
|
|
if officeHour.Tutor == oldOfficeHour.Tutor &&
|
|
|
|
officeHour.Date == oldOfficeHour.Date &&
|
|
|
|
officeHour.Room == oldOfficeHour.Room &&
|
|
|
|
// officeHour.Course == oldOfficeHour.Course && already filtered above
|
|
|
|
officeHour.Info == oldOfficeHour.Info &&
|
|
|
|
officeHour.Active == oldOfficeHour.Active &&
|
|
|
|
officeHour.Duration == oldOfficeHour.Duration {
|
2022-09-07 19:48:40 +00:00
|
|
|
return oldOfficeHour.Id, nil
|
2022-08-31 20:49:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 14:40:47 +00:00
|
|
|
sqlResult, err := r.db.Exec("INSERT INTO `officeHour` (tutor, day, hour, minute, room, roomname, course, week, info, active, duration) VALUES (?,?,?,?,?,?,?,?,?,?,?)",
|
2022-08-31 20:49:14 +00:00
|
|
|
officeHour.Tutor.Id,
|
|
|
|
officeHour.Date.Day,
|
|
|
|
officeHour.Date.Hour,
|
|
|
|
officeHour.Date.Minute,
|
|
|
|
officeHour.Room.Id,
|
2022-09-27 14:40:47 +00:00
|
|
|
officeHour.RoomName,
|
2022-08-31 20:49:14 +00:00
|
|
|
officeHour.Course.Id,
|
|
|
|
officeHour.Date.Week,
|
|
|
|
officeHour.Info,
|
|
|
|
officeHour.Active,
|
|
|
|
officeHour.Duration)
|
2022-09-27 14:40:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("SQL-error inserting new office hour: %w", err)
|
|
|
|
}
|
2022-09-21 20:24:08 +00:00
|
|
|
id, lastInsertIdErr := sqlResult.LastInsertId()
|
|
|
|
if lastInsertIdErr != nil {
|
|
|
|
log.Printf("Error getting Id for new tutor: %s", lastInsertIdErr.Error())
|
|
|
|
}
|
|
|
|
return int(id), err
|
2022-08-29 20:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *OfficeHourRepo) Delete(officeHour models.OfficeHour) error {
|
2022-09-07 16:26:05 +00:00
|
|
|
_, err := r.db.Exec("DELETE FROM officeHour WHERE id=?", officeHour.Id)
|
2022-09-19 12:46:16 +00:00
|
|
|
if err != nil {
|
2022-09-21 20:24:08 +00:00
|
|
|
err = fmt.Errorf("Error deleting officeHour from database: %w", err)
|
|
|
|
log.Println(err.Error())
|
|
|
|
return err
|
2022-09-19 12:46:16 +00:00
|
|
|
}
|
|
|
|
return nil
|
2022-08-29 20:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *OfficeHourRepo) getFromRow(row *sql.Row) (models.OfficeHour, error) {
|
|
|
|
var officeHour models.OfficeHour
|
2022-09-12 17:42:45 +00:00
|
|
|
var week, day, hour, minute, tutorId, courseId, roomId int
|
2022-09-27 14:40:47 +00:00
|
|
|
err := row.Scan(&officeHour.Id, &tutorId, &day, &hour, &minute, &roomId, &officeHour.RoomName, &courseId, &week, &officeHour.Info, &officeHour.Active, &officeHour.Duration)
|
2022-08-29 20:58:19 +00:00
|
|
|
if err != nil {
|
2022-09-21 20:24:08 +00:00
|
|
|
err = fmt.Errorf("Error getting single officeHour row from database: %w", err)
|
|
|
|
log.Println(err.Error())
|
|
|
|
return models.OfficeHour{}, err
|
2022-08-29 20:58:19 +00:00
|
|
|
}
|
2022-09-19 16:51:51 +00:00
|
|
|
officeHour.Date = models.Date{Week: week, Day: day, Hour: hour, Minute: minute}
|
2022-09-21 20:24:08 +00:00
|
|
|
officeHour.Room, err = r.roomRepo.FindById(roomId)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error finding room by id %d for office hour: %w", roomId, err)
|
|
|
|
log.Println(err.Error())
|
|
|
|
return officeHour, err
|
|
|
|
}
|
|
|
|
officeHour.Tutor, err = r.tutorRepo.FindById(tutorId)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error finding tutor by id %d for office hour: %w", tutorId, err)
|
|
|
|
log.Println(err.Error())
|
|
|
|
return officeHour, err
|
|
|
|
}
|
|
|
|
officeHour.Course, err = r.courseRepo.FindById(courseId)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error finding course by id %d for office hour: %w", courseId, err)
|
|
|
|
log.Println(err.Error())
|
|
|
|
return officeHour, err
|
|
|
|
}
|
2022-08-29 20:58:19 +00:00
|
|
|
return officeHour, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *OfficeHourRepo) getFromRows(rows *sql.Rows) ([]models.OfficeHour, error) {
|
|
|
|
var officeHours []models.OfficeHour
|
|
|
|
for rows.Next() {
|
|
|
|
var officeHour models.OfficeHour
|
2022-08-31 20:49:14 +00:00
|
|
|
var week, day, hour, minute, tutorId, roomId, courseId int
|
2022-09-21 20:24:08 +00:00
|
|
|
var err error
|
2022-09-27 14:40:47 +00:00
|
|
|
if err := rows.Scan(&officeHour.Id, &tutorId, &day, &hour, &minute, &roomId, &officeHour.RoomName, &courseId, &week, &officeHour.Info, &officeHour.Active, &officeHour.Duration); err != nil {
|
2022-09-21 20:24:08 +00:00
|
|
|
return officeHours, fmt.Errorf("Error getting multiple officeHour rows from database: %w", err)
|
2022-08-29 20:58:19 +00:00
|
|
|
}
|
2022-09-19 16:51:51 +00:00
|
|
|
officeHour.Date = models.Date{Week: week, Day: day, Hour: hour, Minute: minute}
|
2022-09-21 20:24:08 +00:00
|
|
|
officeHour.Room, err = r.roomRepo.FindById(roomId)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error finding room by id %d for office hour: %w", roomId, err)
|
|
|
|
log.Println(err.Error())
|
|
|
|
return officeHours, err
|
|
|
|
}
|
|
|
|
officeHour.Tutor, err = r.tutorRepo.FindById(tutorId)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error finding tutor by id %d for office hour: %w", tutorId, err)
|
|
|
|
log.Println(err.Error())
|
|
|
|
return officeHours, err
|
|
|
|
}
|
|
|
|
officeHour.Course, err = r.courseRepo.FindById(courseId)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error finding course by id %d for office hour: %w", courseId, err)
|
|
|
|
log.Println(err.Error())
|
|
|
|
return officeHours, err
|
|
|
|
}
|
2022-08-29 20:58:19 +00:00
|
|
|
officeHours = append(officeHours, officeHour)
|
|
|
|
}
|
|
|
|
return officeHours, nil
|
|
|
|
}
|
2022-09-07 16:26:05 +00:00
|
|
|
|
|
|
|
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 {
|
2022-09-21 20:24:08 +00:00
|
|
|
err = fmt.Errorf("Error getting officeHours by timespan and room from database: %w", err)
|
|
|
|
log.Println(err.Error())
|
|
|
|
return 0, err
|
2022-09-07 16:26:05 +00:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
officeHours, err := r.getFromRows(rows)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
var count int
|
2022-09-19 16:32:15 +00:00
|
|
|
// iterate over all points in the new officehour to get the maximum parallel officehours
|
|
|
|
for minute := 0; minute < duration; minute += r.config.Date.MinuteGranularity {
|
|
|
|
var minuteCount int = 0
|
|
|
|
for _, officeHour := range officeHours {
|
|
|
|
// increase count if officehour starts before this point in time and ends later
|
|
|
|
if models.DateLess(officeHour.Date, models.GetEndDate(date, minute, false)) && models.DateLess(models.GetEndDate(date, minute, false), models.GetEndDate(officeHour.Date, officeHour.Duration, false)) {
|
|
|
|
minuteCount += 1
|
|
|
|
}
|
2022-09-07 16:26:05 +00:00
|
|
|
}
|
2022-09-19 16:32:15 +00:00
|
|
|
if minuteCount > count {
|
|
|
|
count = minuteCount
|
2022-09-07 16:26:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2022-09-07 19:48:40 +00:00
|
|
|
return numberOfOfficeHours < room.MaxOccupy, nil
|
2022-09-07 16:26:05 +00:00
|
|
|
}
|