2022-08-29 20:58:19 +00:00
|
|
|
// tutor
|
|
|
|
package repositories
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2022-09-21 20:24:08 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2022-09-20 10:21:01 +00:00
|
|
|
"officeHours/models"
|
2022-08-29 20:58:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type TutorRepo struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTutorRepo(db *sql.DB) *TutorRepo {
|
|
|
|
return &TutorRepo{
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *TutorRepo) FindByEmail(email string) ([]models.Tutor, error) {
|
|
|
|
rows, err := r.db.Query("SELECT * FROM tutor WHERE email=?", email)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2022-09-28 10:00:45 +00:00
|
|
|
return r.getFromRows(rows)
|
2022-08-29 20:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *TutorRepo) FindById(id int) (models.Tutor, error) {
|
2022-09-28 10:00:45 +00:00
|
|
|
return r.getFromRow(r.db.QueryRow("SELECT * FROM tutor WHERE id=?", id))
|
2022-08-29 20:58:19 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 20:49:14 +00:00
|
|
|
func (r *TutorRepo) FindByNameAndEmail(name string, email string) (models.Tutor, error) {
|
2022-09-28 10:00:45 +00:00
|
|
|
return r.getFromRow(r.db.QueryRow("SELECT * FROM tutor WHERE email=? AND name=?", email, name))
|
2022-08-31 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 20:58:19 +00:00
|
|
|
func (r *TutorRepo) GetAll() ([]models.Tutor, error) {
|
|
|
|
rows, err := r.db.Query("SELECT * FROM tutor")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2022-09-28 10:00:45 +00:00
|
|
|
return r.getFromRows(rows)
|
2022-08-29 20:58:19 +00:00
|
|
|
}
|
2022-09-05 15:55:08 +00:00
|
|
|
func (r *TutorRepo) Add(tutor models.Tutor) (int, error) {
|
2022-08-31 20:49:14 +00:00
|
|
|
//Don't add identical tutors
|
2022-09-05 15:55:08 +00:00
|
|
|
existentTutor, err := r.FindByNameAndEmail(tutor.Name, tutor.Email)
|
2022-09-21 20:24:08 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2024-01-03 16:04:38 +00:00
|
|
|
sqlResult, err := r.db.Exec("INSERT INTO `tutor` (name, email, subscribeToMailinglist) VALUES (?,?,?)", tutor.Name, tutor.Email, tutor.SubscribeToMailinglist)
|
2022-09-28 10:00:45 +00:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("SQL-error inserting new tutor: %w", err)
|
|
|
|
log.Println(err.Error())
|
|
|
|
return 0, err
|
|
|
|
}
|
2022-09-21 20:24:08 +00:00
|
|
|
id, lastInsertIdErr := sqlResult.LastInsertId()
|
|
|
|
if lastInsertIdErr != nil {
|
2024-12-10 19:28:29 +00:00
|
|
|
log.Printf("error getting Id for new tutor: %s", lastInsertIdErr.Error())
|
2022-09-21 20:24:08 +00:00
|
|
|
}
|
2022-09-05 15:55:08 +00:00
|
|
|
return int(id), err
|
2022-08-31 20:49:14 +00:00
|
|
|
}
|
2022-09-05 15:55:08 +00:00
|
|
|
return existentTutor.Id, err
|
2022-08-29 20:58:19 +00:00
|
|
|
}
|
2022-09-28 10:00:45 +00:00
|
|
|
|
|
|
|
func (r *TutorRepo) getFromRow(row *sql.Row) (models.Tutor, error) {
|
|
|
|
var tutor models.Tutor
|
2024-01-03 16:04:38 +00:00
|
|
|
if err := row.Scan(&tutor.Id, &tutor.Name, &tutor.Email, &tutor.SubscribeToMailinglist); err != nil {
|
2022-09-28 10:00:45 +00:00
|
|
|
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
|
2024-01-03 16:04:38 +00:00
|
|
|
if err := rows.Scan(&tutor.Id, &tutor.Name, &tutor.Email, &tutor.SubscribeToMailinglist); err != nil {
|
2024-12-10 19:28:29 +00:00
|
|
|
err = fmt.Errorf("error scanning tutor row: %w", err)
|
2022-09-28 10:00:45 +00:00
|
|
|
log.Println(err.Error())
|
|
|
|
return tutors, err
|
|
|
|
}
|
|
|
|
tutors = append(tutors, tutor)
|
|
|
|
}
|
|
|
|
return tutors, nil
|
|
|
|
}
|