|
| 1 | +package ddd |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/google/uuid" |
| 7 | + |
| 8 | + "github.com/theandrew168/bloggulus/backend/timeutil" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + // Ensure that a post doesn't get synced more than once every SyncCooldown. |
| 13 | + SyncCooldown = 2 * time.Hour |
| 14 | +) |
| 15 | + |
| 16 | +type Blog struct { |
| 17 | + id uuid.UUID |
| 18 | + feedURL string |
| 19 | + siteURL string |
| 20 | + title string |
| 21 | + etag string |
| 22 | + lastModified string |
| 23 | + syncedAt time.Time |
| 24 | + |
| 25 | + createdAt time.Time |
| 26 | + updatedAt time.Time |
| 27 | +} |
| 28 | + |
| 29 | +func NewBlog(feedURL, siteURL, title string) (*Blog, error) { |
| 30 | + now := timeutil.Now() |
| 31 | + blog := Blog{ |
| 32 | + id: uuid.New(), |
| 33 | + feedURL: feedURL, |
| 34 | + siteURL: siteURL, |
| 35 | + title: title, |
| 36 | + |
| 37 | + createdAt: now, |
| 38 | + updatedAt: now, |
| 39 | + } |
| 40 | + return &blog, nil |
| 41 | +} |
| 42 | + |
| 43 | +func LoadBlog(id uuid.UUID, feedURL, siteURL, title, etag, lastModified string, syncedAt, createdAt, updatedAt time.Time) *Blog { |
| 44 | + blog := Blog{ |
| 45 | + id: id, |
| 46 | + feedURL: feedURL, |
| 47 | + siteURL: siteURL, |
| 48 | + title: title, |
| 49 | + etag: etag, |
| 50 | + lastModified: lastModified, |
| 51 | + syncedAt: syncedAt, |
| 52 | + |
| 53 | + createdAt: createdAt, |
| 54 | + updatedAt: updatedAt, |
| 55 | + } |
| 56 | + return &blog |
| 57 | +} |
| 58 | + |
| 59 | +func (b *Blog) ID() uuid.UUID { |
| 60 | + return b.id |
| 61 | +} |
| 62 | + |
| 63 | +func (b *Blog) FeedURL() string { |
| 64 | + return b.feedURL |
| 65 | +} |
| 66 | + |
| 67 | +func (b *Blog) SiteURL() string { |
| 68 | + return b.siteURL |
| 69 | +} |
| 70 | + |
| 71 | +func (b *Blog) Title() string { |
| 72 | + return b.title |
| 73 | +} |
| 74 | + |
| 75 | +func (b *Blog) ETag() string { |
| 76 | + return b.etag |
| 77 | +} |
| 78 | + |
| 79 | +func (b *Blog) SetETag(etag string) error { |
| 80 | + b.etag = etag |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +func (b *Blog) LastModified() string { |
| 85 | + return b.lastModified |
| 86 | +} |
| 87 | + |
| 88 | +func (b *Blog) SetLastModified(lastModified string) error { |
| 89 | + b.lastModified = lastModified |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +func (b *Blog) SyncedAt() time.Time { |
| 94 | + return b.syncedAt |
| 95 | +} |
| 96 | + |
| 97 | +func (b *Blog) SetSyncedAt(syncedAt time.Time) error { |
| 98 | + b.syncedAt = syncedAt |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +func (b *Blog) CanBeSynced(now time.Time) bool { |
| 103 | + return b.syncedAt.Add(SyncCooldown).Before(now) |
| 104 | +} |
| 105 | + |
| 106 | +func (b *Blog) CreatedAt() time.Time { |
| 107 | + return b.createdAt |
| 108 | +} |
| 109 | + |
| 110 | +func (b *Blog) UpdatedAt() time.Time { |
| 111 | + return b.updatedAt |
| 112 | +} |
| 113 | + |
| 114 | +func (b *Blog) SetUpdatedAt(updatedAt time.Time) error { |
| 115 | + b.updatedAt = updatedAt |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +func (b *Blog) CheckDelete() error { |
| 120 | + return nil |
| 121 | +} |
0 commit comments