Permalink
Browse files

Finished the repository structures

  • Loading branch information...
1 parent d58d681 commit cb7a9b48c470f277e86b21b80a7f7ca540335981 @manuelkiessling committed Jan 18, 2016
Showing with 41 additions and 0 deletions.
  1. +27 −0 app/repositories/CassandraRepository.scala
  2. +14 −0 app/repositories/ProductsRepository.scala
@@ -0,0 +1,27 @@
+package repositories
+
+import com.datastax.driver.core.querybuilder.QueryBuilder
+import com.datastax.driver.core.querybuilder.QueryBuilder._
+import com.datastax.driver.core.{Row, Session}
+
+abstract class CassandraRepository[M, I](session: Session, tablename: String, partitionKeyName: String)
+ extends Repository[M, I] {
+ def rowToModel(row: Row): M
+
+ def getOneRowBySinglePartitionKeyId(partitionKeyValue: I): Row = {
+ val selectStmt =
+ select()
+ .from(tablename)
+ .where(QueryBuilder.eq(partitionKeyName, partitionKeyValue))
+ .limit(1)
+
+ val resultSet = session.execute(selectStmt)
+ val row = resultSet.one()
+ row
+ }
+
+ override def getOneById(id: I): M = {
+ val row = getOneRowBySinglePartitionKeyId(id)
+ rowToModel(row)
+ }
+}
@@ -0,0 +1,14 @@
+package repositories
+
+import com.datastax.driver.core.{Row, Session}
+import models.ProductModel
+
+class ProductsRepository(session: Session)
+ extends CassandraRepository[ProductModel, Int](session, "products", "id") {
+ override def rowToModel(row: Row): ProductModel = {
+ ProductModel(
+ row.getInt("id"),
+ row.getString("name")
+ )
+ }
+}

0 comments on commit cb7a9b4

Please sign in to comment.