💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Gemigit › files › b926531357cf0ff741e03b34be… captured on 2023-04-19 at 23:33:27. Gemini links have been rewritten to link to archived content

View Raw

More Information

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

0 package db

1

2 import (

3 "errors"

4 "strconv"

5 )

6

7 func (user *User) repoAlreadyExist(repo string) error {

8 rows, err := db.Query(

9 "SELECT * FROM repo WHERE UPPER(name) LIKE UPPER(?)" +

10 " AND UPPER(userID) LIKE UPPER(?)",

11 repo, user.ID)

12 if err != nil {

13 return err

14 }

15 defer rows.Close()

16 if rows.Next() {

17 return errors.New(

18 "Repository with the same name already exist")

19 }

20 return nil

21 }

22

23 func (user User) CreateRepo(repo string, signature string) error {

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

25 return err

26 }

27

28 if err := isRepoNameValid(repo); err != nil {

29 return err

30 }

31

32 err := user.repoAlreadyExist(repo)

33 if err != nil {

34 return err

35 }

36

37 _, err = db.Exec("INSERT INTO repo " +

38 "(userID, name, creation, public, description) " +

39 "VALUES(?, ?, " + unixTime + ", 0, \"\")",

40 user.ID, repo)

41 if err != nil {

42 return err

43 }

44

45 return nil

46 }

47

48 func GetRepoID(repo string, userID int) (int, error) {

49 rows, err := db.Query("SELECT repoID FROM repo " +

50 "WHERE UPPER(?) = UPPER(name) AND userID = ?",

51 repo, userID)

52 if err != nil {

53 return -1, err

54 }

55 defer rows.Close()

56 if !rows.Next() {

57 return -1, errors.New("Repository not found")

58 }

59 var id int

60 err = rows.Scan(&id)

61 if err != nil {

62 return -1, err

63 }

64 return id, nil

65 }

66

67 func (user User) ChangeRepoName(name string, newname string,

68 signature string) error {

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

70 return err

71 }

72 err := isRepoNameValid(newname)

73 if err != nil {

74 return err

75 }

76 statement, err := db.Exec("UPDATE repo SET name=? " +

77 "WHERE UPPER(name) LIKE UPPER(?) " +

78 "AND userID=?",

79 newname, name, user.ID)

80 if err != nil {

81 return err

82 }

83 rows, err := statement.RowsAffected()

84 if err != nil {

85 return err

86 }

87 if rows < 1 {

88 return errors.New("failed to change the repository name")

89 }

90 return nil

91 }

92

93 func (user User) ChangeRepoDesc(name string, newdesc string) error {

94 statement, err := db.Exec("UPDATE repo SET description=? " +

95 "WHERE UPPER(name) LIKE UPPER(?) " +

96 "AND userID=?", newdesc, name, user.ID)

97 if err != nil {

98 return err

99 }

100 rows, err := statement.RowsAffected()

101 if err != nil {

102 return err

103 }

104 if rows > 0 {

105 return nil

106 }

107 return errors.New("failed to change the repository description")

108 }

109

110 func GetRepoDesc(name string, username string) (string, error) {

111 rows, err := db.Query("SELECT a.description FROM repo a " +

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

113 "WHERE UPPER(a.name) LIKE UPPER(?) " +

114 "AND UPPER(b.name) LIKE UPPER(?)",

115 name, username)

116 if err != nil {

117 return "", err

118 }

119 defer rows.Close()

120 if rows.Next() {

121 var description string

122 err = rows.Scan(&description)

123 if err != nil {

124 return "", err

125 }

126 return description, nil

127 }

128 return "", errors.New("No repository called " + name +

129 " by user " + username)

130 }

131

132 func (user User) DeleteRepo(repo string, signature string) error {

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

134 return err

135 }

136 statement, err := db.Exec("DELETE FROM repo WHERE name=? AND userID=?",

137 repo, user.ID)

138 if err != nil {

139 return err

140 }

141 rows, err := statement.RowsAffected()

142 if err != nil {

143 return err

144 }

145 if rows != 1 {

146 return errors.New(strconv.Itoa(int(rows)) +

147 " deleted instead of only one")

148 }

149 return nil

150 }

151

152 func IsRepoPublic(repo string, username string) (bool, error) {

153 rows, err := db.Query("SELECT a.public FROM repo a " +

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

155 "WHERE UPPER(a.name) LIKE UPPER(?) " +

156 "AND UPPER(b.name) LIKE UPPER(?)",

157 repo, username)

158 if err != nil {

159 return false, err

160 }

161 defer rows.Close()

162 if rows.Next() {

163 var public bool

164 err = rows.Scan(&public)

165 if err != nil {

166 return false, err

167 }

168 return public, nil

169 }

170 return false, errors.New("No repository called " + repo +

171 " by user " + username)

172 }

173

174 func (user User) TogglePublic(repo string, signature string) error {

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

176 return err

177 }

178 b, err := IsRepoPublic(repo, user.Name)

179 if err != nil {

180 return err

181 }

182 i := 1

183 if b {

184 i = 0

185 }

186 _, err = db.Exec("UPDATE repo SET public=? " +

187 "WHERE UPPER(name) LIKE UPPER(?) " +

188 "AND userID=?", i, repo, user.ID)

189 return err

190 }

191

192 func (user User) GetRepo(reponame string) (Repo, error) {

193 rows, err := db.Query("SELECT repoID, userID, name, " +

194 "creation, public, description " +

195 "FROM repo WHERE UPPER(name) LIKE UPPER(?) " +

196 "AND userID=?", reponame, user.ID)

197 if err != nil {

198 return Repo{}, err

199 }

200 defer rows.Close()

201 if rows.Next() {

202 var r = Repo{}

203 err = rows.Scan(&r.RepoID, &r.UserID, &r.Name,

204 &r.Date, &r.IsPublic, &r.Description)

205 if err != nil {

206 return Repo{}, err

207 }

208 return r, nil

209 }

210 return Repo{}, errors.New("No repository called " +

211 reponame + " by user " + user.Name)

212 }

213

214 func (user User) GetRepos(onlyPublic bool) ([]Repo, error) {

215 query := "SELECT repoID, userID, name, " +

216 "creation, public, description " +

217 "FROM repo WHERE userID=?"

218 if onlyPublic {

219 query += " AND public=1"

220 }

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

222 if err != nil {

223 return nil, err

224 }

225 defer rows.Close()

226 var repos []Repo

227 for rows.Next() {

228 var r = Repo{}

229 err = rows.Scan(&r.RepoID, &r.UserID, &r.Name,

230 &r.Date, &r.IsPublic, &r.Description)

231 if err != nil {

232 return nil, err

233 }

234 repos = append(repos, r)

235 }

236 return repos, nil

237 }

238

239 func GetPublicRepo() ([]Repo, error) {

240 rows, err := db.Query("SELECT b.name, a.repoID, a.userID, a.name, " +

241 "a.creation, a.public, a.description " +

242 "FROM repo a INNER JOIN user b " +

243 "ON a.userID=b.userID WHERE a.public=1")

244 if err != nil {

245 return nil, err

246 }

247 defer rows.Close()

248 var repos []Repo

249 for rows.Next() {

250 var r = Repo{}

251 err = rows.Scan(&r.Username, &r.RepoID, &r.UserID, &r.Name,

252 &r.Date, &r.IsPublic, &r.Description)

253 if err != nil {

254 return nil, err

255 }

256 repos = append(repos, r)

257 }

258 return repos, nil

259 }

260