All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
im.actor.server.persist.contact.UserContactRepo.scala Maven / Gradle / Ivy
package im.actor.server.persist.contact
import slick.dbio.Effect.Write
import im.actor.server.db.ActorPostgresDriver.api._
import slick.profile.FixedSqlAction
import im.actor.server.model
private[contact] abstract class UserContactBase[T](tag: Tag, tname: String) extends Table[T](tag, tname) {
def ownerUserId = column[Int]("owner_user_id", O.PrimaryKey)
def contactUserId = column[Int]("contact_user_id", O.PrimaryKey)
def name = column[Option[String]]("name")
def isDeleted = column[Boolean]("is_deleted", O.Default(false))
def idx = index("idx_user_contacts_owner_user_id_is_deleted", (ownerUserId, isDeleted))
}
final class UserContactTable(tag: Tag) extends UserContactBase[model.contact.UserContact](tag, "user_contacts") {
def * = (ownerUserId, contactUserId, name, isDeleted) <> (model.contact.UserContact.tupled, model.contact.UserContact.unapply)
}
object UserContactRepo {
val contacts = TableQuery[UserContactTable]
val active = contacts.filter(_.isDeleted === false)
def byPK(ownerUserId: Int, contactUserId: Int) =
contacts.filter(c ⇒ c.ownerUserId === ownerUserId && c.contactUserId === contactUserId)
def byOwnerUserIdNotDeleted(ownerUserId: Int) =
contacts.filter(c ⇒ c.ownerUserId === ownerUserId && c.isDeleted === false)
def byPKNotDeleted(ownerUserId: Rep[Int], contactUserId: Rep[Int]) =
contacts.filter(c ⇒ c.ownerUserId === ownerUserId && c.contactUserId === contactUserId && c.isDeleted === false)
val nameByPKNotDeletedC = Compiled(
(ownerUserId: Rep[Int], contactUserId: Rep[Int]) ⇒
byPKNotDeleted(ownerUserId, contactUserId) map (_.name)
)
def byContactUserId(contactUserId: Rep[Int]) = active.filter(_.contactUserId === contactUserId)
val byContactUserIdC = Compiled(byContactUserId _)
def byPKDeleted(ownerUserId: Int, contactUserId: Int) =
contacts.filter(c ⇒ c.ownerUserId === ownerUserId && c.contactUserId === contactUserId && c.isDeleted === true)
def existsC = Compiled { (ownerUserId: Rep[Int], contactUserId: Rep[Int]) ⇒
byPKNotDeleted(ownerUserId, contactUserId).exists
}
def fetchAll = active.result
def exists(ownerUserId: Int, contactUserId: Int) = existsC((ownerUserId, contactUserId)).result
//TODO: check usages - make sure they dont need phone number
def find(ownerUserId: Int, contactUserId: Int) =
byPKNotDeleted(ownerUserId, contactUserId).result.headOption
def findIds(ownerUserId: Int, contactUserIds: Set[Int]) =
byOwnerUserIdNotDeleted(ownerUserId).filter(_.contactUserId inSet contactUserIds).map(_.contactUserId).result
def findOwners(contactUserId: Int) = byContactUserIdC(contactUserId).result
def findNotDeletedIds(ownerUserId: Int) =
byOwnerUserIdNotDeleted(ownerUserId).map(_.contactUserId).result
def findName(ownerUserId: Int, contactUserId: Int) =
nameByPKNotDeletedC((ownerUserId, contactUserId)).result
def findContactIdsAll(ownerUserId: Int) =
contacts.filter(c ⇒ c.ownerUserId === ownerUserId).map(_.contactUserId).result
def findContactIdsActive(ownerUserId: Int) =
byOwnerUserIdNotDeleted(ownerUserId).map(_.contactUserId).result
def updateName(ownerUserId: Int, contactUserId: Int, name: Option[String]): FixedSqlAction[Int, NoStream, Write] = {
contacts.filter(c ⇒ c.ownerUserId === ownerUserId && c.contactUserId === contactUserId).map(_.name).update(name)
}
def delete(ownerUserId: Int, contactUserId: Int) =
byPKNotDeleted(ownerUserId, contactUserId).map(_.isDeleted).update(true)
def insertOrUpdate(contact: model.contact.UserContact) =
contacts.insertOrUpdate(contact)
}