Doh! Sorry about that - im using a bunch of implicit conversions that
I wrote for my specific application (as I had beef with not being able
to configure the root node of the response xml generically - im still
looking for a good solution to this but here's what I have)

Stick this in the same file: http://gist.github.com/137066

Hope that helps

Cheers, Tim

On Jun 27, 6:51 pm, fbettag <fr...@bett.ag> wrote:
> This looks alot cleaner than my try. Anyhow, using the update/create
> in one method is only useful if you give your db-objects something
> like a unique key identifier in form of a string.
> Since mine only consist of an id, name (with spaces and trash-chars),
> content and a mappedlocale, i am not sure if it is so good to simply
> create an entry for id 50234 just because somebody tries to "update"
> it ;)
> I hope you get my draft.
>
> Also this irritates me:
>   // user methods
>   def listUsers(): LiftResponse = {
>     val users = User.findAll.flatMap(_.toXml)
>     // XmlResponse(wrapXmlBody("users",users.open_!))
>     ("users",users)
>   }
>
> NetBeans shows me an Error that ("users",users) is a type mismatch.
> found: (java.lang.String, List[scala.xml.Node])
> required: net.liftweb.http.LiftResponse (obvious)
>
> I tried something like this, but understanding Box[] and making the
> best of it still gives me headaches:
>         val obj: Content = if (inContentid == "new") new Content;
>                 else Content.find(By(Content.id, inContentid.toLong));
>
> Also NetBeans always complains that wrapXmlBody was not found. I
> googled for it but i really couldn't find anything to it which would
> have turned up the import path i'd have to use.
>
> best regards
>
> On Jun 27, 11:33 am, Timothy Perrett <timo...@getintheloop.eu> wrote:
>
>
>
> > I read your post with interest - having built quite a large ROA with
> > Lift. Here's what I have with a simple bit of REST user management:
>
> >   def dispatch: LiftRules.DispatchPF = {
> >     // user methods
> >     case Req("api" :: "users" :: Nil, "", GetRequest) => () =>
> > listUsers()
> >     case Req("api" :: "user" :: user :: Nil, "", GetRequest) => () =>
> > showUser(user)
> >     case r @ Req("api" :: "user" :: user :: Nil, "", PutRequest) => ()
> > => addOrUpdateUser(user, r)
> >     case Req("api" :: "user" :: user :: Nil, "", DeleteRequest) => ()
> > => deleteUser(user)
> >    ....
> >   }
>
> >   // user methods
> >   def listUsers(): LiftResponse = {
> >     val users = User.findAll.flatMap(_.toXml)
> >     // XmlResponse(wrapXmlBody("users",users.open_!))
> >     ("users",users)
> >   }
>
> >   def showUser(inUsername: String): LiftResponse = {
> >     val user: Box[NodeSeq] = for(user <- User.find(By(User.username,
> > inUsername))) yield user.toXml
> >     ("users",user)
> >   }
>
> >   def addOrUpdateUser(inUsername: String, req: Req): LiftResponse = {
> >     var user = User.find(By(User.username, inUsername)) openOr new
> > User
> >     user.username(inUsername)
> >     req.xml match {
> >       case Full(<user>{paramaters @ _*}</user>) => {
> >         for(paramater <- paramaters){
> >           paramater match {
> >             case <firstname>{firstname}</firstname> => user.firstName
> > (firstname)
> >             case <lastname>{lastname}</lastname> => user.lastName
> > (lastname)
> >             case <email>{email}</email> => user.email(email)
> >             case <superuser>{superuser}</superuser> => user.superUser
> > (superuser.text.toBoolean)
> >             case <active>{active}</active> => user.active
> > (active.text.toBoolean)
> >             case <password>{password}</password> => user.password
> > (password.text)
> >             case _ =>
> >           }
> >         }
> >         try {
> >           user.save
> >           CreatedResponse(wrapXmlBody("user",user.toXml), "text/xml")
> >         } catch {
> >           case e => Log.error("Could not save user", e);
> > InternalServerErrorResponse()
> >         }
> >       }
> >       case _ => Log.error("Request was malformed"); BadResponse()
> >     }
> >   }
>
> > The main difference is that I see no need for seperate add and update
> > methods - generally speaking the semantics are the same in rest. For
> > instance:
>
> > GET - /users (lists all the users)
> > GET - /user/timperrett (displays just timperrett user)
> > PUT - /user/timperrett (if no user exists, create a new one,
> > otherwise, update the existing one)
>
> > This is a fairly pure ROA, if you have url's like /user/add/timperrett
> > - that is less REST and more RPC action based servicing.
>
> > Thoughts?
>
> > Cheers, Tim
>
> > On Jun 27, 5:15 am, fbettag <fr...@bett.ag> wrote:
>
> > > Hey guys,
>
> > > i've been playing around with REST all night, the list, add and delete
> > > is working great so far.
> > > The problem is, that upon update, it returns me an empty page and
> > > doesn't run through the update functionality.
> > > Any ideas what the problem in my update method might be?
>
> > > This is what i got from the samples of the liftweb sources:
>
> > > object Layouts {
> > >         // Register the Layouts webservice with the dispatcher
> > >         def init() {
> > >                 LiftRules.dispatch.append(NamedPF("Layouts WebService") {
> > >                         case Req("layouts" :: "list" :: Nil, _, 
> > > GetRequest) =>
> > >                                 () => Full(list())
>
> > >                         case Req("layouts" :: "add" :: Nil, _, rt)
> > >                                 if rt == GetRequest || rt == PostRequest 
> > > || rt == PutRequest =>
> > >                                 () => Full(add())
>
> > >                 case Req("layouts" :: "update" :: Nil, _, rt)
> > >                                 if rt == GetRequest || rt == PostRequest 
> > > || rt == PutRequest =>
> > >                                 () => Full(update())
>
> > >                 case Req("layouts" :: "delete" :: Nil, _, rt)
> > >                         if rt == GetRequest || rt == PostRequest || rt == 
> > > DeleteRequest
> > > =>
> > >                                 () => Full(delete())
> > >                 })
> > >         }
>
> > >         // List all Layouts as XML
> > >         def list(): XmlResponse =
> > >                 XmlResponse(
> > >                         <results>
> > >                                 {
> > >                                         Layout.findAll.map(_.toXml)
> > >                                 }
> > >                         </results>
> > >                 )
>
> > >         // extract the parameters, create the layout
> > >         // return the appropriate response
> > >         def add(): LiftResponse =
> > >                 (for {
> > >                         name <- S.param("name") ?~ "name parameter 
> > > missing"
> > >                         layout <- S.param("layout") ?~ "layout content 
> > > missing"
> > >                         language <- S.param("language") ?~ "language 
> > > parameter missing"
> > >                 } yield {
> > >                         val l = 
> > > Layout.create.name(name).layout(layout).language(language)
> > >                         l.save
> > >                 }) match {
> > >                         case Full(success) => XmlResponse(<add 
> > > success={success.toString}/>)
>
> > >                         case Failure(msg, _, _) => 
> > > NotAcceptableResponse(msg)
> > >                         case _ => NotFoundResponse()
> > >                 }
>
> > >         // extract the parameters, create the layout
> > >         // return the appropriate response
> > >         def update(): LiftResponse =
> > >                 (for {
> > >                         id <- S.param("id") ?~ "id parameter missing"
> > >                         name <- S.param("name")
> > >                         layout <- S.param("layout")
> > >                         language <- S.param("language")
> > >                 } yield {
> > >                         var l = Layout.findAll(By(Layout.id, 
> > > id.toLong)).first
> > >                         if (name != "") l.name(name)
> > >                         if (layout != "") l.layout(layout)
> > >                         if (language != "") l.language(language)
> > >                         l.save
> > >                 }) match {
> > >                         case Full(success) => XmlResponse(<update success=
> > > {success.toString}/>)
> > >                         case Failure(msg, _, _) => 
> > > NotAcceptableResponse(msg)
> > >                         case _ => NotFoundResponse()
> > >                 }
>
> > >         // extract the parameters, delete the layout
> > >         // return the appropriate response
> > >         def delete(): LiftResponse =
> > >                 (for {
> > >                         id <- S.param("id") ?~ "id parameter missing"
> > >                 } yield {
> > >                         Layout.findAll(By(Layout.id, 
> > > id.toLong)).first.delete_!
> > >                 }) match {
> > >                         case Full(success) => XmlResponse(<delete success=
> > > {success.toString}/>)
> > >                         case Failure(msg, _, _) => 
> > > NotAcceptableResponse(msg)
> > >                         case _ => NotFoundResponse()
> > >                 }
>
> > > }
>
> > > The Idea is to create a Scala/Liftweb based CMS which is administrated
> > > over a jQuery Window Interface. So a user can manage everything (and
> > > see live-changes on the page) without having an extra Browser-Window
> > > for the administration interface open.
>
> > > Authentication isn't builtin yet tho ;) I'll get to that when the bare
> > > system is working.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to