From c8f85e40c49678e1c042e51bc7baf56bd74c06d7 Mon Sep 17 00:00:00 2001 From: Gonne Date: Wed, 28 Sep 2022 12:00:45 +0200 Subject: [PATCH] Verbessere Fehlerbehandlung bei Tutoren und Sprechstunden --- repositories/request.go | 6 ++-- repositories/tutor.go | 71 ++++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 39 deletions(-) diff --git a/repositories/request.go b/repositories/request.go index e4ec7ed..762bd7d 100644 --- a/repositories/request.go +++ b/repositories/request.go @@ -37,7 +37,7 @@ func (r *RequestRepo) FindBySecret(secret string) (models.Request, error) { var officeHourId int err := row.Scan(&request.Id, &officeHourId, &request.Action, &request.Secret) if err != nil { - return models.Request{}, err + return models.Request{}, fmt.Errorf("SQL-error scanning request row: %w", err) } request.OfficeHour, err = r.officeHourRepo.FindById(officeHourId) return request, err @@ -46,7 +46,7 @@ func (r *RequestRepo) FindBySecret(secret string) (models.Request, error) { func (r *RequestRepo) FindByOfficeHour(officeHour models.OfficeHour) ([]models.Request, error) { rows, err := r.db.Query("SELECT * FROM request WHERE officeHour=?", officeHour.Id) if err != nil { - return nil, err + return nil, fmt.Errorf("SQL-error selecting requests by office hour: %w", err) } defer rows.Close() @@ -88,7 +88,7 @@ func (r *RequestRepo) Add(officeHour models.OfficeHour, action int) (int, error) request := models.Request{Id: 0, OfficeHour: officeHour, Action: action, Secret: secret} _, err = r.db.Exec("INSERT INTO `request` (officeHour, action, secret) VALUES (?,?,?)", officeHour.Id, action, secret) if err != nil { - return 0, err + return 0, fmt.Errorf("SQL-error inserting new request: %w", err) } request, err = r.FindBySecret(secret) if err != nil { diff --git a/repositories/tutor.go b/repositories/tutor.go index 3c84819..77fc32c 100644 --- a/repositories/tutor.go +++ b/repositories/tutor.go @@ -26,35 +26,15 @@ func (r *TutorRepo) FindByEmail(email string) ([]models.Tutor, error) { } defer rows.Close() - var tutors []models.Tutor - for rows.Next() { - var tutor models.Tutor - if err := rows.Scan(&tutor.Id, &tutor.Name, &tutor.Email); err != nil { - err = fmt.Errorf("Error scanning tutor row: %w", err) - log.Println(err.Error()) - return tutors, err - } - tutors = append(tutors, tutor) - } - return tutors, nil + return r.getFromRows(rows) } func (r *TutorRepo) FindById(id int) (models.Tutor, error) { - row := r.db.QueryRow("SELECT * FROM tutor WHERE id=?", id) - var tutor models.Tutor - if err := row.Scan(&tutor.Id, &tutor.Name, &tutor.Email); err != nil { - return models.Tutor{}, err - } - return tutor, nil + return r.getFromRow(r.db.QueryRow("SELECT * FROM tutor WHERE id=?", id)) } func (r *TutorRepo) FindByNameAndEmail(name string, email string) (models.Tutor, error) { - row := r.db.QueryRow("SELECT * FROM tutor WHERE email=? AND name=?", email, name) - var tutor models.Tutor - if err := row.Scan(&tutor.Id, &tutor.Name, &tutor.Email); err != nil { - return models.Tutor{}, err - } - return tutor, nil + return r.getFromRow(r.db.QueryRow("SELECT * FROM tutor WHERE email=? AND name=?", email, name)) } func (r *TutorRepo) GetAll() ([]models.Tutor, error) { @@ -64,6 +44,38 @@ func (r *TutorRepo) GetAll() ([]models.Tutor, error) { } defer rows.Close() + return r.getFromRows(rows) +} +func (r *TutorRepo) Add(tutor models.Tutor) (int, error) { + //Don't add identical tutors + existentTutor, err := r.FindByNameAndEmail(tutor.Name, tutor.Email) + if errors.Is(err, sql.ErrNoRows) { + sqlResult, err := r.db.Exec("INSERT INTO `tutor` (name, email) VALUES (?,?)", tutor.Name, tutor.Email) + if err != nil { + err = fmt.Errorf("SQL-error inserting new tutor: %w", err) + log.Println(err.Error()) + return 0, err + } + id, lastInsertIdErr := sqlResult.LastInsertId() + if lastInsertIdErr != nil { + log.Printf("Error getting Id for new tutor: %s", lastInsertIdErr.Error()) + } + return int(id), err + } + return existentTutor.Id, err +} + +func (r *TutorRepo) getFromRow(row *sql.Row) (models.Tutor, error) { + var tutor models.Tutor + if err := row.Scan(&tutor.Id, &tutor.Name, &tutor.Email); err != nil { + err = fmt.Errorf("SQL-error scanning tutor row: %w", err) + log.Println(err.Error()) + return models.Tutor{}, err + } + return tutor, nil +} + +func (r *TutorRepo) getFromRows(rows *sql.Rows) ([]models.Tutor, error) { var tutors []models.Tutor for rows.Next() { var tutor models.Tutor @@ -76,16 +88,3 @@ func (r *TutorRepo) GetAll() ([]models.Tutor, error) { } return tutors, nil } -func (r *TutorRepo) Add(tutor models.Tutor) (int, error) { - //Don't add identical tutors - existentTutor, err := r.FindByNameAndEmail(tutor.Name, tutor.Email) - if errors.Is(err, sql.ErrNoRows) { - sqlResult, err := r.db.Exec("INSERT INTO `tutor` (name, email) VALUES (?,?)", tutor.Name, tutor.Email) - id, lastInsertIdErr := sqlResult.LastInsertId() - if lastInsertIdErr != nil { - log.Printf("Error getting Id for new tutor: %s", lastInsertIdErr.Error()) - } - return int(id), err - } - return existentTutor.Id, err -}