init
This commit is contained in:
67
internal/repository/app_lock_repository.go
Normal file
67
internal/repository/app_lock_repository.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"accounting-app/internal/models"
|
||||
"errors"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// AppLockRepository handles database operations for app lock
|
||||
type AppLockRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewAppLockRepository creates a new app lock repository
|
||||
func NewAppLockRepository(db *gorm.DB) *AppLockRepository {
|
||||
return &AppLockRepository{db: db}
|
||||
}
|
||||
|
||||
// GetOrCreate retrieves the app lock settings or creates default settings if none exist
|
||||
func (r *AppLockRepository) GetOrCreate(userID uint) (*models.AppLock, error) {
|
||||
var appLock models.AppLock
|
||||
|
||||
// Try to get existing app lock for the user
|
||||
err := r.db.Where("user_id = ?", userID).First(&appLock).Error
|
||||
if err == nil {
|
||||
return &appLock, nil
|
||||
}
|
||||
|
||||
// If not found, create default settings
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
appLock = models.AppLock{
|
||||
UserID: userID,
|
||||
IsEnabled: false,
|
||||
FailedAttempts: 0,
|
||||
}
|
||||
if err := r.db.Create(&appLock).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &appLock, nil
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Update updates the app lock settings
|
||||
func (r *AppLockRepository) Update(appLock *models.AppLock) error {
|
||||
return r.db.Save(appLock).Error
|
||||
}
|
||||
|
||||
// IncrementFailedAttempts increments the failed attempts counter
|
||||
func (r *AppLockRepository) IncrementFailedAttempts(appLock *models.AppLock) error {
|
||||
return r.db.Model(appLock).Updates(map[string]interface{}{
|
||||
"failed_attempts": appLock.FailedAttempts,
|
||||
"last_failed_attempt": appLock.LastFailedAttempt,
|
||||
"locked_until": appLock.LockedUntil,
|
||||
}).Error
|
||||
}
|
||||
|
||||
// ResetFailedAttempts resets the failed attempts counter
|
||||
func (r *AppLockRepository) ResetFailedAttempts(appLock *models.AppLock) error {
|
||||
return r.db.Model(appLock).Updates(map[string]interface{}{
|
||||
"failed_attempts": 0,
|
||||
"last_failed_attempt": nil,
|
||||
"locked_until": nil,
|
||||
}).Error
|
||||
}
|
||||
Reference in New Issue
Block a user