-
Notifications
You must be signed in to change notification settings - Fork 2
/
retrieve_diary.go
94 lines (81 loc) · 1.59 KB
/
retrieve_diary.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
package diary
import (
"errors"
"github.com/kaikourok/lunchtote-backend/entity/model"
)
func (db *DiaryRepository) RetrieveDiary(characterId *int, targetId, nth int) (*model.Diary, error) {
if characterId != nil {
row := db.QueryRowx(`
SELECT
EXISTS (
SELECT
*
FROM
blocks
WHERE
(blocker = $1 AND blocked = $2) OR
(blocker = $2 AND blocked = $1)
);
`, characterId, targetId)
var isInvalid bool
err := row.Scan(&isInvalid)
if err != nil {
return nil, err
}
if isInvalid {
return nil, errors.New("ブロックしている、もしくはブロックされているため取得できません")
}
}
row := db.QueryRowx(`
SELECT
characters.id,
characters.nickname,
characters.mainicon,
diaries.title,
diaries.diary,
diaries.nth
FROM
diaries
JOIN
characters ON (diaries.character = characters.id)
WHERE
diaries.character = $1 AND diaries.nth = $2;
`, targetId, nth)
var diary model.Diary
err := row.Scan(
&diary.Author.Id,
&diary.Author.Name,
&diary.Author.Mainicon,
&diary.Title,
&diary.Diary,
&diary.Nth,
)
if err != nil {
return nil, err
}
rows, err := db.Queryx(`
SELECT
diaries.nth
FROM
diaries
WHERE
diaries.character = $1
ORDER BY
diaries.nth;
`, targetId)
if err != nil {
return nil, err
}
defer rows.Close()
diaries := make([]int, 0, 64)
for rows.Next() {
var diaryIndex int
err = rows.Scan(&diaryIndex)
if err != nil {
return nil, err
}
diaries = append(diaries, diaryIndex)
}
diary.ExistingDiaries = diaries
return &diary, nil
}