@@ -10,17 +10,33 @@ private val config: DatabaseConfig = DatabaseConfig {
1010 defaultMaxAttempts = 5
1111}
1212
13+ /* *
14+ * The [DBConfig] instance.
15+ */
1316var dbConfig: DBConfig = DBConfig (" " , " " , " " )
1417
18+ /* *
19+ * The MariaDB configuration.
20+ * @param url The URl to the database.
21+ * @param username The username into the database.
22+ * @param password The password into the database.
23+ */
1524data class DBConfig (
1625 val url : String ,
1726 val username : String ,
1827 val password : String
1928)
2029
30+ /* *
31+ * Represents the database connection.
32+ */
2133lateinit var database: Database
2234 private set
2335
36+ /* *
37+ * Attempts to connect to the database.
38+ * @return The new [database] value
39+ */
2440fun connect (): Database {
2541 database = Database .connect(
2642 url = dbConfig.url,
@@ -32,6 +48,12 @@ fun connect(): Database {
3248 return database
3349}
3450
51+ /* *
52+ * Adds a user to the database.
53+ * @param username The username of the jenkins user
54+ * @param discord The Discord ID of the jenkins user
55+ * @return The result of the `INSERT` statement.
56+ */
3557fun addUser (
3658 username : String ,
3759 discord : Long
@@ -44,6 +66,11 @@ fun addUser(
4466 }
4567}
4668
69+ /* *
70+ * Gets a [User] by its jenkins username.
71+ * @param username The username to lookup
72+ * @return The user mapped to the jenkins username, or `null` if not found
73+ */
4774fun getUser (
4875 username : String
4976): User ? = transaction(database) {
@@ -52,11 +79,21 @@ fun getUser(
5279 .firstOrNull()
5380}
5481
82+ /* *
83+ * Gets all users currently linked in the database.
84+ * @return All users in the database
85+ */
5586fun getAllUsers (): List <User > = transaction(database) {
5687 Users .selectAll()
5788 .map { row -> User (row[Users .username], row[Users .discord]) }
5889}
5990
91+ /* *
92+ * Updates the user linked in the database.
93+ * @param username The jenkins user to update
94+ * @param discord The new Discord ID mapped to the user
95+ * @return `1` if updated, else `0`
96+ */
6097fun updateUser (
6198 username : String ,
6299 discord : Long
@@ -66,12 +103,21 @@ fun updateUser(
66103 }
67104}
68105
106+ /* *
107+ * Removes a user from the database.
108+ * @param username The jenkins user to remove
109+ * @return `1` if removed, else `0`
110+ */
69111fun removeUser (
70112 username : String
71113) = transaction(database) {
72114 Users .deleteWhere { Users .username eq username }
73115}
74116
117+ /* *
118+ * Removes all users from the database.
119+ * @return The count of deleted members
120+ */
75121fun removeAllUsers () = transaction(database) {
76122 Users .deleteAll()
77123}
0 commit comments