-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomment.go
More file actions
81 lines (66 loc) · 2.43 KB
/
comment.go
File metadata and controls
81 lines (66 loc) · 2.43 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package pa
import (
"context"
"time"
)
// Comment represents a comment in the system.
type Comment struct {
// the pk of the comment.
ID int `json:"id"`
// linking fields of the comment.
SubBlogID int `json:"subBlogID"`
UserID int `json:"userID"`
User *User `json:"user"`
// content of the comment.
Content string `json:"content"`
// timestamp.
CreatedAt time.Time `json:"createdAt"`
}
// Validate performs basic validation on the comment.
// returns EINVALID if any error is found.
func (c *Comment) Validate() error {
if c.Content == "" {
return Errorf(EINVALID, "content is a required field.")
}
if c.SubBlogID == 0 {
return Errorf(EINVALID, "comment must be linked to a sub blog.")
}
if c.UserID == 0 {
return Errorf(EINVALID, "comment must be linked to a user.")
}
return nil
}
// CommentService represents a service which manages comments in the system.
type CommentService interface {
// FindCommentByID returns a comment based on the id.
// returns ENOTFOUND if the comment doesent exist.
FindCommentByID(ctx context.Context, id int) (*Comment, error)
// FindComments returns a range of comments and the length of the range. If filter
// is specified FindComments will apply the filter to return set response.
FindComments(ctx context.Context, filter CommentFilter) ([]*Comment, int, error)
// CreateComment creates a comment.
CreateComment(ctx context.Context, comment *Comment) error
// UpdateComment updates a comment based on the update field.
// returns ENOTFOUND if the comment doesent exist.
// returns EUNAUTHORIZED if used by anyone other then the adim user.
UpdateComment(ctx context.Context, id int, update CommentUpdate) (*Comment, error)
// DeleteComment permanently deletes a comment.
// returns ENOTFOUND if comment doesent exist.
// returns EUNAUTHORIZED if used by anyone other then the user owning the comment.
DeleteComment(ctx context.Context, id int) error
}
// CommentFilter represents a filter used by FindComments to filter the response.
type CommentFilter struct {
// fields to filter on.
ID *int `json:"id"`
SubBlogID *int `json:"SubBlogID"`
UserID *int `json:"userID"`
// restrictions on the result set, used for pagination and set limits.
Offset int `json:"offset"`
Limit int `json:"limit"`
}
// CommentUpdate represents an update used by UpdateComment to update a comment.
type CommentUpdate struct {
// fields which can be updated.
Content *string `json:"content"`
}