-
Notifications
You must be signed in to change notification settings - Fork 5
/
tweet.go
63 lines (52 loc) · 1.46 KB
/
tweet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package twitter
import (
"context"
"fmt"
"strings"
"time"
)
var (
TweetMinLength = 2
TweetMaxLength = 250
)
type CreateTweetInput struct {
Body string
}
func (in *CreateTweetInput) Sanitize() {
in.Body = strings.TrimSpace(in.Body)
}
func (in CreateTweetInput) Validate() error {
if len(in.Body) < TweetMinLength {
return fmt.Errorf("%w: body not long enough, (%d) characters at least", ErrValidation, TweetMinLength)
}
if len(in.Body) > TweetMaxLength {
return fmt.Errorf("%w: body too long, (%d) characters at max", ErrValidation, TweetMaxLength)
}
return nil
}
type Tweet struct {
ID string
Body string
UserID string
ParentID *string
CreatedAt time.Time
UpdatedAt time.Time
}
func (t Tweet) CanDelete(user User) bool {
return t.UserID == user.ID
}
type TweetService interface {
All(ctx context.Context) ([]Tweet, error)
Create(ctx context.Context, input CreateTweetInput) (Tweet, error)
CreateReply(ctx context.Context, parentID string, input CreateTweetInput) (Tweet, error)
GetByID(ctx context.Context, id string) (Tweet, error)
GetByParentID(ctx context.Context, id string) ([]Tweet, error)
Delete(ctx context.Context, id string) error
}
type TweetRepo interface {
All(ctx context.Context) ([]Tweet, error)
Create(ctx context.Context, tweet Tweet) (Tweet, error)
GetByID(ctx context.Context, id string) (Tweet, error)
GetByParentID(ctx context.Context, id string) ([]Tweet, error)
Delete(ctx context.Context, id string) error
}