-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_posts.go
67 lines (58 loc) 路 1.53 KB
/
get_posts.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
64
65
66
67
package postimpl
import (
"context"
"errors"
"github.com/leaq-ru/parser/logger"
"github.com/leaq-ru/parser/post"
"github.com/leaq-ru/proto/codegen/go/parser"
"go.mongodb.org/mongo-driver/bson/primitive"
"time"
)
func (*server) GetPosts(ctx context.Context, req *parser.GetPostsRequest) (res *parser.GetPostsResponse, err error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
companyOID, err := primitive.ObjectIDFromHex(req.GetCompanyId())
if err != nil {
return
}
var excludeOIDs []primitive.ObjectID
for _, id := range req.GetOpts().GetExcludeIds() {
oID, e := primitive.ObjectIDFromHex(id)
if e != nil {
err = e
return
}
excludeOIDs = append(excludeOIDs, oID)
}
limit := uint32(20)
if req.GetOpts() != nil {
if req.GetOpts().GetLimit() > 100 || req.GetOpts().GetLimit() < 0 {
err = errors.New("limit out of 1-100")
return
} else if req.GetOpts().GetLimit() != 0 {
limit = req.GetOpts().GetLimit()
}
}
posts, err := post.Get(ctx, companyOID, req.GetOpts().GetSkip(), limit, excludeOIDs)
if err != nil {
logger.Log.Error().Err(err).Send()
return
}
res = &parser.GetPostsResponse{}
for _, item := range posts {
resPost := &parser.PostItem{
Id: item.ID.Hex(),
CompanyId: item.CompanyID.Hex(),
Date: item.Date.String(),
Text: item.Text,
}
for _, photo := range item.Photos {
resPost.Photos = append(resPost.Photos, &parser.PhotoItem{
UrlM: photo.URLm,
UrlR: photo.URLr,
})
}
res.Posts = append(res.Posts, resPost)
}
return
}