mirror of
https://github.com/FoggedLens/deflock.git
synced 2026-07-09 21:58:39 +02:00
update popup, update pages
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package models
|
||||
import software.amazon.awssdk.services.dynamodb.model.AttributeValue
|
||||
import spray.json._
|
||||
|
||||
case class OperatorInfo(wikidataId: String, transparencyPortalUrl: String)
|
||||
|
||||
object OperatorInfoJsonProtocol extends DefaultJsonProtocol {
|
||||
implicit val operatorInfoFormat: RootJsonFormat[OperatorInfo] = jsonFormat2(OperatorInfo)
|
||||
|
||||
def fromAttributeValueMap(map: Map[String, AttributeValue]): Option[OperatorInfo] = {
|
||||
for {
|
||||
wikidataId <- map.get("wikidataId").flatMap(attr => Option(attr.s()))
|
||||
transparencyPortalUrl <- map.get("transparencyPortalUrl").flatMap(attr => Option(attr.s()))
|
||||
} yield OperatorInfo(wikidataId, transparencyPortalUrl)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package services
|
||||
|
||||
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider
|
||||
import software.amazon.awssdk.regions.Region
|
||||
import software.amazon.awssdk.services.dynamodb.DynamoDbClient
|
||||
import software.amazon.awssdk.services.dynamodb.model._
|
||||
|
||||
import scala.concurrent.{ExecutionContext, Future}
|
||||
import scala.collection.JavaConverters._
|
||||
import scala.util.Try
|
||||
|
||||
class DynamoDBClient(region: Region = Region.US_EAST_1)(implicit ec: ExecutionContext) {
|
||||
|
||||
// Create the base DynamoDB client
|
||||
private val dynamoDbClient = DynamoDbClient.builder()
|
||||
.region(region)
|
||||
.credentialsProvider(DefaultCredentialsProvider.create())
|
||||
.build()
|
||||
|
||||
// Get an item using the standard client
|
||||
private def getItem(tableName: String, key: java.util.Map[String, AttributeValue]): Future[Option[java.util.Map[String, AttributeValue]]] = Future {
|
||||
val request = GetItemRequest.builder()
|
||||
.tableName(tableName)
|
||||
.key(key)
|
||||
.build()
|
||||
|
||||
dynamoDbClient.getItem(request).item() match {
|
||||
case item if item.isEmpty => None
|
||||
case item => Some(item)
|
||||
}
|
||||
}
|
||||
|
||||
// Get item with Scala Map instead of Java Map
|
||||
def getItem(tableName: String, key: Map[String, AttributeValue]): Future[Option[Map[String, AttributeValue]]] = {
|
||||
getItem(tableName, key.asJava).map {
|
||||
_.map(_.asScala.toMap)
|
||||
}
|
||||
}
|
||||
|
||||
// Put an item
|
||||
def putItem(tableName: String, item: Map[String, AttributeValue]): Future[PutItemResponse] = Future {
|
||||
val request = PutItemRequest.builder()
|
||||
.tableName(tableName)
|
||||
.item(item.asJava)
|
||||
.build()
|
||||
|
||||
dynamoDbClient.putItem(request)
|
||||
}
|
||||
|
||||
// Delete an item
|
||||
def deleteItem(tableName: String, key: Map[String, AttributeValue]): Future[DeleteItemResponse] = Future {
|
||||
val request = DeleteItemRequest.builder()
|
||||
.tableName(tableName)
|
||||
.key(key.asJava)
|
||||
.build()
|
||||
|
||||
dynamoDbClient.deleteItem(request)
|
||||
}
|
||||
|
||||
// Scan items
|
||||
def scanItems(tableName: String): Future[List[Map[String, AttributeValue]]] = Future {
|
||||
val request = ScanRequest.builder()
|
||||
.tableName(tableName)
|
||||
.build()
|
||||
|
||||
dynamoDbClient.scan(request)
|
||||
.items()
|
||||
.asScala
|
||||
.map(_.asScala.toMap)
|
||||
.toList
|
||||
}
|
||||
|
||||
// Close resources
|
||||
def close(): Unit = {
|
||||
dynamoDbClient.close()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user