95 lines
2.8 KiB
Go
95 lines
2.8 KiB
Go
// officeHour
|
|
package repositories
|
|
|
|
import (
|
|
"database/sql"
|
|
"sprechstundentool/models"
|
|
)
|
|
|
|
type OfficeHourRepo struct {
|
|
db *sql.DB
|
|
roomRepo *RoomRepo
|
|
tutorRepo *TutorRepo
|
|
courseRepo *CourseRepo
|
|
}
|
|
|
|
func NewOfficeHourRepo(db *sql.DB, roomRepo *RoomRepo, tutorRepo *TutorRepo, courseRepo *CourseRepo) *OfficeHourRepo {
|
|
return &OfficeHourRepo{
|
|
db: db,
|
|
roomRepo: roomRepo,
|
|
tutorRepo: tutorRepo,
|
|
courseRepo: courseRepo,
|
|
}
|
|
}
|
|
|
|
func (r *OfficeHourRepo) GetAll() ([]models.OfficeHour, error) {
|
|
rows, err := r.db.Query("SELECT * FROM officeHour")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
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)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
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)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
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))
|
|
|
|
}
|
|
|
|
func (r *OfficeHourRepo) Add(officeHour models.OfficeHour) error {
|
|
return nil
|
|
}
|
|
|
|
func (r *OfficeHourRepo) Delete(officeHour models.OfficeHour) error {
|
|
return nil
|
|
}
|
|
|
|
func (r *OfficeHourRepo) getFromRow(row *sql.Row) (models.OfficeHour, error) {
|
|
var officeHour models.OfficeHour
|
|
var 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)
|
|
if err != nil {
|
|
return models.OfficeHour{}, err
|
|
}
|
|
officeHour.Date = models.Date{day, hour, minute}
|
|
officeHour.Room, _ = r.roomRepo.FindByName(roomName)
|
|
officeHour.Tutor, _ = r.tutorRepo.FindById(tutorid)
|
|
officeHour.Course, _ = r.courseRepo.FindByName(courseName)
|
|
return officeHour, nil
|
|
}
|
|
|
|
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 {
|
|
return officeHours, err
|
|
}
|
|
officeHour.Date = models.Date{day, hour, minute}
|
|
officeHour.Room, _ = r.roomRepo.FindById(roomId)
|
|
officeHour.Tutor, _ = r.tutorRepo.FindById(tutorId)
|
|
officeHour.Course, _ = r.courseRepo.FindById(courseId)
|
|
officeHours = append(officeHours, officeHour)
|
|
}
|
|
return officeHours, nil
|
|
}
|