Verbessere Logging und Fehlerbehandlung
This commit is contained in:
parent
c737818ce4
commit
6e97d867de
14 changed files with 223 additions and 88 deletions
|
@ -3,6 +3,9 @@ package repositories
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"officeHours/models"
|
||||
)
|
||||
|
||||
|
@ -27,6 +30,8 @@ func (r *TutorRepo) FindByEmail(email string) ([]models.Tutor, error) {
|
|||
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)
|
||||
|
@ -63,23 +68,24 @@ func (r *TutorRepo) GetAll() ([]models.Tutor, error) {
|
|||
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
|
||||
}
|
||||
func (r *TutorRepo) Save(tutor models.Tutor) error {
|
||||
return nil
|
||||
}
|
||||
func (r *TutorRepo) Add(tutor models.Tutor) (int, error) {
|
||||
//Don't add identical tutors
|
||||
existentTutor, err := r.FindByNameAndEmail(tutor.Name, tutor.Email)
|
||||
if err == sql.ErrNoRows {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
sqlResult, err := r.db.Exec("INSERT INTO `tutor` (name, email) VALUES (?,?)", tutor.Name, tutor.Email)
|
||||
id, _ := sqlResult.LastInsertId()
|
||||
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
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue