Erstelle Maske, um Sprechstunden hinzuzufügen
This commit is contained in:
parent
c7a9522c2c
commit
369f4ebcec
15 changed files with 357 additions and 50 deletions
|
@ -22,8 +22,14 @@ func NewOfficeHourRepo(db *sql.DB, roomRepo *RoomRepo, tutorRepo *TutorRepo, cou
|
|||
}
|
||||
}
|
||||
|
||||
func (r *OfficeHourRepo) GetAll() ([]models.OfficeHour, error) {
|
||||
rows, err := r.db.Query("SELECT * FROM officeHour")
|
||||
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")
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -31,8 +37,14 @@ func (r *OfficeHourRepo) GetAll() ([]models.OfficeHour, error) {
|
|||
return r.getFromRows(rows)
|
||||
}
|
||||
|
||||
func (r *OfficeHourRepo) FindByCourse(course models.Course) ([]models.OfficeHour, error) {
|
||||
rows, err := r.db.Query("SELECT * FROM officeHour WHERE course=?", course.Id)
|
||||
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)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -40,8 +52,14 @@ func (r *OfficeHourRepo) FindByCourse(course models.Course) ([]models.OfficeHour
|
|||
return r.getFromRows(rows)
|
||||
}
|
||||
|
||||
func (r *OfficeHourRepo) FindByRoom(room models.Room) ([]models.OfficeHour, error) {
|
||||
rows, err := r.db.Query("SELECT * FROM officeHour WHERE room=?", room.Id)
|
||||
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)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -55,7 +73,43 @@ func (r *OfficeHourRepo) FindById(id int) (models.OfficeHour, error) {
|
|||
}
|
||||
|
||||
func (r *OfficeHourRepo) Add(officeHour models.OfficeHour) error {
|
||||
return nil
|
||||
// Find correct tutor or add if not existent
|
||||
r.tutorRepo.Add(officeHour.Tutor)
|
||||
var err error
|
||||
officeHour.Tutor, err = r.tutorRepo.FindByNameAndEmail(officeHour.Tutor.Name, officeHour.Tutor.Email)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//Don't add identical officeHours
|
||||
officeHours, err := r.FindByCourse(officeHour.Course, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
_, err = r.db.Exec("INSERT INTO `officeHour` (tutor, day, hour, minute, room, course, week, info, active, duration) VALUES (?,?,?,?,?,?,?,?,?,?)",
|
||||
officeHour.Tutor.Id,
|
||||
officeHour.Date.Day,
|
||||
officeHour.Date.Hour,
|
||||
officeHour.Date.Minute,
|
||||
officeHour.Room.Id,
|
||||
officeHour.Course.Id,
|
||||
officeHour.Date.Week,
|
||||
officeHour.Info,
|
||||
officeHour.Active,
|
||||
officeHour.Duration)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *OfficeHourRepo) Delete(officeHour models.OfficeHour) error {
|
||||
|
@ -64,13 +118,13 @@ func (r *OfficeHourRepo) Delete(officeHour models.OfficeHour) error {
|
|||
|
||||
func (r *OfficeHourRepo) getFromRow(row *sql.Row) (models.OfficeHour, error) {
|
||||
var officeHour models.OfficeHour
|
||||
var day, hour, minute, tutorid int
|
||||
var week, day, hour, minute, tutorid int
|
||||
var roomName, courseName string
|
||||
err := row.Scan(&officeHour.Id, &tutorid, &day, &hour, &minute, &roomName, &courseName, &officeHour.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 {
|
||||
return models.OfficeHour{}, err
|
||||
}
|
||||
officeHour.Date = models.Date{day, hour, minute}
|
||||
officeHour.Date = models.Date{week, day, hour, minute}
|
||||
officeHour.Room, _ = r.roomRepo.FindByName(roomName)
|
||||
officeHour.Tutor, _ = r.tutorRepo.FindById(tutorid)
|
||||
officeHour.Course, _ = r.courseRepo.FindByName(courseName)
|
||||
|
@ -81,11 +135,11 @@ func (r *OfficeHourRepo) getFromRows(rows *sql.Rows) ([]models.OfficeHour, error
|
|||
var officeHours []models.OfficeHour
|
||||
for rows.Next() {
|
||||
var officeHour models.OfficeHour
|
||||
var day, hour, minute, tutorId, roomId, courseId int
|
||||
if err := rows.Scan(&officeHour.Id, &tutorId, &day, &hour, &minute, &roomId, &courseId, &officeHour.Week, &officeHour.Info, &officeHour.Active, &officeHour.Duration); err != nil {
|
||||
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 {
|
||||
return officeHours, err
|
||||
}
|
||||
officeHour.Date = models.Date{day, hour, minute}
|
||||
officeHour.Date = models.Date{week, day, hour, minute}
|
||||
officeHour.Room, _ = r.roomRepo.FindById(roomId)
|
||||
officeHour.Tutor, _ = r.tutorRepo.FindById(tutorId)
|
||||
officeHour.Course, _ = r.courseRepo.FindById(courseId)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue