repo updates
This commit is contained in:
parent
55415fb5e2
commit
24f225f68b
@ -3,8 +3,12 @@ package models
|
|||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
ID string
|
ID string
|
||||||
User *User
|
SessionToken string
|
||||||
|
CsrfToken string
|
||||||
|
User *User
|
||||||
|
CreatedAt time.Time
|
||||||
|
ExpiredAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
|
@ -49,6 +49,10 @@ func (d *Database) GetUserByLogin(ctx context.Context, login string) (*models.Us
|
|||||||
return getUserByLogin(ctx, d.db, login)
|
return getUserByLogin(ctx, d.db, login)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Database) AddSession(ctx context.Context, ses *models.Session) (uint64, error) {
|
||||||
|
return addSession(ctx, d.db, ses)
|
||||||
|
}
|
||||||
|
|
||||||
func addUser(ctx context.Context, conn dbtx, login string, username string, passwordHash []byte) (uint64, error) {
|
func addUser(ctx context.Context, conn dbtx, login string, username string, passwordHash []byte) (uint64, error) {
|
||||||
const stmt = `INSERT INTO users (login,username,password)
|
const stmt = `INSERT INTO users (login,username,password)
|
||||||
VALUES ($1,$2,$3) RETURNING id`
|
VALUES ($1,$2,$3) RETURNING id`
|
||||||
@ -87,3 +91,15 @@ func getUserByLogin(ctx context.Context, conn dbtx, login string) (*models.User,
|
|||||||
|
|
||||||
return u, nil
|
return u, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func addSession(ctx context.Context, conn dbtx, session *models.Session) (uint64, error) {
|
||||||
|
const stmt = `INSERT INTO sessions (session_token, csrf_token, user_id,
|
||||||
|
created_at, expired_at) VALUES ($1, $2, $3, $4, $5) RETURNING id;`
|
||||||
|
var id uint64
|
||||||
|
row := conn.QueryRow(ctx, stmt, session.SessionToken, session.CsrfToken, session.User.ID, session.CreatedAt, session.ExpiredAt)
|
||||||
|
if err := row.Scan(&id); err != nil {
|
||||||
|
return 0, fmt.Errorf("failed to insert new session: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return id, nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user