Complete CRUD operations for habits
Implement all missing endpoints for full habit management:
- GET /api/v1/habits - List all user habits
- GET /api/v1/habits/{id} - Get specific habit
- PUT /api/v1/habits/{id} - Update habit
- DELETE /api/v1/habits/{id} - Archive habit (soft delete)
- GET /api/v1/habits/{id}/entries - Get habit entry history
- DELETE /api/v1/habits/{id}/entries/{date} - Unmark habit (soft delete entry)
All endpoints include:
- TDD approach with comprehensive test coverage
- JWT authentication and ownership validation
- Proper error handling (404, 403, 400, 500)
- Clean architecture with separated commands/queries
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"apocapoc-api/internal/domain/repositories"
|
||||
"apocapoc-api/internal/shared/errors"
|
||||
)
|
||||
|
||||
type ArchiveHabitCommand struct {
|
||||
HabitID string
|
||||
UserID string
|
||||
}
|
||||
|
||||
type ArchiveHabitHandler struct {
|
||||
habitRepo repositories.HabitRepository
|
||||
}
|
||||
|
||||
func NewArchiveHabitHandler(habitRepo repositories.HabitRepository) *ArchiveHabitHandler {
|
||||
return &ArchiveHabitHandler{
|
||||
habitRepo: habitRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *ArchiveHabitHandler) Handle(ctx context.Context, cmd ArchiveHabitCommand) error {
|
||||
// Find existing habit
|
||||
habit, err := h.habitRepo.FindByID(ctx, cmd.HabitID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Check ownership
|
||||
if habit.UserID != cmd.UserID {
|
||||
return errors.ErrUnauthorized
|
||||
}
|
||||
|
||||
// Archive the habit (idempotent operation)
|
||||
habit.Archive()
|
||||
|
||||
// Save changes
|
||||
return h.habitRepo.Update(ctx, habit)
|
||||
}
|
||||
Reference in New Issue
Block a user