From 00b0381dfccab2ddff7de04933fdb11b32695faf Mon Sep 17 00:00:00 2001 From: mo khan Date: Fri, 25 Apr 2025 10:10:28 -0600 Subject: refactor: move id and entity to domain package --- pkg/domain/entity.go | 7 +++++++ pkg/domain/id.go | 7 +++++++ pkg/domain/sparkle.go | 14 ++++++++------ pkg/domain/user.go | 24 ++++++++++++++++++------ pkg/domain/user_test.go | 24 ++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 pkg/domain/entity.go create mode 100644 pkg/domain/id.go create mode 100644 pkg/domain/user_test.go (limited to 'pkg/domain') diff --git a/pkg/domain/entity.go b/pkg/domain/entity.go new file mode 100644 index 0000000..fb1cab8 --- /dev/null +++ b/pkg/domain/entity.go @@ -0,0 +1,7 @@ +package domain + +type Entity interface { + GetID() ID + SetID(id ID) error + Validate() error +} diff --git a/pkg/domain/id.go b/pkg/domain/id.go new file mode 100644 index 0000000..117a9ad --- /dev/null +++ b/pkg/domain/id.go @@ -0,0 +1,7 @@ +package domain + +type ID string + +func (id ID) String() string { + return string(id) +} diff --git a/pkg/domain/sparkle.go b/pkg/domain/sparkle.go index 3f8b4f4..5c07fbc 100644 --- a/pkg/domain/sparkle.go +++ b/pkg/domain/sparkle.go @@ -8,9 +8,11 @@ import ( ) type Sparkle struct { - ID string `json:"id" jsonapi:"primary,sparkles"` - Sparklee string `json:"sparklee" jsonapi:"attr,sparklee"` - Reason string `json:"reason" jsonapi:"attr,reason"` + ID ID `json:"id" jsonapi:"primary,sparkles"` + Sparklee string `json:"sparklee" jsonapi:"attr,sparklee"` + Recipient *User `json:"recipient" jsonapi:"attr,recipient"` + Author *User `json:"author" jsonapi:"attr,author"` + Reason string `json:"reason" jsonapi:"attr,reason"` } var SparkleRegex = regexp.MustCompile(`\A\s*(?P@\w+)\s+(?P.+)\z`) @@ -33,17 +35,17 @@ func NewSparkle(text string) (*Sparkle, error) { } return &Sparkle{ - ID: pls.GenerateULID(), + ID: ID(pls.GenerateULID()), Sparklee: matches[SparkleeIndex], Reason: matches[ReasonIndex], }, nil } -func (s *Sparkle) GetID() string { +func (s *Sparkle) GetID() ID { return s.ID } -func (s *Sparkle) SetID(id string) error { +func (s *Sparkle) SetID(id ID) error { s.ID = id return nil } diff --git a/pkg/domain/user.go b/pkg/domain/user.go index 4053e8f..5a0420b 100644 --- a/pkg/domain/user.go +++ b/pkg/domain/user.go @@ -1,22 +1,34 @@ package domain type User struct { - ID string `json:"id" jsonapi:"primary,users"` + ID ID `json:"id" jsonapi:"primary,users"` + Username string `json:"username" jsonapi:"attr,username"` + Email string `json:"email" jsonapi:"attr,email"` + ProfileURL string `json:"profile" jsonapi:"attr,profile"` + Picture string `json:"picture" jsonapi:"attr,picture"` } func NewUser() *User { return &User{} } -func (s *User) GetID() string { - return s.ID +func (u *User) GetID() ID { + return u.ID } -func (s *User) SetID(id string) error { - s.ID = id +func (u *User) SetID(id ID) error { + u.ID = id return nil } -func (s *User) Validate() error { +func (u *User) Validate() error { return nil } + +func (self *User) Sparkle(recipient *User, reason string) *Sparkle { + return &Sparkle{ + Recipient: recipient, + Author: self, + Reason: reason, + } +} diff --git a/pkg/domain/user_test.go b/pkg/domain/user_test.go new file mode 100644 index 0000000..dbdba6d --- /dev/null +++ b/pkg/domain/user_test.go @@ -0,0 +1,24 @@ +package domain + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestUser(t *testing.T) { + t.Run("Sparkle", func(t *testing.T) { + t.Run("returns a new Sparkle", func(t *testing.T) { + tanuki := &User{} + user := &User{} + + sparkle := user.Sparkle(tanuki, "for helping me with my homework") + + require.NotNil(t, sparkle) + assert.Equal(t, tanuki, sparkle.Recipient) + assert.Equal(t, "for helping me with my homework", sparkle.Reason) + assert.Equal(t, user, sparkle.Author) + }) + }) +} -- cgit v1.2.3