Skip to content

Commit

Permalink
Permit sorting by externalId (camel -> snake case for col name)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathonherbert committed Nov 5, 2024
1 parent 8456b2e commit 0afe5f9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion apps/rule-manager/app/db/DbRuleDraft.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import scalikejdbc._

import java.time.OffsetDateTime
import scala.util.{Failure, Success, Try}
import utils.StringHelpers
import service.RuleManager.RuleType

case class DbRuleDraft(
id: Option[Int],
Expand Down Expand Up @@ -280,7 +282,8 @@ object DbRuleDraft extends SQLSyntaxSupport[DbRuleDraft] {
val orderByClause = {
val maybeSimilarityCol = maybeWord.map { _ => SQLSyntax.createUnsafely(similarityCol) }
val orderStmts = maybeSimilarityCol.toList ++ sortBy.map { sortByStr =>
val col = rd.column(sortByStr.slice(1, sortByStr.length))
val colName = StringHelpers.camelToSnakeCase(sortByStr.slice(1, sortByStr.length))
val col = rd.column(colName)
sortByStr.slice(0, 1) match {
case "+" => sqls"$col ASC"
case "-" => sqls"$col DESC"
Expand Down
10 changes: 10 additions & 0 deletions apps/rule-manager/app/utils/StringHelpers.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package utils

object StringHelpers {
def camelToSnakeCase(name: String) = "[A-Z\\d]".r.replaceAllIn(
name,
{ m =>
"_" + m.group(0).toLowerCase()
}
)
}
1 change: 1 addition & 0 deletions apps/rule-manager/test/db/DbRuleDraftSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import scalikejdbc._
import play.api.mvc.Results.NotFound

import java.time.OffsetDateTime
import service.RuleManager.RuleType

class DbRuleDraftSpec extends RuleFixture with Matchers with DBTest {

Expand Down

0 comments on commit 0afe5f9

Please sign in to comment.