feat: add data access layer (repositories)

- Add user repository with CRUD operations and authentication methods
- Add session repository for session management and validation
- Add API key repository for API key management
- Include proper error handling and database transaction support
- Implement search and filtering capabilities
This commit is contained in:
Björn Benouarets
2025-09-25 23:24:28 +02:00
parent 4ee00d0b97
commit 6f4e929959
3 changed files with 222 additions and 0 deletions

32
repositories/api-key.go Normal file
View File

@@ -0,0 +1,32 @@
package repositories
import (
"git.secnex.io/secnex/idp-api/models"
"gorm.io/gorm"
)
type ApiKeyRepository struct {
db *gorm.DB
}
func NewApiKeyRepository(db *gorm.DB) *ApiKeyRepository {
return &ApiKeyRepository{db: db}
}
func (r *ApiKeyRepository) GetApiKeyByID(id string) (*models.ApiKey, error) {
var apiKey models.ApiKey
err := r.db.First(&apiKey, "id = ?", id).Error
if err != nil {
return nil, err
}
return &apiKey, nil
}
func (r *ApiKeyRepository) CreateApiKey(apiKey *models.ApiKey) (*models.ApiKey, error) {
// Create and return the api key and return the database object
result := r.db.Create(apiKey)
if result.Error != nil {
return nil, result.Error
}
return apiKey, nil
}