PageRenderTime 67ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/salat/src/main/scala/prasinous/dao/LibraryDAO.scala

https://github.com/scala-phase/scala-orms
Scala | 73 lines | 53 code | 19 blank | 1 comment | 0 complexity | cdac49bdf22f43dd6092290ec4828270 MD5 | raw file
  1. package prasinous.dao
  2. import com.novus.salat._
  3. import com.novus.salat.global._
  4. import com.novus.salat.dao._
  5. import com.mongodb.casbah.Imports._
  6. import prasinous.model._
  7. import prasinous.{collections => db}
  8. import com.mongodb.casbah.commons.MongoDBList
  9. object AuthorDAO extends SalatDAO[Author, ObjectId](collection = db.author) {
  10. class BookAuthorCollection(collection: MongoCollection, parentIdField: String)
  11. extends ChildCollection[BookAuthor, ObjectId](collection, parentIdField)
  12. val bookAuthor = new BookAuthorCollection(collection = db.bookAuthor, parentIdField = "authorId")
  13. def idByAuthorName(firstName: String, lastName: String): Option[ObjectId] = {
  14. // author collection has a unique index on firstName, lastName
  15. ids(MongoDBObject("firstName" -> firstName, "lastName" -> lastName)).firstOption
  16. }
  17. def addBook(a: Author, b: Book) = {
  18. BookDAO.insert(b)
  19. bookAuthor.insert(BookAuthor(bookId = b.id, authorId = a.id))
  20. }
  21. def booksByAuthor(author: Author): List[Book] = {
  22. val bookIds = bookAuthor.primitiveProjectionsByParentId[ObjectId](parentId = author.id, field = "bookId")
  23. BookDAO.find(ref = MongoDBObject("_id" -> MongoDBObject("$in" -> MongoDBList(bookIds: _*))))
  24. .sort(MongoDBObject("title" -> 1))
  25. .toList
  26. }
  27. }
  28. object BookDAO extends SalatDAO[Book, ObjectId](collection = db.book) {
  29. class BookAuthorCollection(collection: MongoCollection, parentIdField: String)
  30. extends ChildCollection[BookAuthor, ObjectId](collection, parentIdField)
  31. class BorrowalCollection(collection: MongoCollection, parentIdField: String)
  32. extends ChildCollection[Borrowal, ObjectId](collection, parentIdField)
  33. val bookAuthor = new BookAuthorCollection(collection = db.bookAuthor, parentIdField = "bookId")
  34. val borrowal = new BorrowalCollection(collection = db.borrowal, parentIdField = "bookId")
  35. def idByTitle(title: String): Option[ObjectId] = {
  36. ids(MongoDBObject("title" -> title)).firstOption
  37. }
  38. def authorsForBook(book: Book): List[Author] = {
  39. val bookIds = bookAuthor.primitiveProjectionsByParentId[ObjectId](parentId = book.id, field = "authorId")
  40. AuthorDAO.find(ref = MongoDBObject("_id" -> MongoDBObject("$in" -> MongoDBList(bookIds: _*))))
  41. .sort(MongoDBObject("lastName" -> 1))
  42. .toList
  43. }
  44. def borrowalHistoryForBook(book: Book): List[Borrowal] = {
  45. borrowal.findByParentId(book.id).
  46. sort(MongoDBObject("scheduledToReturnOn" -> -1)).
  47. toList
  48. }
  49. }
  50. object BorrowerDAO extends SalatDAO[Borrower, ObjectId](collection = db.borrower) {
  51. class BorrowalCollection(collection: MongoCollection, parentIdField: String)
  52. extends ChildCollection[Borrowal, ObjectId](collection, parentIdField)
  53. val borrowal = new BorrowalCollection(collection = db.borrowal, parentIdField = "borrowerId")
  54. }