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:
32
repositories/api-key.go
Normal file
32
repositories/api-key.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user