chore: update session and challenge repo -- using Redis for caching session and challenge verifying -- added api handler docs
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"backend/internal/domain"
|
||||
"backend/internal/repository/storage/types"
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type SessionRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewSessionRepository(db *gorm.DB) domain.SessionRepo {
|
||||
return &SessionRepository{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *SessionRepository) Create(ctx context.Context, session *domain.UserSession) error {
|
||||
model := types.CastSessionToStorage(*session)
|
||||
return r.db.WithContext(ctx).Create(&model).Error
|
||||
}
|
||||
|
||||
func (r *SessionRepository) GetByID(ctx context.Context, id uuid.UUID) (*domain.UserSession, error) {
|
||||
var session types.UserSession
|
||||
if err := r.db.WithContext(ctx).Preload("User").First(&session, "id = ?", id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return types.CastSessionToDomain(session), nil
|
||||
}
|
||||
|
||||
func (r *SessionRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
var session types.UserSession
|
||||
if err := r.db.WithContext(ctx).First(&session, "id = ?", id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return r.db.WithContext(ctx).Delete(&session).Error
|
||||
}
|
||||
|
||||
func (r *SessionRepository) GetUserSessions(ctx context.Context, userID uuid.UUID) ([]domain.UserSession, error) {
|
||||
var sessions []types.UserSession
|
||||
if err := r.db.WithContext(ctx).Preload("User").Find(&sessions, "user_id = ?", userID).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make([]domain.UserSession, len(sessions))
|
||||
for i, session := range sessions {
|
||||
result[i] = *types.CastSessionToDomain(session)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"backend/internal/domain"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type UserSession struct {
|
||||
Base
|
||||
UserID uuid.UUID
|
||||
User *User
|
||||
WalletID string
|
||||
ExpireAt time.Time
|
||||
}
|
||||
|
||||
func CastSessionToStorage(s domain.UserSession) *UserSession {
|
||||
return &UserSession{
|
||||
Base: Base{
|
||||
ID: s.ID,
|
||||
CreatedAt: s.CreatedAt,
|
||||
UpdatedAt: s.UpdatedAt,
|
||||
DeletedAt: s.DeletedAt,
|
||||
},
|
||||
UserID: s.UserID,
|
||||
User: CastUserToStorage(s.User),
|
||||
WalletID: s.WalletID,
|
||||
ExpireAt: s.ExpiresAt,
|
||||
}
|
||||
}
|
||||
|
||||
func CastSessionToDomain(s UserSession) *domain.UserSession {
|
||||
return &domain.UserSession{
|
||||
ID: s.ID,
|
||||
UserID: s.UserID,
|
||||
User: *CastUserToDomain(*s.User),
|
||||
WalletID: s.WalletID,
|
||||
ExpiresAt: s.ExpireAt,
|
||||
CreatedAt: s.CreatedAt,
|
||||
UpdatedAt: s.UpdatedAt,
|
||||
DeletedAt: s.DeletedAt,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user