-
Notifications
You must be signed in to change notification settings - Fork 0
/
reaction_handler.go
172 lines (142 loc) · 5.05 KB
/
reaction_handler.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
"github.com/jmoiron/sqlx"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
)
type ReactionModel struct {
ID int64 `db:"id"`
EmojiName string `db:"emoji_name"`
UserID int64 `db:"user_id"`
LivestreamID int64 `db:"livestream_id"`
CreatedAt int64 `db:"created_at"`
}
type Reaction struct {
ID int64 `json:"id"`
EmojiName string `json:"emoji_name"`
User User `json:"user"`
Livestream Livestream `json:"livestream"`
CreatedAt int64 `json:"created_at"`
}
type PostReactionRequest struct {
EmojiName string `json:"emoji_name"`
}
func getReactionsHandler(c echo.Context) error {
ctx := c.Request().Context()
if err := verifyUserSession(c); err != nil {
// echo.NewHTTPErrorが返っているのでそのまま出力
return err
}
livestreamID, err := strconv.Atoi(c.Param("livestream_id"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "livestream_id in path must be integer")
}
tx, err := dbConn.BeginTxx(ctx, nil)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to begin transaction: "+err.Error())
}
defer tx.Rollback()
query := "SELECT * FROM reactions WHERE livestream_id = ? ORDER BY created_at DESC"
if c.QueryParam("limit") != "" {
limit, err := strconv.Atoi(c.QueryParam("limit"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "limit query parameter must be integer")
}
query += fmt.Sprintf(" LIMIT %d", limit)
}
reactionModels := []ReactionModel{}
if err := tx.SelectContext(ctx, &reactionModels, query, livestreamID); err != nil {
return echo.NewHTTPError(http.StatusNotFound, "failed to get reactions")
}
reactions := make([]Reaction, len(reactionModels))
for i := range reactionModels {
reaction, err := fillReactionResponse(ctx, tx, reactionModels[i])
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to fill reaction: "+err.Error())
}
reactions[i] = reaction
}
if err := tx.Commit(); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to commit: "+err.Error())
}
return c.JSON(http.StatusOK, reactions)
}
func postReactionHandler(c echo.Context) error {
ctx := c.Request().Context()
livestreamID, err := strconv.Atoi(c.Param("livestream_id"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "livestream_id in path must be integer")
}
if err := verifyUserSession(c); err != nil {
// echo.NewHTTPErrorが返っているのでそのまま出力
return err
}
// error already checked
sess, _ := session.Get(defaultSessionIDKey, c)
// existence already checked
userID := sess.Values[defaultUserIDKey].(int64)
var req *PostReactionRequest
if err := json.NewDecoder(c.Request().Body).Decode(&req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "failed to decode the request body as json")
}
tx, err := dbConn.BeginTxx(ctx, nil)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to begin transaction: "+err.Error())
}
defer tx.Rollback()
reactionModel := ReactionModel{
UserID: int64(userID),
LivestreamID: int64(livestreamID),
EmojiName: req.EmojiName,
CreatedAt: time.Now().Unix(),
}
result, err := tx.NamedExecContext(ctx, "INSERT INTO reactions (user_id, livestream_id, emoji_name, created_at) VALUES (:user_id, :livestream_id, :emoji_name, :created_at)", reactionModel)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to insert reaction: "+err.Error())
}
reactionID, err := result.LastInsertId()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get last inserted reaction id: "+err.Error())
}
reactionModel.ID = reactionID
reaction, err := fillReactionResponse(ctx, tx, reactionModel)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to fill reaction: "+err.Error())
}
if err := tx.Commit(); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to commit: "+err.Error())
}
return c.JSON(http.StatusCreated, reaction)
}
func fillReactionResponse(ctx context.Context, tx *sqlx.Tx, reactionModel ReactionModel) (Reaction, error) {
userModel := UserModel{}
if err := tx.GetContext(ctx, &userModel, "SELECT * FROM users WHERE id = ?", reactionModel.UserID); err != nil {
return Reaction{}, err
}
user, err := fillUserResponse(ctx, tx, userModel)
if err != nil {
return Reaction{}, err
}
livestreamModel := LivestreamModel{}
if err := tx.GetContext(ctx, &livestreamModel, "SELECT * FROM livestreams WHERE id = ?", reactionModel.LivestreamID); err != nil {
return Reaction{}, err
}
livestream, err := fillLivestreamResponse(ctx, tx, livestreamModel)
if err != nil {
return Reaction{}, err
}
reaction := Reaction{
ID: reactionModel.ID,
EmojiName: reactionModel.EmojiName,
User: user,
Livestream: livestream,
CreatedAt: reactionModel.CreatedAt,
}
return reaction, nil
}