Verbessere Fehlerbehandlung bei Tutoren und Sprechstunden
This commit is contained in:
parent
58eb848f20
commit
c8f85e40c4
2 changed files with 38 additions and 39 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue