@@ -54,6 +54,8 @@ fun connect(): Database {
5454 return database
5555}
5656
57+ // Users
58+
5759/* *
5860 * Adds a user to the database.
5961 * @param username The username of the jenkins user
@@ -134,4 +136,90 @@ fun removeUser(
134136fun removeAllUsers () = transaction(database) {
135137 if (! Users .exists()) return @transaction 0
136138 Users .deleteAll()
139+ }
140+
141+ // Requests
142+
143+ /* *
144+ * Adds a request to the database.
145+ * @param messageId The unique ID of the request according to Discord
146+ * @param userId The unique ID of the Discord user
147+ * @param githubName The GitHub username of the user
148+ * @param repoName The name of the repository
149+ * @return The result of the `INSERT` statement
150+ */
151+ fun createRequest (
152+ messageId : Long ,
153+ userId : Long ,
154+ githubName : String ,
155+ repoName : String
156+ ) = transaction(database) {
157+ SchemaUtils .create(Requests )
158+
159+ return @transaction Requests .insert { row ->
160+ row[Requests .messageId] = messageId
161+ row[Requests .userId] = userId
162+ row[Requests .githubName] = githubName
163+ row[Requests .repoName] = repoName
164+ }
165+ }
166+
167+ /* *
168+ * Gets a [Request] by its unique ID.
169+ * @param messageId The unique ID to lookup
170+ * @return The request mapped to the unique ID, or `null` if not found
171+ */
172+ fun getRequest (
173+ messageId : Long
174+ ): Request ? = transaction(database) {
175+ if (! Requests .exists()) return @transaction null
176+
177+ return @transaction Requests .selectAll().where { Requests .messageId eq messageId }
178+ .map { row -> Request (row[Requests .messageId], row[Requests .userId], row[Requests .githubName], row[Requests .repoName]) }
179+ .firstOrNull()
180+ }
181+
182+ /* *
183+ * Checks if a request exists by its unique ID.
184+ * @param messageId The unique ID to lookup
185+ * @return `true` if the request exists, `false` otherwise
186+ */
187+ fun requestExists (
188+ messageId : Long
189+ ): Boolean = transaction(database) {
190+ if (! Requests .exists()) return @transaction false
191+
192+ return @transaction Requests .selectAll().where { Requests .messageId eq messageId }.count() > 0
193+ }
194+
195+ /* *
196+ * Gets all requests currently linked in the database.
197+ * @return All requests in the database
198+ */
199+ fun getAllRequests (): List <Request > = transaction(database) {
200+ if (! Requests .exists()) return @transaction emptyList()
201+
202+ return @transaction Requests .selectAll()
203+ .map { row -> Request (row[Requests .messageId], row[Requests .userId], row[Requests .githubName], row[Requests .repoName]) }
204+ }
205+
206+ /* *
207+ * Removes a request from the database.
208+ * @param messageId The unique ID of the request to remove
209+ * @return `1` if removed, else `0`
210+ */
211+ fun removeRequest (
212+ messageId : Long
213+ ) = transaction(database) {
214+ if (! Requests .exists()) return @transaction 0
215+ return @transaction Requests .deleteWhere { Requests .messageId eq messageId }
216+ }
217+
218+ /* *
219+ * Removes all requests from the database.
220+ * @return The count of deleted requests
221+ */
222+ fun removeAllRequests () = transaction(database) {
223+ if (! Requests .exists()) return @transaction 0
224+ return @transaction Requests .deleteAll()
137225}
0 commit comments