package sqlite import ( "context" "database/sql" "log" _ "modernc.org/sqlite" "uvok.de/go/training_fellow/registration" ) type SqliteRepository struct { FileName string context context.Context } func NewRepository(fileName string, context context.Context) *SqliteRepository { repo := SqliteRepository{FileName: fileName, context: context} return &repo } func (repo *SqliteRepository) SaveRegistration(registration *registration.Registration) error { db, err := repo.checkInit() if err != nil { return err } defer db.Close() _, err = db.ExecContext( repo.context, `INSERT INTO registrations (reg_id, first_name, last_name, email, company, training_code, date, privacy_policy_accepted, confirmed) VALUES (?,?,?,?,?,?,?,?,?)`, registration.RegId, registration.FirstName, registration.LastName, registration.Email, registration.Company, registration.TrainingCode, registration.Date, registration.PrivacyPolicyAccepted, registration.Confirmed) return err } func (repo *SqliteRepository) GetUnconfirmedRegistrations() ([]*registration.Registration, error) { db, err := repo.checkInit() if err != nil { return nil, err } res, err := db.QueryContext( repo.context, `SELECT reg_id, first_name, last_name, email, company, training_code, date, privacy_policy_accepted FROM registrations WHERE confirmed = 0 `) if err != nil { return nil, err } defer res.Close() regArray := make([]*registration.Registration, 0) for res.Next() { reg := registration.Registration{} err = res.Scan(®.RegId, ®.FirstName, ®.LastName, ®.Email, ®.Company, ®.TrainingCode, ®.Date, ®.PrivacyPolicyAccepted) if err != nil { log.Printf("Error scanning: %v", err) } regArray = append(regArray, ®) } return regArray, nil } func (repo *SqliteRepository) ConfirmRegistration(registrationId string) (*registration.Registration, error) { repo.checkInit() panic("not implemented") // TODO: Implement } func (repo *SqliteRepository) checkInit() (*sql.DB, error) { db, err := sql.Open("sqlite", repo.FileName) if err != nil { return nil, err } err = db.PingContext(repo.context) if err != nil { return nil, err } return db, nil }