diff options
Diffstat (limited to 'persistence')
-rw-r--r-- | persistence/sqlite/sqliteRepository.go | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/persistence/sqlite/sqliteRepository.go b/persistence/sqlite/sqliteRepository.go index 7b66c80..0ae7132 100644 --- a/persistence/sqlite/sqliteRepository.go +++ b/persistence/sqlite/sqliteRepository.go @@ -3,6 +3,7 @@ package sqlite import ( "context" "database/sql" + "fmt" "log" "os" @@ -85,8 +86,57 @@ func (repo *sqliteRepository) GetUnconfirmedRegistrations() ([]*registration.Reg } func (repo *sqliteRepository) ConfirmRegistration(registrationId string) (*registration.Registration, error) { - repo.checkInit() - panic("not implemented") // TODO: Implement + db, err := repo.checkInit() + + if err != nil { + return nil, err + } + defer db.Close() + + transaction, err := db.BeginTx(repo.context, nil) + if err != nil { + return nil, err + } + defer transaction.Rollback() + + updateRes, err := transaction.Exec( + "UPDATE registrations SET confirmed=1 WHERE reg_id=? AND confirmed=0", + registrationId) + + if err != nil { + return nil, err + } + count, err := updateRes.RowsAffected() + switch { + case err != nil: + return nil, err + case count == 0: + return nil, sql.ErrNoRows + case count > 1: + newError := fmt.Errorf("more than one result for registration ID %v", registrationId) + return nil, newError + } + transaction.Commit() + + queryRes := db.QueryRowContext( + repo.context, + `SELECT reg_id, first_name, last_name, email, company, training_code, date, privacy_policy_accepted + FROM registrations + WHERE reg_id=? + `, + registrationId) + + reg, err := rowToRegistration(queryRes) + // Not included in result set + reg.Confirmed = true + switch { + // case err == sql.ErrNoRows: + // return nil, err + case err != nil: + return nil, err + default: + return reg, nil + } } func (repo *sqliteRepository) checkInit() (*sql.DB, error) { |