mirror of
https://github.com/SamyRai/turash.git
synced 2025-12-26 23:01:33 +00:00
118 lines
3.1 KiB
Go
118 lines
3.1 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"bugulma/backend/internal/domain"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// ActivityLogRepository implements domain.ActivityLogRepository with GORM
|
|
type ActivityLogRepository struct {
|
|
*BaseRepository[domain.ActivityLog]
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewActivityLogRepository creates a new GORM-based activity log repository
|
|
func NewActivityLogRepository(db *gorm.DB) domain.ActivityLogRepository {
|
|
return &ActivityLogRepository{
|
|
BaseRepository: NewBaseRepository[domain.ActivityLog](db),
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
// GetByUser retrieves activity logs for a user with pagination
|
|
func (r *ActivityLogRepository) GetByUser(ctx context.Context, userID string, limit, offset int) ([]*domain.ActivityLog, int64, error) {
|
|
var activities []*domain.ActivityLog
|
|
var total int64
|
|
|
|
// Get total count
|
|
if err := r.db.WithContext(ctx).
|
|
Model(&domain.ActivityLog{}).
|
|
Where("user_id = ?", userID).
|
|
Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
// Get paginated results
|
|
result := r.db.WithContext(ctx).
|
|
Where("user_id = ?", userID).
|
|
Order("timestamp DESC").
|
|
Limit(limit).
|
|
Offset(offset).
|
|
Find(&activities)
|
|
if result.Error != nil {
|
|
return nil, 0, result.Error
|
|
}
|
|
|
|
return activities, total, nil
|
|
}
|
|
|
|
// GetByTarget retrieves activity logs for a target entity with pagination
|
|
func (r *ActivityLogRepository) GetByTarget(ctx context.Context, targetType, targetID string, limit, offset int) ([]*domain.ActivityLog, int64, error) {
|
|
var activities []*domain.ActivityLog
|
|
var total int64
|
|
|
|
// Get total count
|
|
if err := r.db.WithContext(ctx).
|
|
Model(&domain.ActivityLog{}).
|
|
Where("target_type = ? AND target_id = ?", targetType, targetID).
|
|
Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
// Get paginated results
|
|
result := r.db.WithContext(ctx).
|
|
Where("target_type = ? AND target_id = ?", targetType, targetID).
|
|
Order("timestamp DESC").
|
|
Limit(limit).
|
|
Offset(offset).
|
|
Find(&activities)
|
|
if result.Error != nil {
|
|
return nil, 0, result.Error
|
|
}
|
|
|
|
return activities, total, nil
|
|
}
|
|
|
|
// GetRecent retrieves recent activity logs
|
|
func (r *ActivityLogRepository) GetRecent(ctx context.Context, limit int) ([]*domain.ActivityLog, error) {
|
|
var activities []*domain.ActivityLog
|
|
result := r.db.WithContext(ctx).
|
|
Order("timestamp DESC").
|
|
Limit(limit).
|
|
Find(&activities)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
return activities, nil
|
|
}
|
|
|
|
// GetByAction retrieves activity logs by action type with pagination
|
|
func (r *ActivityLogRepository) GetByAction(ctx context.Context, action domain.ActivityAction, limit, offset int) ([]*domain.ActivityLog, int64, error) {
|
|
var activities []*domain.ActivityLog
|
|
var total int64
|
|
|
|
// Get total count
|
|
if err := r.db.WithContext(ctx).
|
|
Model(&domain.ActivityLog{}).
|
|
Where("action = ?", action).
|
|
Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
// Get paginated results
|
|
result := r.db.WithContext(ctx).
|
|
Where("action = ?", action).
|
|
Order("timestamp DESC").
|
|
Limit(limit).
|
|
Offset(offset).
|
|
Find(&activities)
|
|
if result.Error != nil {
|
|
return nil, 0, result.Error
|
|
}
|
|
|
|
return activities, total, nil
|
|
}
|