diff options
author | uvok cheetah | 2023-12-08 17:05:40 +0000 |
---|---|---|
committer | uvok cheetah | 2023-12-08 17:05:40 +0000 |
commit | e6d7e091b7b6dde1c736e7baf01fad5fd5c1ce27 (patch) | |
tree | 38a11d4df6a3661ba9eec8d0e7c7df0d9116a19d /persistence/sqlite/sqliteRepository.go |
Initial Commit
Diffstat (limited to 'persistence/sqlite/sqliteRepository.go')
-rw-r--r-- | persistence/sqlite/sqliteRepository.go | 100 |
1 files changed, 100 insertions, 0 deletions
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 +} |