-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomment.go
More file actions
207 lines (161 loc) · 4.29 KB
/
Copy pathcomment.go
File metadata and controls
207 lines (161 loc) · 4.29 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"database/sql"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
"time"
)
type Comment struct {
ID int `json:"id,omitempty"`
UserId string `json:"user_id,omitempty"`
Body string `json:"body,omitempty"`
IssueId int `json:"issue_id,omitempty"`
RepoId string `json:"repo_id,omitempty"`
User User `json:"user,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}
func GetComment(db *sql.DB, id int) (Comment, error) {
var userId, body, repoId, createdAt, updatedAt string
var issueId int
row := db.QueryRow("SELECT id,user_id,body,issue_id,repo_id,created_at, updated_at FROM comments WHERE id=$1", id)
err := row.Scan(&id, &userId, &body, &issueId, &repoId, &createdAt, &updatedAt)
if err != nil {
return Comment{}, err
}
CreatedAt, err := time.Parse(time.RFC3339, createdAt)
if err != nil {
return Comment{}, err
}
UpdatedAt, err := time.Parse(time.RFC3339, updatedAt)
if err != nil {
return Comment{}, err
}
comment := Comment{
ID: id,
UserId: userId,
Body: body,
IssueId: issueId,
RepoId: repoId,
CreatedAt: CreatedAt,
UpdatedAt: UpdatedAt,
}
return comment, nil
}
func CreateComment(db *sql.DB, comment Comment) error {
_, err := db.Exec(`INSERT INTO comments(user_id,body,issue_id,repo_id,created_at, updated_at)
VALUES($1,$2,$3,$4,$5,$6)`,
comment.UserId,
comment.Body,
comment.IssueId,
comment.RepoId,
time.Now().Format(time.RFC3339),
time.Now().Format(time.RFC3339))
if err != nil {
return err
}
return nil
}
func UpdateComment(db *sql.DB, comment Comment) error {
_, err := db.Exec(`UPDATE comments SET body =$1, updated_at =$2 WHERE id = $3`,
comment.Body, time.Now().Format(time.RFC3339), comment.ID)
if err != nil {
return err
}
return nil
}
func DeleteComment(db *sql.DB, id int) error {
_, err := db.Exec(`DELETE FROM comments WHERE id = $1`, id)
if err != nil {
return err
}
return nil
}
func getCommentHandler(c *gin.Context, db *sql.DB) {
id := c.Param("id")
Id, err := strconv.Atoi(id)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
comment, err := GetComment(db, Id)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.JSON(http.StatusOK, comment)
}
func postCommentHandler(c *gin.Context, db *sql.DB) {
userId, err := authorization(c, db)
if err != nil {
return
}
username := c.Param("owner")
repoName := c.Param("repo")
issueNumber := c.Param("issue_number")
var repoId string
var issueId int
row := db.QueryRow(`WITH repo_cte AS(
SELECT repos.id FROM repos JOIN users ON repos.user_id= users.id
WHERE repos.name= $1 AND users.username= $2
)
SELECT id,repo_id FROM issues
WHERE repo_id in ( select id from repo_cte )
AND issue_number = $3`,
repoName, username, issueNumber)
err = row.Scan(&issueId, &repoId)
if err != nil {
fmt.Println(err)
return
}
comment := Comment{}
err = c.BindJSON(&comment)
if err != nil {
fmt.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
comment.UserId = userId
comment.RepoId = repoId
comment.IssueId = issueId
err = CreateComment(db, comment)
if err != nil {
fmt.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Status(http.StatusCreated)
}
func putCommentHandler(c *gin.Context, db *sql.DB) {
comment := Comment{}
err := c.BindJSON(&comment)
if err != nil {
fmt.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
err = UpdateComment(db, comment)
if err != nil {
fmt.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Status(http.StatusNoContent)
}
func deleteCommentHandler(c *gin.Context, db *sql.DB) {
id := c.Param("id")
Id, err := strconv.Atoi(id)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
err = DeleteComment(db, Id)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
// c.JSON(http.StatusOK, repo)
c.Status(http.StatusNoContent)
}