Skip to content

Instantly share code, notes, and snippets.

@gertjana
Created June 8, 2011 11:53
Show Gist options
  • Save gertjana/1014284 to your computer and use it in GitHub Desktop.
Save gertjana/1014284 to your computer and use it in GitHub Desktop.
Getting a list Of Authors for a User through liftweb's REST interface
Given the Following Mapper Data Model:
Author <- 1) -> Book <- 2) -> User
1) An Author can have more books while books can be written by multiple authors (N-N)
2) A user can own multiple books, while a book can have multiple owners (N-N)
note: When successfully authenticating in a user his API key is returned which is used in all other requests
serve {
case "api" :: key :: "user" :: "authors" :: "all" :: _ XmlGet _ => {
<Authors> {
Book.findAll(In(Book.id ,BookUser.book, By(BookUser.user, getUserIdFromKey(key))))
.map(book => { book.authors.get })
.foldLeft(List[Author]())(_ ++ _)
.distinct
.map(author => author.toXml)
}
</Authors>
}
case "api" :: key :: "user" :: "authors" :: "all" :: _ JsonGet _ => {
JsonWrapper ("Authors", {
Book.findAll(In(Book.id ,BookUser.book, By(BookUser.user, getUserIdFromKey(key))))
.map(book => { book.authors.get })
.foldLeft(List[Author]())(_ ++ _)
.distinct
.map(author => author.toJson)
})
}
}
//helper methods
def getUserIdFromKey(key:String):Long = {
User.find(By(User.key, key) match {
case Full(user) => user.id
case (_) => 0
}
}
def JsonWrapper(name:String, value:JValue):JValue {
(name, value)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment