aa8f7af55d
Add comprehensive offline synchronization support for habits and entries: ## Infrastructure (Phase 1) - Add UpdatedAt and DeletedAt timestamps to Habit and HabitEntry entities - Implement soft delete with Delete(), Touch(), and IsDeleted() methods - Create SQL migration with optimized composite indexes for sync queries - Add GetChangesSince() and SoftDelete() to both repositories - Update all Find* methods to exclude soft-deleted records - 13 comprehensive TDD tests for sync repository methods ## HTTP Endpoints (Phase 2) - GET /api/v1/sync/changes: retrieve all changes since timestamp - POST /api/v1/sync/batch: apply client changes with conflict resolution - Implement Last-Write-Wins strategy using UpdatedAt timestamps - Add authentication and rate limiting (100 req/min) - Validate user ownership for all sync operations - 9 tests for sync handlers (3 queries + 6 commands) ## Technical Details - Composite indexes: (user_id, updated_at) for optimal query performance - No pagination: atomic sync operations for data consistency - Upsert behavior: create resources if not found on server - DTOs with full entity state including timestamps - Swagger documentation updated for new endpoints All 220+ tests passing ✓
77 lines
1.6 KiB
Go
77 lines
1.6 KiB
Go
package queries
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"apocapoc-api/internal/domain/entities"
|
|
"apocapoc-api/internal/domain/repositories"
|
|
"apocapoc-api/internal/shared/errors"
|
|
)
|
|
|
|
type HabitChangesDTO struct {
|
|
Created []*entities.Habit
|
|
Updated []*entities.Habit
|
|
Deleted []string
|
|
}
|
|
|
|
type EntryChangesDTO struct {
|
|
Created []*entities.HabitEntry
|
|
Updated []*entities.HabitEntry
|
|
Deleted []string
|
|
}
|
|
|
|
type SyncChangesDTO struct {
|
|
Habits HabitChangesDTO
|
|
Entries EntryChangesDTO
|
|
}
|
|
|
|
type GetSyncChangesQuery struct {
|
|
UserID string
|
|
Since time.Time
|
|
}
|
|
|
|
type GetSyncChangesHandler struct {
|
|
habitRepo repositories.HabitRepository
|
|
entryRepo repositories.HabitEntryRepository
|
|
}
|
|
|
|
func NewGetSyncChangesHandler(
|
|
habitRepo repositories.HabitRepository,
|
|
entryRepo repositories.HabitEntryRepository,
|
|
) *GetSyncChangesHandler {
|
|
return &GetSyncChangesHandler{
|
|
habitRepo: habitRepo,
|
|
entryRepo: entryRepo,
|
|
}
|
|
}
|
|
|
|
func (h *GetSyncChangesHandler) Handle(ctx context.Context, query GetSyncChangesQuery) (*SyncChangesDTO, error) {
|
|
if query.UserID == "" {
|
|
return nil, errors.ErrInvalidInput
|
|
}
|
|
|
|
habitChanges, err := h.habitRepo.GetChangesSince(ctx, query.UserID, query.Since)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
entryChanges, err := h.entryRepo.GetChangesSince(ctx, query.UserID, query.Since)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &SyncChangesDTO{
|
|
Habits: HabitChangesDTO{
|
|
Created: habitChanges.Created,
|
|
Updated: habitChanges.Updated,
|
|
Deleted: habitChanges.Deleted,
|
|
},
|
|
Entries: EntryChangesDTO{
|
|
Created: entryChanges.Created,
|
|
Updated: entryChanges.Updated,
|
|
Deleted: entryChanges.Deleted,
|
|
},
|
|
}, nil
|
|
}
|