Reorganize project structure: - backend/cmd/openteam/ — entry point - backend/internal/ — core packages - backend/middleware/ — HTTP middleware - backend/router/ — route setup - backend/wire/ — dependency injection - backend/pkg/ — shared utilities - backend/go.mod, go.sum — Go module files Updated Makefile to work from backend/ directory. Removed old lowercase makefile.
69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package dao
|
|
|
|
import (
|
|
"errors"
|
|
"opencatd-open/internal/store"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type ApiKeyDAO struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewApiKeyDAO(db *gorm.DB) *ApiKeyDAO {
|
|
return &ApiKeyDAO{db: db}
|
|
}
|
|
|
|
func (d *ApiKeyDAO) Create(apiKey *store.APIKey) error {
|
|
if apiKey == nil {
|
|
return errors.New("apiKey is nil")
|
|
}
|
|
return d.db.Create(apiKey).Error
|
|
}
|
|
|
|
func (d *ApiKeyDAO) GetByID(id uint64) (*store.APIKey, error) {
|
|
var apiKey store.APIKey
|
|
err := d.db.First(&apiKey, id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &apiKey, nil
|
|
}
|
|
|
|
func (d *ApiKeyDAO) GetByHash(keyHash string) (*store.APIKey, error) {
|
|
var apiKey store.APIKey
|
|
err := d.db.Where("key_hash = ? AND status = ?", keyHash, store.KeyStatusActive).First(&apiKey).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &apiKey, nil
|
|
}
|
|
|
|
func (d *ApiKeyDAO) ListByUserID(userID uint64, limit, offset int) ([]*store.APIKey, int64, error) {
|
|
var apiKeys []*store.APIKey
|
|
var total int64
|
|
db := d.db.Where("user_id = ?", userID)
|
|
db.Model(&store.APIKey{}).Count(&total)
|
|
err := db.Limit(limit).Offset(offset).Order("created_at DESC").Find(&apiKeys).Error
|
|
return apiKeys, total, err
|
|
}
|
|
|
|
func (d *ApiKeyDAO) Update(apiKey *store.APIKey) error {
|
|
if apiKey == nil {
|
|
return errors.New("apiKey is nil")
|
|
}
|
|
return d.db.Save(apiKey).Error
|
|
}
|
|
|
|
func (d *ApiKeyDAO) Delete(id uint64) error {
|
|
return d.db.Delete(&store.APIKey{}, id).Error
|
|
}
|
|
|
|
func (d *ApiKeyDAO) BatchDelete(ids []uint64) error {
|
|
if len(ids) == 0 {
|
|
return errors.New("ids is empty")
|
|
}
|
|
return d.db.Delete(&store.APIKey{}, ids).Error
|
|
}
|