Skip to content

Commit

Permalink
- Fixed db error because of discord id
Browse files Browse the repository at this point in the history
- Added logs of RatingDBApi
- Increased version
  • Loading branch information
makeevrserg committed May 18, 2023
1 parent 6ac3406 commit 10e8cb9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,32 @@ import ru.astrainteractive.astralibs.orm.mapNotNull
import ru.astrainteractive.astralibs.orm.query.CountQuery
import ru.astrainteractive.astralibs.orm.query.SelectQuery
import ru.astrainteractive.astralibs.orm.with
import java.io.File
import java.text.SimpleDateFormat
import java.time.Instant
import java.util.Date

class RatingDBApiImpl(private val database: Database, private val pluginFolder: File) : RatingDBApi {
private fun <T> Result<T>.logStackTrace(): Result<T> {
return this.onFailure {
it.printStackTrace()
val logsFolder = File(pluginFolder, "logs")
if (!logsFolder.exists()) logsFolder.mkdirs()
val fileName = SimpleDateFormat("dd.MM.yyyy").format(Date.from(Instant.now()))
val logFile = File(logsFolder, "$fileName.log")
if (!logFile.exists()) logFile.createNewFile()
logFile.appendText(it.stackTraceToString() + "\n")
}
}

class RatingDBApiImpl(private val database: Database) : RatingDBApi {
private val String.sqlString: String
get() = "\"$this\""

override suspend fun selectUser(playerName: String): Result<UserDTO> = kotlin.runCatching {
UserTable.find(database, constructor = UserEntity) {
UserTable.minecraftName.eq(playerName.uppercase())
}.map(UserMapper::toDTO).first()
}
}.logStackTrace()

override suspend fun updateUser(user: UserDTO) = kotlin.runCatching {
val userEntity = UserTable.find(database, constructor = UserEntity) {
Expand All @@ -36,16 +52,16 @@ class RatingDBApiImpl(private val database: Database) : RatingDBApi {
userEntity?.lastUpdated = System.currentTimeMillis()
user.discordID?.let { userEntity?.discordID = it }
userEntity?.let { UserTable.update(database, entity = it) }
}
}.logStackTrace()

override suspend fun insertUser(user: UserModel) = kotlin.runCatching {
UserTable.insert(database) {
this[UserTable.lastUpdated] = System.currentTimeMillis()
this[UserTable.minecraftUUID] = user.minecraftUUID.toString()
this[UserTable.minecraftName] = user.minecraftName.uppercase()
this[UserTable.discordID] = user.discordID
this[UserTable.discordID] = null
}
}
}.logStackTrace()

override suspend fun insertUserRating(
reporter: UserDTO?,
Expand All @@ -62,13 +78,13 @@ class RatingDBApiImpl(private val database: Database) : RatingDBApi {
this[UserRatingTable.time] = System.currentTimeMillis()
this[UserRatingTable.ratingTypeIndex] = type.ordinal
}
}
}.logStackTrace()

override suspend fun deleteUserRating(it: UserRatingDTO) = kotlin.runCatching {
UserRatingTable.delete<UserRatingEntity>(database) {
UserRatingTable.id.eq(it.id)
}
}
}.logStackTrace()

override suspend fun fetchUserRatings(playerName: String) = kotlin.runCatching {
val query = """
Expand All @@ -91,7 +107,7 @@ class RatingDBApiImpl(private val database: Database) : RatingDBApi {
}
statement?.close()
result ?: emptyList()
}
}.logStackTrace()

override suspend fun fetchUsersTotalRating() = kotlin.runCatching {
val query = """
Expand All @@ -110,7 +126,7 @@ class RatingDBApiImpl(private val database: Database) : RatingDBApi {
}
statement?.close()
result ?: emptyList()
}
}.logStackTrace()

override suspend fun countPlayerTotalDayRated(playerName: String) = kotlin.runCatching {
val query = CountQuery(UserRatingTable) {
Expand All @@ -127,7 +143,7 @@ class RatingDBApiImpl(private val database: Database) : RatingDBApi {
it.getInt("total")
} ?: 0
} ?: 0
}
}.logStackTrace()

override suspend fun countPlayerOnPlayerDayRated(playerName: String, ratedPlayerName: String) = kotlin.runCatching {
val query = CountQuery(UserRatingTable) {
Expand All @@ -146,5 +162,5 @@ class RatingDBApiImpl(private val database: Database) : RatingDBApi {
it.getInt("total")
} ?: 0
} ?: 0
}
}.logStackTrace()
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object UserMapper : Mapper<UserEntity, UserDTO> {
id = it.id,
minecraftUUID = it.minecraftUUID,
minecraftName = it.minecraftName,
discordID = it.discordID,
discordID = null,
lastUpdated = it.lastUpdated
)
}
2 changes: 1 addition & 1 deletion domain/src/test/java/AuctionsTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AuctionsTests : ORMTest() {
database.openConnection()
UserTable.create(database)
UserRatingTable.create(database)
api = RatingDBApiImpl(database)
api = RatingDBApiImpl(database,File("."))
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]
# Plugin core
project-version = "1.7.3"
project-version = "1.7.4"
project-name = "AstraRating"
project-group = "com.astrainteractive.astrarating"
project-url = "EmpireProjekt.ru"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ object RootModuleImpl : RootModule {
}

override val dbApi = Single {
RatingDBApiImpl(database.value) as RatingDBApi
RatingDBApiImpl(database.value, plugin.value.dataFolder) as RatingDBApi
}
override val cachedApi = Single {
val scope by scope
Expand Down

0 comments on commit 10e8cb9

Please sign in to comment.