From e6d7e091b7b6dde1c736e7baf01fad5fd5c1ce27 Mon Sep 17 00:00:00 2001 From: uvok cheetah Date: Fri, 8 Dec 2023 17:05:40 +0000 Subject: Initial Commit --- persistence/sqlite/0001_Create_Initial_Tables.sql | 13 +++ persistence/sqlite/sqliteRepository.go | 100 ++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 persistence/sqlite/0001_Create_Initial_Tables.sql create mode 100644 persistence/sqlite/sqliteRepository.go (limited to 'persistence') diff --git a/persistence/sqlite/0001_Create_Initial_Tables.sql b/persistence/sqlite/0001_Create_Initial_Tables.sql new file mode 100644 index 0000000..d563e4a --- /dev/null +++ b/persistence/sqlite/0001_Create_Initial_Tables.sql @@ -0,0 +1,13 @@ +CREATE TABLE IF NOT EXISTS registrations ( + reg_id TEXT PRIMARY KEY NOT NULL, + first_name TEXT NOT NULL, + last_name TEXT NOT NULL, + email TEXT NOT NULL, + company TEXT, + training_code TEXT, + date TEXT NOT NULL, + privacy_policy_accepted INTEGER NOT NULL, + confirmed INTEGER NOT NULL +); + +PRAGMA user_version = 1 diff --git a/persistence/sqlite/sqliteRepository.go b/persistence/sqlite/sqliteRepository.go new file mode 100644 index 0000000..61e89d8 --- /dev/null +++ b/persistence/sqlite/sqliteRepository.go @@ -0,0 +1,100 @@ +package persistence + +import ( + "context" + "database/sql" + "log" + + _ "modernc.org/sqlite" + + "uvok.de/go/training_fellow/registration" +) + +var ( + debug bool = true +) + +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() + + res, 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) + + if debug { + log.Printf("SQL result: %v", res) + } + 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 +} -- cgit v1.2.3