-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.go
51 lines (38 loc) · 1.07 KB
/
post.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
package service
import (
"errors"
"time"
"unicode/utf8"
"github.com/novadinn/social-network/model"
)
var (
ErrInvalidPostContent = errors.New("invalid post content")
ErrUnauthenticated = errors.New("unauthenticated")
)
func (svc *Service) CreatePost(content, userID string, loggedIn bool) error {
if content == "" || utf8.RuneCountInString(content) > 1023 {
return ErrInvalidPostContent
}
if !loggedIn {
// TODO: this is not working
return ErrUnauthenticated
}
id := genID()
createdAt := time.Now()
if err := svc.Queries.CreatePost(id, userID, content, createdAt); err != nil {
return err
}
return nil
}
func (svc *Service) GetPosts() ([]model.Post, error) {
return svc.Queries.GetPosts()
}
func (svc *Service) GetFollowingPosts(ids []interface{}) ([]model.Post, error) {
return svc.Queries.GetFollowingPosts(ids)
}
func (svc *Service) GetPostsByUsername(username string) ([]model.Post, error) {
return svc.Queries.GetPostsByUsername(username)
}
func (svc *Service) GetPostByID(id string) (model.Post, error) {
return svc.Queries.GetPostByID(id)
}