repo updates

This commit is contained in:
r8zavetr8v 2024-10-27 12:44:05 -04:00
parent 55415fb5e2
commit 24f225f68b
2 changed files with 22 additions and 2 deletions

View File

@ -3,8 +3,12 @@ package models
import "time"
type Session struct {
ID string
User *User
ID string
SessionToken string
CsrfToken string
User *User
CreatedAt time.Time
ExpiredAt time.Time
}
type User struct {

View File

@ -49,6 +49,10 @@ func (d *Database) GetUserByLogin(ctx context.Context, login string) (*models.Us
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) {
const stmt = `INSERT INTO users (login,username,password)
VALUES ($1,$2,$3) RETURNING id`
@ -87,3 +91,15 @@ func getUserByLogin(ctx context.Context, conn dbtx, login string) (*models.User,
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
}