aboutsummaryrefslogtreecommitdiff
path: root/pkg/database/sessions.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/database/sessions.go')
-rw-r--r--pkg/database/sessions.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/pkg/database/sessions.go b/pkg/database/sessions.go
new file mode 100644
index 0000000..72930f0
--- /dev/null
+++ b/pkg/database/sessions.go
@@ -0,0 +1,33 @@
+package database
+
+import (
+ "git.adyxax.org/adyxax/trains/pkg/model"
+ "github.com/google/uuid"
+)
+
+func (env *DBEnv) CreateSession(user *model.User) (*string, error) {
+ token := uuid.NewString()
+
+ query := `
+ INSERT INTO sessions
+ (token, user_id)
+ VALUES
+ ($1, $2);`
+ tx, err := env.db.Begin()
+ if err != nil {
+ return nil, newTransactionError("Could not Begin()", err)
+ }
+ _, err = tx.Exec(
+ query,
+ token,
+ user.Id,
+ )
+ if err != nil {
+ tx.Rollback()
+ return nil, newQueryError("Could not run database query: most likely the token already exists in database, or the user id does not exist", err)
+ }
+ if err := tx.Commit(); err != nil {
+ return nil, newTransactionError("Could not commit transaction", err)
+ }
+ return &token, nil
+}