💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Gemigit › files › cb4d83301df422e2838cd73070… captured on 2023-09-08 at 16:27:35. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-05-24)

🚧 View Differences

-=-=-=-=-=-=-

Go Back

0 package db

1

2 import (

3 "errors"

4 )

5

6 func groupAlreadyExist(group string) (error) {

7 rows, err := db.Query(

8 "SELECT * FROM groups WHERE UPPER(name) LIKE UPPER(?)",

9 group)

10 if err != nil {

11 return err

12 }

13 defer rows.Close()

14 if rows.Next() {

15 return errors.New(

16 "A group with the same name already exist")

17 }

18 return nil

19 }

20

21 func (user User) CreateGroup(group string, signature string) error {

22 if err := user.VerifySignature(signature); err != nil {

23 return err

24 }

25

26 if err := isGroupNameValid(group); err != nil {

27 return err

28 }

29

30 err := groupAlreadyExist(group)

31 if err != nil {

32 return err

33 }

34

35 rows, err := db.Exec("INSERT INTO groups " +

36 "(owner, name, description, creation) " +

37 "VALUES(?, ?, \"\", " + unixTime + ")",

38 user.ID, group)

39 if err != nil {

40 return err

41 }

42

43 groupID, err := rows.LastInsertId()

44 if err != nil {

45 return err

46 }

47

48 _, err = db.Exec("INSERT INTO member (groupID, userID) " +

49 "VALUES(?, ?)", groupID, user.ID)

50 if err != nil {

51 return err

52 }

53

54 return nil

55 }

56

57 func GetGroupDesc(name string) (string, error) {

58 query := "SELECT description FROM groups WHERE UPPER(?) = UPPER(name);"

59 rows, err := db.Query(query, name)

60 if err != nil {

61 return "", err

62 }

63 defer rows.Close()

64 if !rows.Next() {

65 return "", errors.New("Group not found")

66 }

67 var desc string

68 err = rows.Scan(&desc)

69 if err != nil {

70 return "", err

71 }

72 return desc, nil

73 }

74

75 func GetGroupID(name string) (int, error) {

76 query := "SELECT groupID FROM groups WHERE UPPER(?) = UPPER(name);"

77 rows, err := db.Query(query, name)

78 if err != nil {

79 return -1, err

80 }

81 defer rows.Close()

82 if !rows.Next() {

83 return -1, errors.New("Group not found")

84 }

85 var id int

86 err = rows.Scan(&id)

87 if err != nil {

88 return -1, err

89 }

90 return id, nil

91 }

92

93 func IsInGroup(userID int, groupID int) (error) {

94 query := "SELECT * FROM member WHERE userID=? AND groupID=?"

95 rows, err := db.Query(query, userID, groupID)

96 if err != nil {

97 return err

98 }

99 defer rows.Close()

100 if rows.Next() {

101 return errors.New("The user is already in the group")

102 }

103 return nil

104 }

105

106 func (u User) AddUserToGroup(group string, user string) error {

107 owner, err := GetGroupOwner(group)

108 if err != nil {

109 return err

110 }

111 if u.ID != owner.UserID {

112 return errors.New("only the group owner can add members")

113 }

114 id, err := GetGroupID(group)

115 if err != nil {

116 return err

117 }

118 userID, err := GetUserID(user)

119 if err != nil {

120 return err

121 }

122 if err = IsInGroup(userID, id); err != nil {

123 return err

124 }

125 _, err = db.Exec("INSERT INTO member (groupID, userID) " +

126 "VALUES(?, ?)", id, userID)

127 if err != nil {

128 return err

129 }

130 return nil

131 }

132

133 func DeleteMember(user int, group int) error {

134 statement, err := db.Exec("DELETE FROM member " +

135 "WHERE userID = ? AND groupID = ?",

136 user, group)

137 if err != nil {

138 return err

139 }

140 rows, err := statement.RowsAffected()

141 if err != nil {

142 return err

143 }

144 if rows < 1 {

145 return errors.New("The user is not a member of the group")

146 }

147 return nil

148 }

