-
Notifications
You must be signed in to change notification settings - Fork 0
/
friend.go
192 lines (181 loc) · 5 KB
/
friend.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
package user
import (
"github.com/gin-gonic/gin"
"fmt"
)
type Adder struct {
F string `json:"friend"`
}
func (u *UserUtils) AddFriend(c *gin.Context) {
e, ee := ExtractTokenMetadata(c.Request)
if ee != nil {
return
}
var g bool
var f Adder
c.BindJSON(&f)
if f.F == e.UserId {
return
}
db, err := u.S.Data.Bdd.Connect()
if err != nil {
return
}
eo := `SELECT IF(COUNT(*),'true','false') FROM friends WHERE user_id = (SELECT id FROM user WHERE uuid = ?) AND friend_id = (SELECT id FROM user WHERE uuid = ?)`
errs := db.QueryRow(eo, e.UserId, f.F).Scan(&g)
if errs != nil {
fmt.Println(err)
}
if !g {
t := `INSERT INTO friends (user_id, friend_id) VALUES((SELECT id FROM user WHERE uuid =?),(SELECT id FROM user WHERE uuid = ?))`
if _, err := db.Exec(t, e.UserId, f.F); err != nil {
fmt.Println(err)
} else {
fmt.Println("oui")
}
}
}
func (a *UserUtils) GetAllFriend(c *gin.Context) {
fmt.Print("eazeaz")
e, err := ExtractTokenMetadata(c.Request)
if err != nil {
return
}
db, errs := a.S.Data.Bdd.Connect()
if errs != nil {
return
}
eo := `SELECT a.id FROM user as q
JOIN friend as a ON a.user_id = q.id OR a.friend_id = q.id
WHERE q.uuid = ?
`
rows, errt := db.Query(eo, e.UserId)
if errt != nil {
return
}
defer rows.Close()
for rows.Next() {
var t int
rows.Scan(&t)
fmt.Println("t=", t)
}
}
func (u *UserUtils) ShowFriend(c *gin.Context) {
var t []int
e, err := ExtractTokenMetadata(c.Request)
if err != nil {
return
}
eo := `SELECT user.id From user WHERE id = (SELECT user_id FROM friends WHERE friend_id = (SELECT id FROM user WHERE uuid = ?))`
db, errs := u.S.Data.Bdd.Connect()
if errs != nil {
return
}
rows, errt := db.Query(eo, e.UserId)
if errt != nil {
fmt.Println(errt)
return
}
defer rows.Close()
for rows.Next() {
var tt int
if err := rows.Scan(&tt); err != nil {
fmt.Println(err)
}
t = append(t, tt)
}
fmt.Println(t)
if err := rows.Err(); err != nil {
fmt.Println(err)
}
}
func (u *UserUtils) AcceptFriend(c *gin.Context) {
e, err := ExtractTokenMetadata(c.Request)
var g bool
if err != nil {
return
}
var f Adder
c.BindJSON(&f)
db, errd := u.S.Data.Bdd.Connect()
if errd != nil {
return
}
eo := `SELECT IF(COUNT(*),'true','false') FROM friends WHERE user_id = (SELECT id FROM user WHERE uuid = ?) AND friend_id = (SELECT id FROM user WHERE uuid = ?) LIMIT 1`
errs := db.QueryRow(eo, f.F, e.UserId).Scan(&g)
if errs != nil {
fmt.Println(errs)
return
}
if !g {
print("eee")
return
}
eo = `INSERT INTO friend (user_id, friend_id) VALUES ((SELECT id FROM user WHERE uuid = ?), (SELECT id FROM user WHERE uuid = ?))`
_, errs = db.Exec(eo, f.F, e.UserId)
if errs != nil {
fmt.Println(errs)
return
}
eo = `DELETE FROM friends WHERE user_id = (SELECT id FROM user WHERE uuid = ?) OR user_id = (SELECT id FROM user WHERE uuid = ?) AND friend_id = (SELECT id FROM user WHERE uuid = ?) OR friend_id = (SELECT id FROM user WHERE uuid = ?)`
_, err = db.Exec(eo, e.UserId, f.F, f.F, e.UserId)
if err != nil {
fmt.Println(err)
}
}
func (u *UserUtils) BanFriend(c *gin.Context) {
e, err := ExtractTokenMetadata(c.Request)
if err != nil {
return
}
var f Adder
c.BindJSON(&f)
db, errd := u.S.Data.Bdd.Connect()
if errd != nil {
return
}
eo := `UPDATE friend SET ban = true WHERE user_id = (SELECT id FROM user WHERE uuid = ?) OR user_id = (SELECT id FROM user WHERE uuid = ?) AND friend_id = (SELECT id FROM user WHERE uuid = ?) OR firend_id = (SELECT id FROM user WHERE uuid = ?)`
db.Exec(eo, e.UserId, f.F, f.F, e.UserId)
}
func (u *UserUtils) DelFriend(c *gin.Context) {
e, err := ExtractTokenMetadata(c.Request)
if err != nil {
return
}
var f Adder
c.BindJSON(&f)
db, errd := u.S.Data.Bdd.Connect()
if errd != nil {
return
}
eo := `DELETE FROM friend WHERE user_id = (SELECT id FROM user WHERE uuid = ?) OR user_id = (SELECT id FROM user WHERE uuid = ?) AND friend_id = (SELECT id FROM user WHERE uuid = ?) OR firend_id = (SELECT id FROM user WHERE uuid = ?)`
db.Exec(eo, e.UserId, f.F, f.F, e.UserId)
}
func (u *UserUtils) Deban(c *gin.Context) {
e, err := ExtractTokenMetadata(c.Request)
if err != nil {
return
}
var f Adder
c.BindJSON(&f)
db, errs := u.S.Data.Bdd.Connect()
if errs != nil {
return
}
eo := `UPDATE friend SET ban = true WHERE user_id = (SELECT id FROM user WHERE uuid = ?) OR user_id = (SELECT id FROM user WHERE uuid = ?) AND friend_id = (SELECT id FROM user WHERE uuid = ?) OR friend_id = (SELECT id FROM user WHERE uuid = ?)`
db.Exec(eo, e.UserId, f.F, f.F, e.UserId)
}
func (u *UserUtils) RefuseFriend(c *gin.Context) {
e, err := ExtractTokenMetadata(c.Request)
if err != nil {
return
}
var f Adder
c.BindJSON(&f)
db, errd := u.S.Data.Bdd.Connect()
if errd != nil {
return
}
eo := `DELETE FROM friends WHERE user_id = (SELECT id FROM user WHERE uuid = ?) OR user_id = (SELECT id FROM user WHERE uuid = ?) AND friend_id = (SELECT id FROM user WHERE uuid = ?) OR firend_id = (SELECT id FROM user WHERE uuid = ?)`
db.Exec(eo, e.UserId, f.F, f.F, e.UserId)
}