Implement domain layer (TDD - Green phase)
Implementation to make all tests pass: Value Objects: - HabitType: BOOLEAN, COUNTER, VALUE with validation - Frequency: DAILY, WEEKLY, MONTHLY with validation Entities: - User: email, password hash, timezone with defaults - Habit: tracking habits with type, frequency, carry-over - HabitEntry: recording habit completions with soft delete Repositories (interfaces): - UserRepository: CRUD operations for users - HabitRepository: CRUD operations for habits - HabitEntryRepository: CRUD operations for entries Shared: - Common error definitions for domain layer All domain tests now pass.
This commit is contained in:
@@ -0,0 +1,48 @@
|
|||||||
|
package entities
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"habit-tracker-api/internal/domain/value_objects"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Habit struct {
|
||||||
|
ID string
|
||||||
|
UserID string
|
||||||
|
Name string
|
||||||
|
Description string
|
||||||
|
Type value_objects.HabitType
|
||||||
|
Frequency value_objects.Frequency
|
||||||
|
SpecificDays []int
|
||||||
|
SpecificDates []int
|
||||||
|
CarryOver bool
|
||||||
|
TargetValue *float64
|
||||||
|
CreatedAt time.Time
|
||||||
|
ArchivedAt *time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHabit(
|
||||||
|
userID string,
|
||||||
|
name string,
|
||||||
|
habitType value_objects.HabitType,
|
||||||
|
frequency value_objects.Frequency,
|
||||||
|
carryOver bool,
|
||||||
|
) *Habit {
|
||||||
|
return &Habit{
|
||||||
|
UserID: userID,
|
||||||
|
Name: name,
|
||||||
|
Type: habitType,
|
||||||
|
Frequency: frequency,
|
||||||
|
CarryOver: carryOver,
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Habit) Archive() {
|
||||||
|
now := time.Now()
|
||||||
|
h.ArchivedAt = &now
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Habit) IsActive() bool {
|
||||||
|
return h.ArchivedAt == nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package entities
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type HabitEntry struct {
|
||||||
|
ID string
|
||||||
|
HabitID string
|
||||||
|
ScheduledDate time.Time
|
||||||
|
CompletedAt time.Time
|
||||||
|
Value *float64
|
||||||
|
DeletedAt *time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHabitEntry(habitID string, scheduledDate time.Time, value *float64) *HabitEntry {
|
||||||
|
return &HabitEntry{
|
||||||
|
HabitID: habitID,
|
||||||
|
ScheduledDate: scheduledDate,
|
||||||
|
CompletedAt: time.Now(),
|
||||||
|
Value: value,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *HabitEntry) Delete() {
|
||||||
|
now := time.Now()
|
||||||
|
e.DeletedAt = &now
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *HabitEntry) IsDeleted() bool {
|
||||||
|
return e.DeletedAt != nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package entities
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
ID string
|
||||||
|
Email string
|
||||||
|
PasswordHash string
|
||||||
|
Timezone string
|
||||||
|
CreatedAt time.Time
|
||||||
|
UpdatedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUser(email, passwordHash, timezone string) *User {
|
||||||
|
now := time.Now()
|
||||||
|
return &User{
|
||||||
|
Email: email,
|
||||||
|
PasswordHash: passwordHash,
|
||||||
|
Timezone: timezone,
|
||||||
|
CreatedAt: now,
|
||||||
|
UpdatedAt: now,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package repositories
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"habit-tracker-api/internal/domain/entities"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HabitEntryRepository interface {
|
||||||
|
Create(ctx context.Context, entry *entities.HabitEntry) error
|
||||||
|
FindByID(ctx context.Context, id string) (*entities.HabitEntry, error)
|
||||||
|
FindByHabitID(ctx context.Context, habitID string) ([]*entities.HabitEntry, error)
|
||||||
|
FindByHabitIDAndDateRange(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error)
|
||||||
|
FindPendingByHabitID(ctx context.Context, habitID string, beforeDate time.Time) ([]*entities.HabitEntry, error)
|
||||||
|
Update(ctx context.Context, entry *entities.HabitEntry) error
|
||||||
|
Delete(ctx context.Context, id string) error
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package repositories
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"habit-tracker-api/internal/domain/entities"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HabitRepository interface {
|
||||||
|
Create(ctx context.Context, habit *entities.Habit) error
|
||||||
|
FindByID(ctx context.Context, id string) (*entities.Habit, error)
|
||||||
|
FindByUserID(ctx context.Context, userID string) ([]*entities.Habit, error)
|
||||||
|
FindActiveByUserID(ctx context.Context, userID string) ([]*entities.Habit, error)
|
||||||
|
Update(ctx context.Context, habit *entities.Habit) error
|
||||||
|
Delete(ctx context.Context, id string) error
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package repositories
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"habit-tracker-api/internal/domain/entities"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserRepository interface {
|
||||||
|
Create(ctx context.Context, user *entities.User) error
|
||||||
|
FindByID(ctx context.Context, id string) (*entities.User, error)
|
||||||
|
FindByEmail(ctx context.Context, email string) (*entities.User, error)
|
||||||
|
Update(ctx context.Context, user *entities.User) error
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package value_objects
|
||||||
|
|
||||||
|
type Frequency string
|
||||||
|
|
||||||
|
const (
|
||||||
|
FrequencyDaily Frequency = "DAILY"
|
||||||
|
FrequencyWeekly Frequency = "WEEKLY"
|
||||||
|
FrequencyMonthly Frequency = "MONTHLY"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (f Frequency) IsValid() bool {
|
||||||
|
switch f {
|
||||||
|
case FrequencyDaily, FrequencyWeekly, FrequencyMonthly:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package value_objects
|
||||||
|
|
||||||
|
type HabitType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
HabitTypeBoolean HabitType = "BOOLEAN"
|
||||||
|
HabitTypeCounter HabitType = "COUNTER"
|
||||||
|
HabitTypeValue HabitType = "VALUE"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (ht HabitType) IsValid() bool {
|
||||||
|
switch ht {
|
||||||
|
case HabitTypeBoolean, HabitTypeCounter, HabitTypeValue:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package errors
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrNotFound = errors.New("resource not found")
|
||||||
|
ErrAlreadyExists = errors.New("resource already exists")
|
||||||
|
ErrInvalidInput = errors.New("invalid input")
|
||||||
|
ErrUnauthorized = errors.New("unauthorized")
|
||||||
|
ErrInvalidCredentials = errors.New("invalid credentials")
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user