-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathpost.go
More file actions
74 lines (63 loc) · 1.68 KB
/
Copy pathpost.go
File metadata and controls
74 lines (63 loc) · 1.68 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
package handler
import (
"context"
"net/http"
"strconv"
"github.com/labstack/echo/v4"
"github.com/labstack/echox/cookbook/twitter/model"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
func (h *Handler) CreatePost(c echo.Context) (err error) {
userID, err := bson.ObjectIDFromHex(userIDFromToken(c))
if err != nil {
return err
}
u := &model.User{
ID: userID,
}
p := &model.Post{
ID: bson.NewObjectID(),
From: u.ID.Hex(),
}
if err = c.Bind(p); err != nil {
return
}
// Validation
if p.To == "" || p.Message == "" {
return &echo.HTTPError{Code: http.StatusBadRequest, Message: "invalid to or message fields"}
}
// Find user from database by id
if err = h.Client.Database("twitter").Collection("users").FindOne(context.TODO(), bson.M{"_id": u.ID}).Decode(u); err != nil {
return err
}
// Save post in database
if _, err = h.Client.Database("twitter").Collection("posts").InsertOne(context.TODO(), p); err != nil {
return err
}
return c.JSON(http.StatusCreated, p)
}
func (h *Handler) FetchPost(c echo.Context) (err error) {
userID := userIDFromToken(c)
page, _ := strconv.Atoi(c.QueryParam("page"))
limit, _ := strconv.Atoi(c.QueryParam("limit"))
// Defaults
if page == 0 {
page = 1
}
if limit == 0 {
limit = 100
}
// Retrieve posts from database
posts := []*model.Post{}
cur, err := h.Client.Database("twitter").Collection("posts").
Find(context.TODO(), bson.M{"to": userID}, options.Find().SetSkip(int64((page-1)*limit)).SetLimit(int64(limit)))
if err != nil {
return
}
defer cur.Close(context.TODO())
if err = cur.All(context.TODO(), &posts); err != nil {
return
}
return c.JSON(http.StatusOK, posts)
}