/
sqllite.go
927 lines (750 loc) · 20.6 KB
/
sqllite.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
package dbrepo
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/Pomog/ForumFFF/internal/forms"
"github.com/Pomog/ForumFFF/internal/models"
)
func (m *SqliteBDRepo) UserPresent(userName, email string) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select count(id)
from users
where username = $1 OR
email = $2
`
var numRows int
row := m.DB.QueryRowContext(ctx, query, userName, email)
err := row.Scan(&numRows)
if err != nil {
return false, nil
}
if numRows == 0 {
return false, nil
}
return true, nil
}
func (m *SqliteBDRepo) UserPresentLogin(email, password string) (int, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select id
from users
where email = $1 and
password = $2
`
row := m.DB.QueryRowContext(ctx, query, email, password)
userID := 0
err := row.Scan(&userID)
if err != nil {
return userID, err
}
return userID, nil
}
func (m *SqliteBDRepo) GetUserByID(ID int) (models.User, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select *
from users
where id = $1
`
var user models.User
row := m.DB.QueryRowContext(ctx, query, ID)
err := row.Scan(&user.ID, &user.UserName, &user.Password, &user.FirstName, &user.LastName, &user.Email, &user.Created, &user.Picture, &user.LastActivity, &user.Type)
if err != nil {
return user, err
}
if user.ID == 0 || user.UserName == "" || user.Email == "" {
return user, errors.New("wrong User Data")
}
if user.ID == 0 || user.UserName == "" || user.Email == "" {
return user, errors.New("wrong User Data")
}
return user, nil
}
func (m *SqliteBDRepo) GetThreadByID(ID int) (models.Thread, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select *
from thread
where id = $1
`
var thread models.Thread
row := m.DB.QueryRowContext(ctx, query, ID)
err := row.Scan(&thread.ID, &thread.Subject, &thread.Created, &thread.UserID, &thread.Image, &thread.Category, &thread.Classification)
if err != nil {
return thread, err
}
return thread, nil
}
func (m *SqliteBDRepo) GetPostByID(ID int) (models.Post, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select *
from post
where id = $1
`
var post models.Post
row := m.DB.QueryRowContext(ctx, query, ID)
err := row.Scan(&post.ID, &post.Subject, &post.Content, &post.Created, &post.ThreadId, &post.UserID, &post.Image, &post.Classification)
if err != nil {
return post, err
}
return post, nil
}
func (m *SqliteBDRepo) CreateUser(r models.User) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
stmt := `insert into users
(username, password, first_name, last_name, email, picture)
values ($1, $2, $3, $4, $5, $6)
`
_, err := m.DB.ExecContext(ctx, stmt,
r.UserName,
r.Password,
r.FirstName,
r.LastName,
r.Email,
r.Picture,
)
if err != nil {
return err
}
return nil
}
func (m *SqliteBDRepo) CreateThread(thread models.Thread) (int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
user, err := m.GetUserByID(thread.UserID)
if err != nil {
return 0, errors.New("guest can not create a thread")
}
userName := user.UserName
if userName == "guest" || strings.TrimSpace(userName) == "" {
return 0, errors.New("guest can not create a thread")
}
stmt := `insert into thread
(subject, userID, threadImage, category)
values ($1, $2, $3, $4)
`
sqlRes, err := m.DB.ExecContext(ctx, stmt,
thread.Subject,
thread.UserID,
thread.Image,
thread.Category,
)
if err != nil {
return 0, err
}
id, err := sqlRes.LastInsertId()
if err != nil {
return 0, err
}
return id, nil
}
// CreatePost insert post into SQLite DB
func (m *SqliteBDRepo) CreatePost(post models.Post) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
stmt := `insert into post
(subject, content, threadID, userID, postImage)
values ($1, $2, $3, $4, $5
)
`
_, err := m.DB.ExecContext(ctx, stmt,
post.Subject,
post.Content,
post.ThreadId,
post.UserID,
post.Image,
)
if err != nil {
return err
}
return nil
}
func (m *SqliteBDRepo) EditPost(post models.Post) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if strings.TrimSpace(post.Content) == "" {
return errors.New("empty post can not be created")
}
if len(post.Content) > m.App.PostLen {
return fmt.Errorf("the post is to long, %d syblos allowed", m.App.PostLen)
}
if !forms.CheckSingleWordLen(post.Content, 45) {
return fmt.Errorf("the post or category without spaces is not allowed, max len of each word (without spaces) is %d", len(m.App.LongestSingleWord))
}
stmt := `UPDATE post
SET subject = $1, content = $2, threadID = $3, userID = $4
WHERE id = $5;
`
_, err := m.DB.ExecContext(ctx, stmt,
post.Subject,
post.Content,
post.ThreadId,
post.UserID,
post.ID,
)
if err != nil {
return err
}
return nil
}
func (m *SqliteBDRepo) DeletePost(post models.Post) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
stmt := `DELETE FROM post
WHERE id = $1;
`
_, err := m.DB.ExecContext(ctx, stmt,
post.ID,
)
if err != nil {
return err
}
return nil
}
// EditTopic updates topic
func (m *SqliteBDRepo) EditTopic(topic models.Thread) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if strings.TrimSpace(topic.Subject) == "" {
return errors.New("empty topic can not be created")
}
if len(topic.Subject) > m.App.PostLen {
return fmt.Errorf("the topic is to long, %d syblos allowed", m.App.PostLen)
}
if !forms.CheckSingleWordLen(topic.Subject, 45) {
return fmt.Errorf("the topic without spaces is not allowed, max len of each word (without spaces) is %d", len(m.App.LongestSingleWord))
}
stmt := `UPDATE thread
SET subject = $1, created = $2, userID = $3, threadImage = $4
WHERE id = $5;
`
_, err := m.DB.ExecContext(ctx, stmt,
topic.Subject,
topic.Created,
topic.UserID,
topic.Image,
topic.ID,
)
if err != nil {
return err
}
return nil
}
// isThreadExist returns true if Thread with same Subject exist
func (m *SqliteBDRepo) IsThreadExist(thread models.Thread) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select count(id)
from thread
where subject = $1
`
var numRows int
row := m.DB.QueryRowContext(ctx, query, thread.Subject)
err := row.Scan(&numRows)
if err != nil {
return false, nil
}
if numRows == 0 {
return false, nil
}
return true, nil
}
// GetAllPostsFromThread returns all slice of all Posts with given ThreadID, nil if there are no Posts
func (m *SqliteBDRepo) GetAllPostsFromThread(threadID int) ([]models.Post, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select *
from post
where threadID = $1
`
rows, err := m.DB.QueryContext(ctx, query, threadID)
if err != nil {
return nil, err
}
defer rows.Close()
var posts []models.Post
for rows.Next() {
var post models.Post
err := rows.Scan(&post.ID, &post.Subject, &post.Content, &post.Created, &post.ThreadId, &post.UserID, &post.Image, &post.Classification)
if err != nil {
return nil, err
}
posts = append(posts, post)
}
return posts, nil
}
// GetAllPostsByUserID returns all slice of all Posts with given UserID, nil if there are no Posts
func (m *SqliteBDRepo) GetAllPostsByUserID(userID int) ([]models.Post, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select *
from post
where userID = $1
`
rows, err := m.DB.QueryContext(ctx, query, userID)
if err != nil {
return nil, err
}
defer rows.Close()
var posts []models.Post
for rows.Next() {
var post models.Post
err := rows.Scan(&post.ID, &post.Subject, &post.Content, &post.Created, &post.ThreadId, &post.UserID, &post.Image, &post.Classification)
if err != nil {
return nil, err
}
posts = append(posts, post)
}
return posts, nil
}
func (m *SqliteBDRepo) GetAllLikedPostsByUserID(userID int) ([]models.Post, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `SELECT post.id, post.subject, post.content, post.created, post.threadID, post.userID, post.postImage
FROM votes
JOIN post ON votes.postID = post.id
WHERE votes.like = 1 AND votes.userID = $1;
`
rows, err := m.DB.QueryContext(ctx, query, userID)
if err != nil {
return nil, err
}
defer rows.Close()
var posts []models.Post
for rows.Next() {
var post models.Post
err := rows.Scan(&post.ID, &post.Subject, &post.Content, &post.Created, &post.ThreadId, &post.UserID, &post.Image)
if err != nil {
return nil, err
}
posts = append(posts, post)
}
return posts, nil
}
// GetAllThreadsByUserID returns all Threads of one user, nil if there are no threads in DB
func (m *SqliteBDRepo) GetAllThreadsByUserID(userID int) ([]models.Thread, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select *
from thread
where userID=$1
`
rows, err := m.DB.QueryContext(ctx, query, userID)
if err != nil {
return nil, err
}
defer rows.Close()
var threads []models.Thread
for rows.Next() {
var thread models.Thread
err := rows.Scan(&thread.ID, &thread.Subject, &thread.Created, &thread.UserID, &thread.Image, &thread.Category, &thread.Classification)
if err != nil {
return nil, err
}
threads = append(threads, thread)
}
return threads, nil
}
// GetAllThreads returns all Threads, nil if there are no threads in DB
func (m *SqliteBDRepo) GetAllThreads() ([]models.Thread, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select *
from thread
`
rows, err := m.DB.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
var threads []models.Thread
for rows.Next() {
var thread models.Thread
err := rows.Scan(&thread.ID, &thread.Subject, &thread.Created, &thread.UserID, &thread.Image, &thread.Category, &thread.Classification)
if err != nil {
return nil, err
}
threads = append(threads, thread)
}
return threads, nil
}
func (m *SqliteBDRepo) GetSessionIDForUserID(userID int) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select sessionID
from sessionId WHERE
userID = $1
`
sessionID := ""
row := m.DB.QueryRowContext(ctx, query, userID)
err := row.Scan(&sessionID)
if err != nil {
return sessionID, err
}
return sessionID, nil
}
func (m *SqliteBDRepo) GetUserIDForSessionID(sessionID string) (int, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select userID
from sessionId WHERE
sessionID = $1
`
var userID int
row := m.DB.QueryRowContext(ctx, query, sessionID)
err := row.Scan(&userID)
if err != nil {
return 0, err
}
return userID, nil
}
func (m *SqliteBDRepo) InsertSessionintoDB(sessionID string, userID int) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
stmt := `insert into sessionId
(sessionID, userID)
values ($1, $2
)
`
_, err := m.DB.ExecContext(ctx, stmt,
sessionID,
userID,
)
if err != nil {
return err
}
return nil
}
func (m *SqliteBDRepo) GetTotalPostsAmmountByUserID(userID int) (int, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select count(*) from post where userID = $1; `
var numberOfPosts int
row := m.DB.QueryRowContext(ctx, query, userID)
err := row.Scan(&numberOfPosts)
if err != nil {
return 0, err
}
return numberOfPosts, nil
}
func (m *SqliteBDRepo) LikePostByUserIdAndPostId(userID, postID int) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
//reverse posts like flag
stmt := `INSERT OR REPLACE INTO votes (id, like, dislike, postID, userID)
VALUES (
COALESCE((SELECT id FROM votes WHERE userID = $1 AND postID = $2), NULL),
1, -- Setting like to true
CASE WHEN NOT COALESCE((SELECT like FROM votes WHERE userID = $1 AND postID = $2), 0)
THEN 0 ELSE COALESCE((SELECT dislike FROM votes WHERE userID = $1 AND postID = $2), 0) END,
$2, $1
);
`
_, err := m.DB.ExecContext(ctx, stmt,
userID,
postID,
)
if err != nil {
return err
}
return nil
}
func (m *SqliteBDRepo) DislikePostByUserIdAndPostId(userID, postID int) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
//reverse posts dislike flag
stmt := `INSERT OR REPLACE INTO votes (id, dislike, like, postID, userID)
VALUES (
COALESCE((SELECT id FROM votes WHERE userID = $1 AND postID = $2), NULL),
1, -- Setting dislike to true
CASE WHEN NOT COALESCE((SELECT dislike FROM votes WHERE userID = $1 AND postID = $2), 0)
THEN 0
ELSE COALESCE((SELECT like FROM votes WHERE userID = $1 AND postID = $2), 0)
END,
$2, $1
);
`
_, err := m.DB.ExecContext(ctx, stmt,
userID,
postID,
)
if err != nil {
return err
}
return nil
}
func (m *SqliteBDRepo) CountLikesAndDislikesForPostByPostID(postID int) (likes, dislikes int, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `
SELECT
SUM(CASE WHEN postID = $1 AND like = TRUE THEN 1 ELSE 0 END) AS like_count,
SUM(CASE WHEN postID = $1 AND dislike = TRUE THEN 1 ELSE 0 END) AS dislike_count
FROM votes;
`
row := m.DB.QueryRowContext(ctx, query, postID)
likes = 0
dislikes = 0
row.Scan(&likes, &dislikes)
err = nil
return
}
func (m *SqliteBDRepo) GetGuestID() (int, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select id
from users
where email = 'guest@gmail.com'
`
var guestID int
row := m.DB.QueryRowContext(ctx, query)
err := row.Scan(&guestID)
if err != nil {
return guestID, err
}
return guestID, nil
}
func (m *SqliteBDRepo) GetSearchedThreads(search string) ([]models.Thread, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select *
from thread
WHERE subject LIKE '%' || $1 || '%';
`
rows, err := m.DB.QueryContext(ctx, query, search)
if err != nil {
return nil, err
}
defer rows.Close()
var threads []models.Thread
for rows.Next() {
var thread models.Thread
err := rows.Scan(&thread.ID, &thread.Subject, &thread.Created, &thread.UserID, &thread.Image, &thread.Category, &thread.Classification)
if err != nil {
return nil, err
}
threads = append(threads, thread)
}
return threads, nil
}
func (m *SqliteBDRepo) GetSearchedThreadsByCategory(search string) ([]models.Thread, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select *
from thread
WHERE category LIKE '%' || $1 || '%';
`
rows, err := m.DB.QueryContext(ctx, query, search)
if err != nil {
return nil, err
}
defer rows.Close()
var threads []models.Thread
for rows.Next() {
var thread models.Thread
err := rows.Scan(&thread.ID, &thread.Subject, &thread.Created, &thread.UserID, &thread.Image, &thread.Category, &thread.Classification)
if err != nil {
return nil, err
}
threads = append(threads, thread)
}
return threads, nil
}
// EditUserType changes type of user from "user" to "moder"
func (m *SqliteBDRepo) EditUserType(user models.User) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
stmt := `UPDATE users
SET type = $1
WHERE id = $2;
`
_, err := m.DB.ExecContext(ctx, stmt,
user.Type,
user.ID,
)
if err != nil {
return err
}
return nil
}
func (m *SqliteBDRepo) DelSessionByUserID(userID int) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
stmt := `delete from sessionId
where userID = $1
`
_, err := m.DB.ExecContext(ctx, stmt,
userID,
)
if err != nil {
return err
}
return nil
}
// GetAllThreadsByClassification returns all Threads of given classification type, nil if there are no threads in DB
func (m *SqliteBDRepo) GetAllThreadsByClassification(classification models.TextClassification) ([]models.Thread, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select *
from thread
where classification=$1
`
rows, err := m.DB.QueryContext(ctx, query, classification)
if err != nil {
return nil, err
}
defer rows.Close()
var threads []models.Thread
for rows.Next() {
var thread models.Thread
err := rows.Scan(&thread.ID, &thread.Subject, &thread.Created, &thread.UserID, &thread.Image, &thread.Category, &thread.Classification)
if err != nil {
return nil, err
}
threads = append(threads, thread)
}
return threads, nil
}
// GetAllPostsByClassification returns all slice of all Posts with given classification type, nil if there are no Posts
func (m *SqliteBDRepo) GetAllPostsByClassification(classification models.TextClassification) ([]models.Post, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select *
from post
where classification = $1
`
rows, err := m.DB.QueryContext(ctx, query, classification)
if err != nil {
return nil, err
}
defer rows.Close()
var posts []models.Post
for rows.Next() {
var post models.Post
err := rows.Scan(&post.ID, &post.Subject, &post.Content, &post.Created, &post.ThreadId, &post.UserID, &post.Image, &post.Classification)
if err != nil {
return nil, err
}
posts = append(posts, post)
}
return posts, nil
}
// EditTopicClassification updates classification of the topic by moderator
func (m *SqliteBDRepo) EditTopicClassification(topic models.Thread, classification models.TextClassification) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
stmt := `UPDATE thread
SET classification = $1
WHERE id = $2;
`
_, err := m.DB.ExecContext(ctx, stmt,
classification,
topic.ID,
)
if err != nil {
return err
}
return nil
}
// EditTopicClassification updates classification of the topic by moderator
func (m *SqliteBDRepo) EditPostClassification(post models.Post, classification models.TextClassification) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
stmt := `UPDATE post
SET classification = $1
WHERE id = $2;
`
_, err := m.DB.ExecContext(ctx, stmt,
classification,
post.ID,
)
if err != nil {
return err
}
return nil
}
// CreatePM insert post into SQLite DB
func (m *SqliteBDRepo) CreatePM(pm models.PM) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
stmt := `insert into pm
(content, senderUserID, receiverUserID)
values ($1, $2, $3)
`
_, err := m.DB.ExecContext(ctx, stmt,
pm.Content,
pm.SenderUserID,
pm.ReceiverUserID,
)
if err != nil {
return err
}
return nil
}
// DeletePM deletes pm
func (m *SqliteBDRepo) DeletePM(pm models.PM) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
stmt := `DELETE FROM pm
WHERE id = $1;
`
_, err := m.DB.ExecContext(ctx, stmt,
pm.ID,
)
if err != nil {
return err
}
return nil
}
// GetPMbyReceiverUserID insert post into SQLite DB
func (m *SqliteBDRepo) GetPMbyReceiverUserID(userID int) ([]models.PM, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select * from pm
where receiverUserID = $1
`
rows, err := m.DB.QueryContext(ctx, query, userID)
if err != nil {
return nil, err
}
defer rows.Close()
var pms []models.PM
for rows.Next() {
var pm models.PM
err := rows.Scan(&pm.ID, &pm.Content, &pm.Created, &pm.SenderUserID, &pm.ReceiverUserID)
if err != nil {
return nil, err
}
pms = append(pms, pm)
}
return pms, nil
}
// GetPMbysenderUserID insert post into SQLite DB
func (m *SqliteBDRepo) GetPMbysenderUserID(userID int) ([]models.PM, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select * from pm
where senderUserID = $1
`
rows, err := m.DB.QueryContext(ctx, query, userID)
if err != nil {
return nil, err
}
defer rows.Close()
var pms []models.PM
for rows.Next() {
var pm models.PM
err := rows.Scan(&pm.ID, &pm.Content, &pm.Created, &pm.SenderUserID, &pm.ReceiverUserID)
if err != nil {
return nil, err
}
pms = append(pms, pm)
}
return pms, nil
}