summaryrefslogtreecommitdiff
path: root/persistence/sqlite/sqliteRepository.go
diff options
context:
space:
mode:
authoruvok cheetah2023-12-09 17:57:32 +0000
committeruvok cheetah2023-12-09 17:57:32 +0000
commit6f11963d8c41be7176bd48bd192c526cb7c189a5 (patch)
tree9fed50bf86c7ee420694957ba5c0b8fb59212c0a /persistence/sqlite/sqliteRepository.go
parent1045770f2beb282143e443a6e7742c9cb5e2945e (diff)
Implement confirm functionHEADmaster
Diffstat (limited to 'persistence/sqlite/sqliteRepository.go')
-rw-r--r--persistence/sqlite/sqliteRepository.go54
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) {