Datenbanken, erster Versuch
This commit is contained in:
parent
b26544756a
commit
766aedf22d
21 changed files with 678 additions and 320 deletions
68
repositories/tutor.go
Normal file
68
repositories/tutor.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
// tutor
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"sprechstundentool/models"
|
||||
)
|
||||
|
||||
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()
|
||||
|
||||
var tutors []models.Tutor
|
||||
for rows.Next() {
|
||||
var tutor models.Tutor
|
||||
if err := rows.Scan(&tutor.Id, &tutor.Name, &tutor.Email); err != nil {
|
||||
return tutors, err
|
||||
}
|
||||
tutors = append(tutors, tutor)
|
||||
}
|
||||
return tutors, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (r *TutorRepo) GetAll() ([]models.Tutor, error) {
|
||||
rows, err := r.db.Query("SELECT * FROM tutor")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
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 {
|
||||
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) error {
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue