aboutsummaryrefslogtreecommitdiff
path: root/pkg/database/sessions.go
blob: 72930f0f60bbfbcc2944357ac259c4df262fcb9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
}