Added activities module
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"FamilyHub/src/domain"
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ActivityRepository interface {
|
||||
Create(ctx context.Context, activity *domain.ActivityLog) error
|
||||
List(ctx context.Context, filter domain.ActivityLogListFilter) ([]*domain.ActivityLog, error)
|
||||
}
|
||||
|
||||
type ActivitySQLRepository struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewActivitySQLRepository(db *sql.DB) *ActivitySQLRepository {
|
||||
return &ActivitySQLRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *ActivitySQLRepository) Create(ctx context.Context, activity *domain.ActivityLog) error {
|
||||
query := `
|
||||
INSERT INTO activity_logs (family_id, user_id, action, entity_type, entity_id, description)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING id, created_at
|
||||
`
|
||||
|
||||
return r.db.QueryRowContext(
|
||||
ctx,
|
||||
query,
|
||||
activity.FamilyID,
|
||||
activity.UserID,
|
||||
activity.Action,
|
||||
activity.EntityType,
|
||||
activity.EntityID,
|
||||
activity.Description,
|
||||
).Scan(&activity.ID, &activity.CreatedAt)
|
||||
}
|
||||
|
||||
func (r *ActivitySQLRepository) List(ctx context.Context, filter domain.ActivityLogListFilter) ([]*domain.ActivityLog, error) {
|
||||
var (
|
||||
whereClauses []string
|
||||
args []any
|
||||
)
|
||||
|
||||
appendFilter := func(condition string, value any) {
|
||||
args = append(args, value)
|
||||
whereClauses = append(whereClauses, fmt.Sprintf(condition, len(args)))
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT id, family_id, user_id, action, entity_type, entity_id, description, created_at
|
||||
FROM activity_logs
|
||||
`
|
||||
|
||||
if filter.FamilyID != nil {
|
||||
appendFilter("family_id = $%d", *filter.FamilyID)
|
||||
}
|
||||
if filter.UserID != nil {
|
||||
appendFilter("user_id = $%d", *filter.UserID)
|
||||
}
|
||||
|
||||
if len(whereClauses) > 0 {
|
||||
query += " WHERE " + strings.Join(whereClauses, " AND ")
|
||||
}
|
||||
|
||||
args = append(args, filter.Limit, filter.Offset)
|
||||
query += fmt.Sprintf(" ORDER BY created_at DESC LIMIT $%d OFFSET $%d", len(args)-1, len(args))
|
||||
|
||||
rows, err := r.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var activities []*domain.ActivityLog
|
||||
for rows.Next() {
|
||||
var activity domain.ActivityLog
|
||||
if err := rows.Scan(
|
||||
&activity.ID,
|
||||
&activity.FamilyID,
|
||||
&activity.UserID,
|
||||
&activity.Action,
|
||||
&activity.EntityType,
|
||||
&activity.EntityID,
|
||||
&activity.Description,
|
||||
&activity.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
activities = append(activities, &activity)
|
||||
}
|
||||
|
||||
return activities, rows.Err()
|
||||
}
|
||||
Reference in New Issue
Block a user