This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
/
post.go
419 lines (343 loc) · 9.86 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package db
import (
"context"
"fmt"
"github.com/go-openapi/strfmt"
"github.com/jackc/pgx"
"github.com/jackc/pgx/pgtype"
"github.com/oleggator/tp-db/models"
"strconv"
"time"
)
func CreatePosts(srcPosts []models.Post, threadSlug string) (posts []models.Post, status int) {
var (
forumId int
forumSlug string
threadId int32
err error
)
if threadId64, parseErr := strconv.ParseInt(threadSlug, 0, 32); parseErr == nil {
threadId = int32(threadId64)
err = conn.QueryRow(`thread_by_id`, threadId).Scan(&forumId, &forumSlug)
} else {
err = conn.QueryRow(`thread_by_slug`, threadSlug).Scan(&forumId, &forumSlug, &threadId)
}
if err != nil {
return nil, 404
}
if len(srcPosts) == 0 {
return nil, 201
}
batch := conn.BeginBatch()
for i, _ := range srcPosts {
batch.Queue(
`select check_parent($1, $2)`,
[]interface{}{threadId, srcPosts[i].Parent},
[]pgtype.OID{pgtype.Int4OID, pgtype.Int8OID},
[]int16{pgx.BinaryFormatCode},
)
}
err = batch.Send(context.Background(), &pgx.TxOptions{IsoLevel: pgx.ReadUncommitted, AccessMode: pgx.ReadOnly})
for _, _ = range srcPosts {
var status int
err = batch.QueryRowResults().Scan(&status)
if err != nil || status != 201 {
batch.Close()
return nil, 409
}
}
batch.Close()
postsIds := make([]int64, 0, len(srcPosts))
err = conn.QueryRow(`get_ids`, len(srcPosts)).Scan(&postsIds)
batch = conn.BeginBatch()
for i, _ := range srcPosts {
if srcPosts[i].Parent != 0 {
batch.Queue(
`get_parents`,
[]interface{}{srcPosts[i].Parent},
[]pgtype.OID{pgtype.Int8OID},
[]int16{pgx.BinaryFormatCode},
)
}
}
err = batch.Send(context.Background(), &pgx.TxOptions{IsoLevel: pgx.ReadUncommitted, AccessMode: pgx.ReadOnly})
parents := make([][]int64, len(srcPosts))
for i, _ := range srcPosts {
if srcPosts[i].Parent != 0 {
err = batch.QueryRowResults().Scan(&parents[i])
}
parents[i] = append(parents[i], postsIds[i])
}
batch.Close()
tx, _ := conn.Begin()
batch = tx.BeginBatch()
for i, _ := range srcPosts {
srcPosts[i].ID = postsIds[i]
srcPosts[i].Thread = threadId
srcPosts[i].Forum = forumSlug
batch.Queue(
`insert_post`,
[]interface{}{srcPosts[i].Author, srcPosts[i].Message, threadId, srcPosts[i].IsEdited, forumId,
srcPosts[i].Created, srcPosts[i].Parent, parents[i], parents[i][0], srcPosts[i].ID, forumSlug},
nil,
nil,
)
}
batch.Send(context.Background(), nil)
_, err = batch.ExecResults()
if err != nil {
tx.Rollback()
return nil, 404
}
batch.Close()
tx.Commit()
batch = conn.BeginBatch()
for i, _ := range srcPosts {
batch.Queue(
`
with s as (
select $1, about, email, fullname, $2 from "User"
where nickname=$2
)
insert into ForumUser (slug, about, email, fullname, nickname)
select * from s
on conflict do nothing
`,
[]interface{}{forumSlug, srcPosts[i].Author},
[]pgtype.OID{pgtype.VarcharOID, pgtype.VarcharOID},
nil,
)
}
err = batch.Send(context.Background(), nil)
_, err = batch.ExecResults()
batch.Close()
conn.Exec(`update forum set postsCount=postsCount+$1 where id=$2`, len(srcPosts), forumId)
return srcPosts, 201
}
func GetPost(postId int64, withAuthor bool, withThread bool, withForum bool) (postInfo *models.PostFull, status int) {
postInfo = &models.PostFull{}
post := models.Post{}
postInfo.Post = &post
post.ID = postId
var (
created time.Time
forumId int32
userId int32
)
err := conn.QueryRow(`
select authorNickname, created, forumSlug, isEdited, message, "thread", forum, author, coalesce(parent, 0)
from Post
where post.id = $1
`, postId).Scan(&post.Author, &created, &post.Forum, &post.IsEdited, &post.Message, &post.Thread, &forumId, &userId, &post.Parent)
if err != nil {
return nil, 404
}
post.Created = (*strfmt.DateTime)(&created)
if withForum {
forum := models.Forum{}
forum.Slug = post.Forum
postInfo.Forum = &forum
err := conn.QueryRow(`
select forum.title, moderatorNickname, postsCount, threadsCount from forum
where forum.id=$1
`, forumId).Scan(&forum.Title, &forum.User, &forum.Posts, &forum.Threads)
if err != nil {
return nil, 404
}
}
if withThread {
var created time.Time
thread := models.Thread{}
thread.ID = post.Thread
postInfo.Thread = &thread
err = conn.QueryRow(`
select authorNickname, created, forumSlug, message, coalesce(slug, ''), title, votes
from thread
where thread.id = $1
`, post.Thread).Scan(&thread.Author, &created, &thread.Forum, &thread.Message, &thread.Slug, &thread.Title, &thread.Votes)
if err != nil {
return nil, 404
}
thread.Created = (*strfmt.DateTime)(&created)
}
if withAuthor {
user := models.User{}
postInfo.Author = &user
err = conn.QueryRow(`
select about, email, fullname, nickname from "User"
where id=$1;
`, userId).Scan(&user.About, &user.Email, &user.Fullname, &user.Nickname)
if err != nil {
return nil, 404
}
}
return postInfo, 200
}
func ModifyPost(postUpdate *models.PostUpdate, postId int64) (post *models.Post, status int) {
post = &models.Post{}
post.ID = postId
var created time.Time
err := conn.QueryRow(`
select authorNickname, post.created, forumSlug, post.isEdited, post.message, "thread", coalesce(post.parent, 0)
from Post
where post.id = $1
`, postId).Scan(&post.Author, &created, &post.Forum, &post.IsEdited, &post.Message, &post.Thread, &post.Parent)
if err != nil {
return nil, 404
}
post.Created = (*strfmt.DateTime)(&created)
if postUpdate.Message == "" || post.Message == postUpdate.Message {
return post, 200
}
tx, _ := conn.Begin()
_, err = tx.Exec(`
update post set message=$1, isEdited=TRUE where id=$2
`, postUpdate.Message, postId)
post.Message = postUpdate.Message
post.IsEdited = true
if err != nil {
tx.Rollback()
return nil, 404
}
tx.Commit()
return post, 200
}
func GetPosts(threadSlug string, limit int32, since int, desc bool, sortString string) (posts []models.Post, status int) {
var (
threadId int32
err error
)
if threadId64, parseErr := strconv.ParseInt(threadSlug, 0, 32); parseErr == nil {
threadId = int32(threadId64)
err = conn.QueryRow(`
select coalesce(thread.slug, '') from Thread
where thread.id = $1
`, threadId).Scan(&threadSlug)
} else {
err = conn.QueryRow(`
select thread.id from Thread
where thread.slug = $1
`, threadSlug).Scan(&threadId)
}
if err != nil {
return nil, 404
}
var limitString string
if limit != 0 {
limitString = fmt.Sprintf("limit %d", limit)
} else {
limitString = ""
}
posts = make([]models.Post, 0)
switch sortString {
case "tree":
var sorting string
var compareString string
if desc {
sorting = "desc"
if since != 0 {
compareString = fmt.Sprintf(" and parents < (select parents from post where id = %d)", since)
}
} else {
sorting = "asc"
if since != 0 {
compareString = fmt.Sprintf(" and parents > (select parents from post where id = %d)", since)
}
}
query := fmt.Sprintf(`
select id, authorNickname, created, forumSlug, isEdited, message, coalesce(parent, 0)
from Post
where post."thread" = $1 %s
order by parents %s
%s
`, compareString, sorting, limitString)
rows, err := conn.Query(query, threadId)
defer rows.Close()
if err != nil {
return nil, 404
}
for rows.Next() {
post := models.Post{}
post.Thread = threadId
var created time.Time
rows.Scan(&post.ID, &post.Author, &created, &post.Forum, &post.IsEdited, &post.Message, &post.Parent)
post.Created = (*strfmt.DateTime)(&created)
posts = append(posts, post)
}
return posts, 200
case "parent_tree":
var sorting string
var compareString string
if desc {
sorting = "desc"
if since != 0 {
compareString = fmt.Sprintf(" and root_parent < (select root_parent from post where id = %d)", since)
}
} else {
sorting = "asc"
if since != 0 {
compareString = fmt.Sprintf(" and root_parent > (select root_parent from post where id = %d)", since)
}
}
query := fmt.Sprintf(`
select post.id, authorNickname, created, forumSlug, isEdited, message, "thread", coalesce(parent, 0)
from Post
join (
select id from post
where parent=0 and post."thread"=$1 %s
order by id %s
%s
) selectedParents
on root_parent=selectedParents.id
order by parents %s
`, compareString, sorting, limitString, sorting)
rows, err := conn.Query(query, threadId)
defer rows.Close()
if err != nil {
return nil, 404
}
for rows.Next() {
post := models.Post{}
var created time.Time
rows.Scan(&post.ID, &post.Author, &created, &post.Forum, &post.IsEdited, &post.Message, &post.Thread, &post.Parent)
post.Created = (*strfmt.DateTime)(&created)
posts = append(posts, post)
}
return posts, 200
default:
var sorting string
var compareString string
if desc {
sorting = "desc"
if since != 0 {
compareString = fmt.Sprintf(" and post.id < %d", since)
}
} else {
sorting = "asc"
if since != 0 {
compareString = fmt.Sprintf(" and post.id > %d", since)
}
}
query := fmt.Sprintf(`
select id, authorNickname, created, forumSlug, isEdited, message, "thread", coalesce(parent, 0)
from Post
where "thread" = $1 %s
order by id %s
%s
`, compareString, sorting, limitString)
rows, err := conn.Query(query, threadId)
defer rows.Close()
if err != nil {
return nil, 404
}
for rows.Next() {
post := models.Post{}
var created time.Time
rows.Scan(&post.ID, &post.Author, &created, &post.Forum, &post.IsEdited, &post.Message, &post.Thread, &post.Parent)
post.Created = (*strfmt.DateTime)(&created)
posts = append(posts, post)
}
return posts, 200
}
return posts, 200
}