A bunch of fixes
This commit is contained in:
1 parent
65534ecfd5
commit
2a4e03e611
20 files changed
+146
-160
No files matched your search
+14
-2
@@ -22,10 +22,22 @@ func (s *Session) CreatedAtLocal() string {
|
||||
return s.CreatedAt.Local().Format("2006-01-02 15:04")
|
||||
}
|
||||
|
||||
func (s *Session) ExpiresAtLocal() string {
|
||||
return s.ExpiresAt.Local().Format("2006-01-02 15:04")
|
||||
}
|
||||
|
||||
func (i *Invite) CreatedAtLocal() string {
|
||||
return i.CreatedAt.Local().Format("2006-01-02 15:04")
|
||||
}
|
||||
|
||||
func (i *Invite) ExpiresAtLocal() string {
|
||||
return i.ExpiresAt.Local().Format("2006-01-02 15:04")
|
||||
}
|
||||
|
||||
func (s *Session) IsValid() bool {
|
||||
return time.Now().Before(s.ExpiresAt)
|
||||
return time.Now().UTC().Before(s.ExpiresAt)
|
||||
}
|
||||
|
||||
func (i *Invite) IsValid() bool {
|
||||
return time.Now().Before(i.ExpiresAt) && i.Uses > 0
|
||||
return time.Now().UTC().Before(i.ExpiresAt) && i.Uses > 0
|
||||
}
|
||||
@@ -92,6 +92,16 @@ func (r *Repository) DeleteInvite(ctx context.Context, token string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) DeleteExpiredInvites(ctx context.Context) (int64, error) {
|
||||
result, err := r.Db.ExecContext(ctx,
|
||||
"DELETE FROM invites WHERE expiresat < CURRENT_TIMESTAMP",
|
||||
)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("db delete failed: %w", err)
|
||||
}
|
||||
return result.RowsAffected()
|
||||
}
|
||||
|
||||
func (r *Repository) CreateSession(ctx context.Context, token string, userId int64, device, address string, expiresAt time.Time) (*Session, error) {
|
||||
query := "INSERT INTO sessions(token, userid, device, address, expiresat) VALUES(?, ?, ?, ?, ?) RETURNING createdat"
|
||||
|
||||
@@ -178,14 +188,12 @@ func (r *Repository) DeleteSessionsByUserId(ctx context.Context, userId int64) e
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteExpiredSessions deletes all sessions whose expiresat < now.
|
||||
// Returns the number of rows deleted.
|
||||
func (r *Repository) DeleteExpiredSessions(ctx context.Context) (int64, error) {
|
||||
result, err := r.Db.ExecContext(ctx,
|
||||
"DELETE FROM sessions WHERE expiresat < datetime('now')",
|
||||
"DELETE FROM sessions WHERE expiresat < CURRENT_TIMESTAMP",
|
||||
)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("delete expired sessions: %w", err)
|
||||
return 0, fmt.Errorf("db delete failed: %w", err)
|
||||
}
|
||||
return result.RowsAffected()
|
||||
}
|
||||
Reference in new issue
Block a user