-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d666afa
commit 360d301
Showing
9 changed files
with
69 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
src/main/kotlin/com/fardragi/dragiutils/database/Database.kt
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/com/fardragi/dragiutils/database/DatabaseConnection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.fardragi.dragiutils.database | ||
|
||
import com.fardragi.dragiutils.config.DatabaseConfig | ||
import com.fardragi.dragiutils.database.tables.UsersTable | ||
import org.jetbrains.exposed.sql.Database | ||
import org.jetbrains.exposed.sql.SchemaUtils | ||
|
||
class DatabaseConnection(config: DatabaseConfig) { | ||
init { | ||
Database.connect( | ||
url = "jdbc:mariadb://${config.host}:${config.port}/${config.name}", | ||
driver = "org.mariadb.jdbc.Driver", | ||
user = config.user, | ||
password = config.password | ||
) | ||
|
||
query { | ||
SchemaUtils.createMissingTablesAndColumns(UsersTable) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/kotlin/com/fardragi/dragiutils/exceptions/NotFoundException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.fardragi.dragiutils.exceptions | ||
|
||
class NotFoundException(message: String) : Exception(message) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 17 additions & 5 deletions
22
src/main/kotlin/com/fardragi/dragiutils/services/UserService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,29 @@ | ||
package com.fardragi.dragiutils.services | ||
|
||
import com.fardragi.dragiutils.database.query | ||
import com.fardragi.dragiutils.exceptions.NotFoundException | ||
import com.fardragi.dragiutils.models.User | ||
|
||
class UserService() { | ||
fun getOrCreateUser(id: String, name: String): User { | ||
var user = User.findById(id) | ||
return query { | ||
var user = User.findById(id) | ||
if (user == null) { | ||
user = User.new(id) { | ||
|
||
if (user == null) { | ||
user = User.new(id) { | ||
this.name = name | ||
this.name = name | ||
} | ||
} | ||
|
||
user | ||
} | ||
} | ||
|
||
return user | ||
fun setPassword(id: String, password: String) { | ||
query { | ||
User.findByIdAndUpdate(id) { user -> | ||
user.updatePassword(password) | ||
} ?: throw NotFoundException("User not found") | ||
} | ||
} | ||
} |