Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Finished the repository structures
- Loading branch information
1 parent
d58d681
commit cb7a9b4
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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) | |||
} | |||
} |
This file contains 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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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") | |||
) | |||
} | |||
} |