Verbessere Logging und Fehlerbehandlung

This commit is contained in:
Gonne 2022-09-21 22:24:08 +02:00
parent c737818ce4
commit 6e97d867de
14 changed files with 223 additions and 88 deletions

View file

@ -3,7 +3,9 @@ package repositories
import (
"database/sql"
"errors"
"fmt"
"log"
"officeHours/config"
"officeHours/models"
)
@ -35,7 +37,11 @@ func (r *OfficeHourRepo) GetAll(activeOnly bool) ([]models.OfficeHour, error) {
rows, err = r.db.Query("SELECT * FROM officeHour")
}
if err != nil {
return nil, fmt.Errorf("Error getting all officeHours from database: %s", err.Error())
err = fmt.Errorf("Error getting all officeHours from database: %w", err)
if !errors.Is(err, sql.ErrNoRows) {
log.Println(err.Error())
}
return nil, err
}
defer rows.Close()
return r.getFromRows(rows)
@ -49,10 +55,14 @@ func (r *OfficeHourRepo) FindByCourse(course models.Course, activeOnly bool) ([]
} else {
rows, err = r.db.Query("SELECT * FROM officeHour WHERE course=?", course.Id)
}
if err != nil {
return nil, fmt.Errorf("Error getting officeHours by course from database: %s", err.Error())
}
defer rows.Close()
if err != nil {
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
}
return r.getFromRows(rows)
}
@ -65,7 +75,11 @@ func (r *OfficeHourRepo) FindByRoom(room models.Room, activeOnly bool) ([]models
rows, err = r.db.Query("SELECT * FROM officeHour WHERE room=?", room.Id)
}
if err != nil {
return nil, fmt.Errorf("Error getting officeHours by room from database: %s", err.Error())
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
}
defer rows.Close()
return r.getFromRows(rows)
@ -75,11 +89,16 @@ 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) {
func (r *OfficeHourRepo) Add(officeHour models.OfficeHour) (int, error) {
// Find correct tutor or add if not existent
r.tutorRepo.Add(officeHour.Tutor)
_, err := r.tutorRepo.Add(officeHour.Tutor)
if err != nil {
return 0, err
}
officeHour.Tutor, err = r.tutorRepo.FindByNameAndEmail(officeHour.Tutor.Name, officeHour.Tutor.Email)
if err != nil {
err = fmt.Errorf("Newly added tutor not found: %w", err)
log.Println(err.Error())
return 0, err
}
@ -111,14 +130,19 @@ func (r *OfficeHourRepo) Add(officeHour models.OfficeHour) (id int, err error) {
officeHour.Info,
officeHour.Active,
officeHour.Duration)
id64, _ := sqlResult.LastInsertId()
return int(id64), err
id, lastInsertIdErr := sqlResult.LastInsertId()
if lastInsertIdErr != nil {
log.Printf("Error getting Id for new tutor: %s", lastInsertIdErr.Error())
}
return int(id), err
}
func (r *OfficeHourRepo) Delete(officeHour models.OfficeHour) error {
_, err := r.db.Exec("DELETE FROM officeHour WHERE id=?", officeHour.Id)
if err != nil {
return fmt.Errorf("Error deleting officeHour from database: %s", err.Error())
err = fmt.Errorf("Error deleting officeHour from database: %w", err)
log.Println(err.Error())
return err
}
return nil
}
@ -128,12 +152,29 @@ func (r *OfficeHourRepo) getFromRow(row *sql.Row) (models.OfficeHour, error) {
var week, day, hour, minute, tutorId, courseId, roomId int
err := row.Scan(&officeHour.Id, &tutorId, &day, &hour, &minute, &roomId, &courseId, &week, &officeHour.Info, &officeHour.Active, &officeHour.Duration)
if err != nil {
return models.OfficeHour{}, fmt.Errorf("Error getting single officeHours row from database: %s", err.Error())
err = fmt.Errorf("Error getting single officeHour row from database: %w", err)
log.Println(err.Error())
return models.OfficeHour{}, err
}
officeHour.Date = models.Date{Week: week, Day: day, Hour: hour, Minute: minute}
officeHour.Room, _ = r.roomRepo.FindById(roomId)
officeHour.Tutor, _ = r.tutorRepo.FindById(tutorId)
officeHour.Course, _ = r.courseRepo.FindById(courseId)
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
}
return officeHour, nil
}
@ -142,13 +183,29 @@ func (r *OfficeHourRepo) getFromRows(rows *sql.Rows) ([]models.OfficeHour, error
for rows.Next() {
var officeHour models.OfficeHour
var week, day, hour, minute, tutorId, roomId, courseId int
var err error
if err := rows.Scan(&officeHour.Id, &tutorId, &day, &hour, &minute, &roomId, &courseId, &week, &officeHour.Info, &officeHour.Active, &officeHour.Duration); err != nil {
return officeHours, fmt.Errorf("Error getting multiple officeHour rows from database: %s", err.Error())
return officeHours, fmt.Errorf("Error getting multiple officeHour rows from database: %w", err)
}
officeHour.Date = models.Date{Week: week, Day: day, Hour: hour, Minute: minute}
officeHour.Room, _ = r.roomRepo.FindById(roomId)
officeHour.Tutor, _ = r.tutorRepo.FindById(tutorId)
officeHour.Course, _ = r.courseRepo.FindById(courseId)
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
}
officeHours = append(officeHours, officeHour)
}
return officeHours, nil
@ -163,7 +220,9 @@ func (r *OfficeHourRepo) NumberByTimeSpanAndRoom(date models.Date, duration int,
rows, err = r.db.Query("SELECT * FROM officeHour WHERE room=?", room.Id)
}
if err != nil {
return 0, fmt.Errorf("Error getting officeHours by timespan and room from database: %s", err.Error())
err = fmt.Errorf("Error getting officeHours by timespan and room from database: %w", err)
log.Println(err.Error())
return 0, err
}
defer rows.Close()
officeHours, err := r.getFromRows(rows)