149

150 func SetGroupDescription(group int, desc string) error {

151 if len(desc) >= descriptionMaxLength {

152 return errors.New("description too long")

153 }

154 res, err := db.Exec("UPDATE groups SET description = ? " +

155 "WHERE groupID = ?", desc, group)

156 if err != nil {

157 return err

158 }

159 rows, err := res.RowsAffected()

160 if rows < 1 {

161 return errors.New("group not found")

162 }

163 return err

164 }

165

166 func DeleteGroup(group int) error {

167 statement, err := db.Exec("DELETE FROM groups " +

168 "WHERE groupID = ?", group)

169 if err != nil {

170 return err

171 }

172 rows, err := statement.RowsAffected()

173 if err != nil {

174 return err

175 }

176 if rows < 1 {

177 return errors.New("There's no such group")

178 }

179 statement, err = db.Exec("DELETE FROM member " +

180 "WHERE groupID = ?", group)

181 if err != nil {

182 return err

183 }

184 return nil

185 }

186

187 func (user User) IsInGroupID(groupID int) (bool, error) {

188 query := "SELECT owner FROM member a " +

189 "INNER JOIN groups b ON a.groupID = b.groupID " +

190 "WHERE a.userID = ? AND a.groupID = ? "

191 rows, err := db.Query(query, user.ID, groupID)

192 if err != nil {

193 return false, err

194 }

195 defer rows.Close()

196 if !rows.Next() {

197 return false, errors.New("Group not found")

198 }

199 var owner int

200 err = rows.Scan(&owner)

201 if err != nil {

202 return false, err

203 }

204 return owner == user.ID, nil

205 }

206

207 func GetGroupOwner(group string) (Member, error) {

208 query := "SELECT c.name, a.userID FROM member a " +

209 "INNER JOIN groups b ON a.groupID = b.groupID " +

210 "INNER JOIN user c ON a.userID = c.userID " +

211 "WHERE a.userID = b.owner AND b.name = ? "

212 rows, err := db.Query(query, group)

213 if err != nil {

214 return Member{}, err

215 }

216 defer rows.Close()

217 var m = Member{}

218 if rows.Next() {

219 err = rows.Scan(&m.Name, &m.UserID)

220 if err != nil {

221 return Member{}, err

222 }

223 } else {

224 return Member{}, errors.New("invalid group")

225 }

226 return m, nil

227 }

228

229 func (user User) GetMembers(group string) ([]Member, error) {

230 query := "SELECT b.Name, b.UserID FROM member a " +

231 "INNER JOIN user b ON a.userID=b.userID " +

232 "INNER JOIN groups c ON a.groupID=c.groupID " +

233 "WHERE c.name = ?"

234 rows, err := db.Query(query, group)

235 if err != nil {

236 return nil, err

237 }

238 defer rows.Close()

239 var members []Member

240 for rows.Next() {

241 var m = Member{}

242 err = rows.Scan(&m.Name, &m.UserID)

243 if err != nil {

244 return nil, err

245 }

246 members = append(members, m)

247 }

248 if len(members) == 0 {

249 return nil, errors.New("invalid group")

250 }

251 return members, nil

252 }

253

254 func (user User) IsInGroup(group string) (bool, error) {

255 id, err := GetGroupID(group)

256 if err != nil {

257 return false, err

258 }

259 return user.IsInGroupID(id)

260 }

261

262 func (user User) GetGroups() ([]Group, error) {

263 query := "SELECT a.groupID, a.name, a.description FROM groups a " +

264 "INNER JOIN member b ON a.groupID = b.groupID " +

265 "WHERE b.userID = ?"

266 rows, err := db.Query(query, user.ID)

267 if err != nil {

268 return nil, err

269 }

270 defer rows.Close()

271 var groups []Group

272 for rows.Next() {

273 var g = Group{}

274 err = rows.Scan(&g.ID, &g.Name, &g.Description)

275 if err != nil {

276 return nil, err

277 }

278 groups = append(groups, g)

279 }

280 return groups, nil

281 }

282