summaryrefslogtreecommitdiff
path: root/pkg/domain
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-04-25 10:10:28 -0600
committermo khan <mo@mokhan.ca>2025-04-25 10:10:28 -0600
commit00b0381dfccab2ddff7de04933fdb11b32695faf (patch)
treedb1e1f0048ba9cccdb415a714cb1344e8f47951f /pkg/domain
parent051e2435b255e0995e97d927b73f643149d9b2f3 (diff)
refactor: move id and entity to domain package
Diffstat (limited to 'pkg/domain')
-rw-r--r--pkg/domain/entity.go7
-rw-r--r--pkg/domain/id.go7
-rw-r--r--pkg/domain/sparkle.go14
-rw-r--r--pkg/domain/user.go24
-rw-r--r--pkg/domain/user_test.go24
5 files changed, 64 insertions, 12 deletions
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<sparklee>@\w+)\s+(?P<reason>.+)\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)
+ })
+ })
+}