-
Notifications
You must be signed in to change notification settings - Fork 1
Fixed some problems #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Antonio171003
wants to merge
13
commits into
wiringbits:main
Choose a base branch
from
Antonio171003:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d049867
Update React-Admin version to '4.14.4'
Antonio171003 112e1a5
Added tables from DB
Antonio171003 f6e188c
Fixed the filter functionality when the parameter is a reference.
Antonio171003 abc5068
Fixed an Internal Server Error, Caused when attempting to filter by a…
Antonio171003 70ee180
Fixed : Failed prop type: Invalid prop `children` of type `array` sup…
Antonio171003 39daeb2
Format
Antonio171003 d874d43
Revert "Added tables from DB"
Antonio171003 d401e5f
Fixed Internal server error
Antonio171003 5cbf089
Corrections to the filter
Antonio171003 16ba68d
Fixed Internal server error
Antonio171003 5806701
Fixed bugs related to image handling (saving and reading).
Antonio171003 ef10525
Changes required for the PR
Antonio171003 9aa43c4
Changes required for the PR
Antonio171003 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -73,6 +73,23 @@ object DatabaseTablesDAO { | |
| """.as(foreignKeyParser.*) | ||
| } | ||
|
|
||
| private def isDouble(columnType: String): Boolean = { | ||
| columnType.contains("int") || columnType.contains("float") || columnType.contains("decimal") | ||
|
Antonio171003 marked this conversation as resolved.
Outdated
|
||
| } | ||
| private def isInt(columnType: String): Boolean = { | ||
| columnType.contains("int") || columnType == "serial" | ||
| } | ||
| private def isUUID(value: String): Boolean = { | ||
| try { | ||
| UUID.fromString(value) | ||
| true | ||
| } catch { | ||
| case _: IllegalArgumentException => false | ||
| } | ||
|
Antonio171003 marked this conversation as resolved.
Outdated
|
||
| } | ||
| private def isText(columnType: String): Boolean = { | ||
| columnType == "text" || columnType == "citext" || columnType == "varchar" || columnType == "char" | ||
| } | ||
|
Antonio171003 marked this conversation as resolved.
Outdated
|
||
| def getTableData( | ||
| settings: TableSettings, | ||
| columns: List[TableColumn], | ||
|
|
@@ -88,15 +105,21 @@ object DatabaseTablesDAO { | |
|
|
||
| val conditionsSql = queryParameters.filters | ||
| .map { case FilterParameter(filterField, filterValue) => | ||
| val columnType = columns.find(_.name.equals(filterField)).getOrElse(TableColumn("", "text")).`type` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to create a hardcoded |
||
| filterValue match { | ||
| case dateRegex(_, _, _) => | ||
| case dateRegex(_, _, _) if columnType == "date" => | ||
| s"DATE($filterField) = ?" | ||
|
|
||
| case _ => | ||
| if (filterValue.toIntOption.isDefined || filterValue.toDoubleOption.isDefined) | ||
| if ( | ||
| (filterValue.toIntOption.isDefined && isInt(columnType)) || | ||
| (filterValue.toDoubleOption.isDefined && isDouble(columnType)) || | ||
|
Antonio171003 marked this conversation as resolved.
Outdated
|
||
| (isUUID(filterValue) && columnType == "uuid") | ||
| ) | ||
| s"$filterField = ?" | ||
| else | ||
| else if (isText(columnType)) | ||
| s"$filterField LIKE ?" | ||
| else | ||
| s"CAST($filterField AS TEXT) LIKE ?" | ||
|
Antonio171003 marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
| .mkString("WHERE ", " AND ", " ") | ||
|
|
@@ -111,20 +134,22 @@ object DatabaseTablesDAO { | |
| val preparedStatement = conn.prepareStatement(sql) | ||
|
|
||
| queryParameters.filters.zipWithIndex | ||
| .foreach { case (FilterParameter(_, filterValue), index) => | ||
| .foreach { case (FilterParameter(filterField, filterValue), index) => | ||
| // We have to increment index by 1 because SQL parameterIndex starts in 1 | ||
| val sqlIndex = index + 1 | ||
|
|
||
| val columnType = columns.find(_.name.equals(filterField)).getOrElse(TableColumn(filterField, "text")).`type` | ||
|
Antonio171003 marked this conversation as resolved.
Outdated
|
||
| filterValue match { | ||
| case dateRegex(year, month, day) => | ||
| case dateRegex(year, month, day) if columnType == "date" => | ||
| val parsedDate = LocalDate.of(year.toInt, month.toInt, day.toInt) | ||
| preparedStatement.setDate(sqlIndex, Date.valueOf(parsedDate)) | ||
|
|
||
| case _ => | ||
| if (filterValue.toIntOption.isDefined) | ||
| if (filterValue.toIntOption.isDefined && isInt(columnType)) | ||
|
Antonio171003 marked this conversation as resolved.
Outdated
|
||
| preparedStatement.setInt(sqlIndex, filterValue.toInt) | ||
| else if (filterValue.toDoubleOption.isDefined) | ||
| else if (filterValue.toDoubleOption.isDefined && isDouble(columnType)) | ||
|
Antonio171003 marked this conversation as resolved.
Outdated
|
||
| preparedStatement.setDouble(sqlIndex, filterValue.toDouble) | ||
| else if (isUUID(filterValue) && columnType == "uuid") | ||
|
Antonio171003 marked this conversation as resolved.
Outdated
|
||
| preparedStatement.setObject(sqlIndex, UUID.fromString(filterValue)) | ||
| else | ||
| preparedStatement.setString(sqlIndex, s"%$filterValue%") | ||
| } | ||
|
|
@@ -230,7 +255,7 @@ object DatabaseTablesDAO { | |
| } | ||
| def create( | ||
| tableName: String, | ||
| fieldsAndValues: Map[TableColumn, String], | ||
| fieldsAndValues: Map[TableColumn, Serializable], | ||
|
Antonio171003 marked this conversation as resolved.
Outdated
|
||
| primaryKeyField: String, | ||
| primaryKeyType: PrimaryKeyDataType = PrimaryKeyDataType.UUID | ||
| )(implicit | ||
|
|
@@ -260,7 +285,7 @@ object DatabaseTablesDAO { | |
|
|
||
| def update( | ||
| tableName: String, | ||
| fieldsAndValues: Map[TableColumn, String], | ||
| fieldsAndValues: Map[TableColumn, Serializable], | ||
|
Antonio171003 marked this conversation as resolved.
Outdated
|
||
| primaryKeyField: String, | ||
| primaryKeyValue: String, | ||
| primaryKeyType: PrimaryKeyDataType = PrimaryKeyDataType.UUID | ||
|
|
||
This file contains hidden or 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
13 changes: 13 additions & 0 deletions
13
spra-play-server/src/main/scala/net/wiringbits/spra/admin/utils/StringParse.scala
This file contains hidden or 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,13 @@ | ||
| package net.wiringbits.spra.admin.utils | ||
|
|
||
| object StringParse { | ||
|
|
||
| def stringToByteArray(value: String): Array[Byte] = { | ||
| try { | ||
| value.replaceAll("[\\[\\]\\s]", "").split(",").map(_.toByte) | ||
|
Antonio171003 marked this conversation as resolved.
Outdated
|
||
| } catch { | ||
| case _: NumberFormatException => Array.emptyByteArray | ||
| } | ||
| } | ||
|
Antonio171003 marked this conversation as resolved.
Outdated
|
||
|
|
||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
75 changes: 75 additions & 0 deletions
75
spra-web/src/main/scala/net/wiringbits/spra/ui/web/facades/DataProvider.scala
This file contains hidden or 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,6 +1,81 @@ | ||
| package net.wiringbits.spra.ui.web.facades | ||
|
|
||
| import org.scalajs.dom | ||
| import org.scalajs.dom.{Blob, File} | ||
|
|
||
| import scala.concurrent.Future | ||
| import scala.scalajs.js | ||
| import scala.scalajs.js.{JSON, Promise} | ||
| import scala.scalajs.js.annotation.{JSExportTopLevel, JSImport} | ||
| import scala.concurrent.ExecutionContext.Implicits.global | ||
| import scala.scalajs.js.typedarray.{ArrayBuffer, Int8Array, Uint8Array} | ||
|
|
||
| @js.native | ||
| trait DataProvider extends js.Object | ||
|
|
||
| @js.native | ||
| @JSImport("ra-data-simple-rest", JSImport.Default) | ||
|
Antonio171003 marked this conversation as resolved.
|
||
| def simpleRestProvider(url: String): DataProvider = js.native | ||
| @js.native | ||
|
Antonio171003 marked this conversation as resolved.
|
||
| @JSImport("react-admin", "withLifecycleCallbacks") | ||
|
Antonio171003 marked this conversation as resolved.
|
||
| object WithLifecycleCallbacks extends js.Object { | ||
| def apply(dataProvider: DataProvider, callbacks: js.Array[js.Object]): DataProvider = js.native | ||
| } | ||
|
|
||
| def prepareRequest(params: js.Dynamic) = { | ||
| val rawFile = params.data.rawFile.asInstanceOf[File] | ||
|
|
||
| val imageFuture = convertImageToByteArray(rawFile) | ||
|
|
||
| imageFuture.`then` { value => | ||
| val newParams = params | ||
| params.updateDynamic("data")(value.asInstanceOf[js.Any]) | ||
| params | ||
| } | ||
|
Antonio171003 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| def processResponse(record: js.Dynamic) = { | ||
| val hexImage = record.data.asInstanceOf[String] | ||
| val urlImage = convertHexToImage(hexImage) | ||
| record.updateDynamic("data")(urlImage) | ||
| record | ||
| } | ||
|
|
||
| def convertImageToByteArray(file: dom.File): js.Promise[String] = { | ||
| val promise = new js.Promise[String]((resolve, reject) => { | ||
| val reader = new dom.FileReader() | ||
| reader.onload = { (e: dom.Event) => | ||
| val arrayBuffer = reader.result.asInstanceOf[ArrayBuffer] | ||
| val byteArray = new Int8Array(arrayBuffer).toArray | ||
| resolve(byteArray.mkString("[", ", ", "]")) | ||
| } | ||
| reader.onerror = { (e: dom.Event) => | ||
| reject(new js.Error("Failed to read file")) | ||
| } | ||
| reader.readAsArrayBuffer(file) | ||
| }) | ||
|
|
||
| promise | ||
|
Antonio171003 marked this conversation as resolved.
Outdated
|
||
| } | ||
| def convertHexToImage(imageHex: String): String = { | ||
|
|
||
| val hex = imageHex.tail.tail | ||
|
Antonio171003 marked this conversation as resolved.
Outdated
|
||
|
|
||
| val imageBinary: Array[Byte] = | ||
| if ((hex.length % 2) == 1) | ||
| Array.empty | ||
| else | ||
| try { | ||
| val binary = hex | ||
| .grouped(2) | ||
| .map { hex => | ||
| Integer.parseInt(hex, 16).toByte | ||
| } | ||
| .toArray | ||
| binary | ||
| } catch case _ => Array.empty | ||
|
Antonio171003 marked this conversation as resolved.
Outdated
|
||
|
|
||
| val byteArray = Uint8Array(js.Array(imageBinary.map(_.toShort): _*)) | ||
|
|
||
| dom.URL.createObjectURL(dom.Blob(js.Array(byteArray.buffer))) | ||
| } | ||
|
Antonio171003 marked this conversation as resolved.
Outdated
Antonio171003 marked this conversation as resolved.
Outdated
|
||
26 changes: 22 additions & 4 deletions
26
spra-web/src/main/scala/net/wiringbits/spra/ui/web/facades/package.scala
This file contains hidden or 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,10 +1,28 @@ | ||
| package net.wiringbits.spra.ui.web | ||
|
|
||
| import scala.scalajs.js | ||
| import scala.scalajs.js.annotation.JSImport | ||
|
|
||
| package object facades { | ||
| @js.native | ||
| @JSImport("ra-data-simple-rest", JSImport.Default) | ||
| def simpleRestProvider(url: String): DataProvider = js.native | ||
|
|
||
| def createDataProvider(url: String): DataProvider = { | ||
|
|
||
| val baseDataProvider = simpleRestProvider(url) | ||
|
|
||
| val dataProvider = WithLifecycleCallbacks( | ||
| baseDataProvider, | ||
| js.Array( | ||
| js.Dynamic.literal( | ||
| resource = "images", | ||
| afterRead = (record: js.Dynamic, dataProvider: js.Any) => { | ||
| processResponse(record) | ||
| }, | ||
| beforeSave = (data: js.Dynamic, dataProvider: js.Any) => { | ||
| prepareRequest(data) | ||
| } | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| dataProvider | ||
|
Antonio171003 marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.