On Wed, Jul 15, 2009 at 5:38 PM, Peter Robinett <[email protected]>wrote:
>
> Thanks so much. How would I handle optional parameters? My knowledge
> of for comprehensions is quite limited...
If the optional parameter has a default value:
for {
nodeId <- S.param("node")
node <- Node.find(nodeId)
temp <- S.param("temp").flatMap(s => tryo(s.toDouble))
mah = (S.param("mah").flatMap(Helpers.asInt)) openOr 0
} Packet.create.node(node).mAh(mah).temp(temp).save
If you want to only set a field if the parameter exists
for {
nodeId <- S.param("node")
node <- Node.find(nodeId)
temp <- S.param("temp").flatMap(s => tryo(s.toDouble))
} {
val p = Packet.create.node(node).temp(temp)
S.param("mah").flatMap(Helpers.asInt).foreach(m =>
p.mAh(m))
p.save
}
>
> Peter
>
> On Jul 14, 10:25 am, David Pollak <[email protected]>
> wrote:
> > On Mon, Jul 13, 2009 at 5:31 PM, Peter Robinett <[email protected]
> >wrote:
> >
> >
> >
> >
> >
> > > Thanks, David. Here is its class definition:
> >
> > > class Packet extends LongKeyedMapper[Packet] with IdPK {
> > > def getSingleton = Packet
> > > object node extends MappedLongForeignKey(this, Node)
> > > object dt extends MappedDateTime(this)
> > > object temp extends MappedDouble(this)
> > > object mAh extends MappedDouble(this)
> > > object lux extends MappedDouble(this)
> > > object rssi extends MappedDouble(this)
> > > }
> >
> > > Also, the Web Services demo (http://demo.liftweb.net/ws) seems to have
> > > exactly what I need but the code seems to be old – I get lots of
> > > compiler errors.
> >
> > object RestAPI extends XMLApiHelper {
> > def dispatch: LiftRules.DispatchPF = {
> > case r @ Req("api" :: "packet" :: Nil, "", PostRequest) => () =>
> > addPacket(r)
> > }
> > def addPacket(req: Req): LiftResponse = {
> > for {
> > nodeId <- S.param("node")
> > node <- Node.find(nodeId)
> > temp <- S.param("temp").flatMap(s => tryo(s.toDouble))
> > } Packet.create.node(node).temp(temp).save
> >
> > OkResponse
> > }
> >
> >
> >
> > }
> >
> > > Peter
> >
> > > On Jul 13, 5:14 pm, David Pollak <[email protected]>
> > > wrote:
> > > > On Mon, Jul 13, 2009 at 5:12 PM, Peter Robinett <
> [email protected]
> > > >wrote:
> >
> > > > > Hello,
> >
> > > > > I'd appreciate any tips on how to take POSTed data and use it to
> save
> > > > > a new object. Using previous threads here I've figured out how to
> do
> > > > > the basic request processing but my limited knowledge of Mapper and
> > > > > Scala's maps mean that I'm not sure how exactly to save the data I
> > > > > receive. I'm guessing that I need to use some sort of for
> > > > > comprehension to assign all the POST parameters to the object.
> >
> > > > > Here is what I have:
> >
> > > > > object RestAPI extends XMLApiHelper {
> > > > > def dispatch: LiftRules.DispatchPF = {
> > > > > case r @ Req("api" :: "packet" :: Nil, "", PostRequest) =>
> () =>
> > > > > addPacket(r)
> > > > > }
> > > > > def addPacket(req: Req): LiftResponse = {
> > > > > val packet = Packet.create
> > > > > /* I want to assign the contents of req.params - Map("node"
> ->
> > > > > List("44"), "temp" -> List("24.32525")) - into packet */
> > > > > packet.save
> > > > > OkResponse
> > > > > }
> > > > > }
> >
> > > > Please include the source for Packet (or at least a list of fields
> and
> > > > types)
> >
> > > > > Thanks,
> > > > > Peter Robinett
> >
> > > > --
> > > > Lift, the simply functional web frameworkhttp://liftweb.net
> > > > Beginning Scalahttp://www.apress.com/book/view/1430219890
> > > > Follow me:http://twitter.com/dpp
> > > > Git some:http://github.com/dpp
> >
> > --
> > Lift, the simply functional web frameworkhttp://liftweb.net
> > Beginning Scalahttp://www.apress.com/book/view/1430219890
> > Follow me:http://twitter.com/dpp
> > Git some:http://github.com/dpp
> >
>
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Lift" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---