-
Notifications
You must be signed in to change notification settings - Fork 34
/
SongsDatabase.scala
50 lines (44 loc) · 1.49 KB
/
SongsDatabase.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.cassandra.phantom.modeling.database
import com.cassandra.phantom.modeling.connector.Connector._
import com.cassandra.phantom.modeling.entity.Song
import com.cassandra.phantom.modeling.model.{SongsByArtistModel, SongsModel}
import com.outworkers.phantom.connectors.CassandraConnection
import com.outworkers.phantom.dsl._
import scala.concurrent.Future
/**
* This is our Database object that wraps our two existing tables,
* giving the ability to receive different connectors
* for example: One for production and other for testing
*/
class SongsDatabase(override val connector: CassandraConnection) extends Database[SongsDatabase](connector) {
object SongsModel extends SongsModel with connector.Connector
object SongsByArtistsModel extends SongsByArtistModel with connector.Connector
/**
* Save a song in both tables
*
* @param songs
* @return
*/
def saveOrUpdate(songs: Song): Future[ResultSet] = {
Batch.logged
.add(SongsModel.store(songs))
.add(SongsByArtistsModel.store(songs))
.future()
}
/**
* Delete a song in both table
*
* @param song
* @return
*/
def delete(song: Song): Future[ResultSet] = {
Batch.logged
.add(SongsModel.delete.where(_.id eqs song.id))
.add(SongsByArtistsModel.delete.where(_.artist eqs song.artist).and(_.id eqs song.id))
.future()
}
}
/**
* This is the database, it connects to a cluster with multiple contact points
*/
object Database extends SongsDatabase(connector)