💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Gemigit › files › 207edcd985b9b7b48d361531c3… captured on 2023-04-19 at 23:33:22. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
0 package db
1
2 import (
3
4 "errors"
5 )
6
7 func groupAlreadyExist(group string) (error) {
8 rows, err := db.Query(
9 "SELECT * FROM groups WHERE UPPER(name) LIKE UPPER(?)",
10 group)
11 if err != nil {
12 return err
13 }
14 defer rows.Close()
15 if rows.Next() {
16 return errors.New(
17 "A group with the same name already exist")
18 }
19 return nil
20 }
21
22 func (user User) CreateGroup(group string, signature string) error {
23 if err := user.VerifySignature(signature); err != nil {
24 return err
25 }
26
27 if err := isGroupNameValid(group); err != nil {
28 return err
29 }
30
31 err := groupAlreadyExist(group)
32 if err != nil {
33 return err
34 }
35
36 rows, err := db.Exec("INSERT INTO groups " +
37 "(owner, name, description, creation) " +
38 "VALUES(?, ?, \"\", " + unixTime + ")",
39 user.ID, group)
40 if err != nil {
41 return err
42 }
43
44 groupID, err := rows.LastInsertId()
45 if err != nil {
46 return err
47 }
48
49 _, err = db.Exec("INSERT INTO member (groupID, userID) " +
50 "VALUES(?, ?)", groupID, user.ID)
51 if err != nil {
52 return err
53 }
54
55 return nil
56 }
57
58 func GetGroupDesc(name string) (string, error) {
59 query := "SELECT description FROM groups WHERE UPPER(?) = UPPER(name);"
60 rows, err := db.Query(query, name)
61 if err != nil {
62 return "", err
63 }
64 defer rows.Close()
65 if !rows.Next() {
66 return "", errors.New("Group not found")
67 }
68 var desc string
69 err = rows.Scan(&desc)
70 if err != nil {
71 return "", err
72 }
73 return desc, nil
74 }
75
76 func GetGroupID(name string) (int, error) {
77 query := "SELECT groupID FROM groups WHERE UPPER(?) = UPPER(name);"
78 rows, err := db.Query(query, name)
79 if err != nil {
80 return -1, err
81 }
82 defer rows.Close()
83 if !rows.Next() {
84 return -1, errors.New("Group not found")
85 }
86 var id int
87 err = rows.Scan(&id)
88 if err != nil {
89 return -1, err
90 }
91 return id, nil
92 }
93
94 func IsInGroup(userID int, groupID int) (error) {
95 query := "SELECT * FROM member WHERE userID=? AND groupID=?"
96 rows, err := db.Query(query, userID, groupID)
97 if err != nil {
98 return err
99 }
100 defer rows.Close()
101 if rows.Next() {
102 return errors.New("The user is already in the group")
103 }
104 return nil
105 }
106
107 func AddUserToGroup(group string, user string) error {
108 id, err := GetGroupID(group)
109 if err != nil {
110 return err
111 }
112 userID, err := GetUserID(user)
113 if err != nil {
114 return err
115 }
116 if err = IsInGroup(userID, id); err != nil {
117 return err
118 }
119 _, err = db.Exec("INSERT INTO member (groupID, userID) " +
120 "VALUES(?, ?)", id, userID)
121 if err != nil {
122 return err
123 }
124 return nil
125 }
126
127 func DeleteMember(user int, group int) error {
128 statement, err := db.Exec("DELETE FROM member " +
129 "WHERE userID = ? AND groupID = ?",
130 user, group)
131 if err != nil {
132 return err
133 }
134 rows, err := statement.RowsAffected()
135 if err != nil {
136 return err
137 }
138 if rows < 1 {
139 return errors.New("The user is not a member of the group")
140 }
141 return nil
142 }
143
144 func SetGroupDescription(group int, desc string) error {
145 _, err := db.Exec("UPDATE groups SET description = ? " +
146 "WHERE groupID = ?", desc, group)
147 return err
148 }
149
150 func DeleteGroup(group int) error {
151 statement, err := db.Exec("DELETE FROM groups " +
152 "WHERE groupID = ?", group)
153 if err != nil {
154 return err
155 }
156 rows, err := statement.RowsAffected()
157 if err != nil {
158 return err
159 }
160 if rows < 1 {
161 return errors.New("There's no such group")
162 }
163 statement, err = db.Exec("DELETE FROM member " +
164 "WHERE groupID = ?", group)
165 if err != nil {
166 return err
167 }
168 return nil
169 }
170
171 func (user User) IsInGroupID(groupID int) (bool, error) {
172 query := "SELECT owner FROM member a " +
173 "INNER JOIN groups b ON a.groupID = b.groupID " +
174 "WHERE a.userID = ? AND a.groupID = ? "
175 rows, err := db.Query(query, user.ID, groupID)
176 if err != nil {
177 return false, err
178 }
179 defer rows.Close()
180 if !rows.Next() {
181 return false, errors.New("Group not found")
182 }
183 var owner int
184 err = rows.Scan(&owner)
185 if err != nil {
186 return false, err
187 }
188 return owner == user.ID, nil
189 }
190
191 func GetGroupOwner(group string) (Member, error) {
192 query := "SELECT c.name, a.userID FROM member a " +
193 "INNER JOIN groups b ON a.groupID = b.groupID " +
194 "INNER JOIN user c ON a.userID = c.userID " +
195 "WHERE a.userID = b.owner AND b.name = ? "
196 rows, err := db.Query(query, group)
197 if err != nil {
198 return Member{}, err
199 }
200 defer rows.Close()
201 var m = Member{}
202 if rows.Next() {
203 err = rows.Scan(&m.Name, &m.UserID)
204 if err != nil {
205 return Member{}, err
206 }
207 }
208 return m, nil
209 }
210
211 func (user User) GetMembers(group string) ([]Member, error) {
212 query := "SELECT b.Name, b.UserID FROM member a " +
213 "INNER JOIN user b ON a.userID=b.userID " +
214 "INNER JOIN groups c ON a.groupID=c.groupID " +
215 "WHERE c.name = ?"
216 rows, err := db.Query(query, group)
217 if err != nil {
218 return nil, err
219 }
220 defer rows.Close()
221 var members []Member
222 for rows.Next() {
223 var m = Member{}
224 err = rows.Scan(&m.Name, &m.UserID)
225 if err != nil {
226 return nil, err
227 }
228 members = append(members, m)
229 }
230 return members, nil
231 }
232
233 func (user User) IsInGroup(group string) (bool, error) {
234 id, err := GetGroupID(group)
235 if err != nil {
236 return false, err
237 }
238 return user.IsInGroupID(id)
239 }
240
241 func (user User) GetGroups() ([]Group, error) {
242 query := "SELECT a.groupID, a.name, a.description FROM groups a " +
243 "INNER JOIN member b ON a.groupID = b.groupID " +
244 "WHERE b.userID = ?"
245 rows, err := db.Query(query, user.ID)
246 if err != nil {
247 return nil, err
248 }
249 defer rows.Close()
250 var groups []Group
251 for rows.Next() {
252 var g = Group{}
253 err = rows.Scan(&g.GroupID, &g.Name, &g.Description)
254 if err != nil {
255 return nil, err
256 }
257 groups = append(groups, g)
258 }
259 return groups, nil
260 }
261