Server Side Swift with Vapor - Part 10: Finishing | Ray Wenderlich

Learn how to retrieve individual models in Fluent using parameters and learn how to update and delete models using Fluent.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4493-server-side-swift-with-vapor/lessons/10

Hi Tim,

You mention that ‘‘extension User: Parameter {}’’ should be included in the UserController.swift file.
What is the logic behind putting it there as opposed to the User.swift model file?

Thanks,
Guy

@kidrong that extension can actually go anywhere in that module. From a design point of view it can be helpful to put it in the file where it is required (ie where you use the parameter)

Yes it seems more powerful, but the syntax lacks simplicity. It becomes messy in no time, especially inside the put and delete handler. This should be abstracted away
Or maybe this is just because flatMap is confusing when we are used to another usage.
But once we understand the logic, it gets better.

hi i’m writing this code but i have a error

func getHandler(_ req : Request) throws -> Future<Acronym> {
    return try req.parameter(Acronym.self)
}

error:
Missing argument for parameter ‘using’ in call

click it and changed my code
return try req.parameter(Acronym.self, using: <#Container#>)

@lashkari have you conformed Acronym to Parameter?

i’m don’t use Parameter for Acronym

@lashkari at the bottom of the AcronymsController.swift you should have extension Acronym: Parameter {}? If not try adding that in and see if it fixes it

ohhh :astonished: no i’m don’t use it

Thanks a lot :pray::pray:

1 Like

Hi Tim

in the video you said parameter call returns a future and we cannot call delete on future itself. but when i try this it works fine. Is it wrong this way ?

func deleteHandler(_ request:Request) throws -> Future<HTTPStatus> {
    return try request.parameter(Acronym.self).delete(on: request).transform(to: .noContent)
}

thanks

@0xtim Do you have any feedback regarding this? Thank you - much appreciated! :]

@alinakhli you are right! Looks like those conveniences were added after the videos were recorded - nice find!

1 Like

Hi Tim,

First, your course is incredible, i’m loved the Vapor.

I have one question, in this video you show as retrieve, update and delete one specific acronym, right?!

My question is about update method:

for a correct operation i need to send the “short” and “long” atributes in my request body as JSON.

It’s possible to send the only attributes that i want update?

For example: I want to change “long” of “What The Flap” to “What the Fugde”

In my test, this action throw an error, but, if send the two attributes, it works perfectly

By chance, if i change the atribute to optional, will this allow me to have this behavior?

Thanks for your help

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

@maclacerda there is a better update mechanism planned at some point but for now you need another approach. You could make it optional, but you may not want this in your model. Another way to do this is to create an AcronymUpdateDate object that looks like:

struct AcronymUpdateData: Content {
  let short: String?
  let long: String?
}

Then you can decode it and check each thing:

let updateData = try req.content.decode(AcronymUpdateData.self)
if let newShort = udpateData.short {
  existingAcronym.short = newShort
}

etc - make sense?

Great!

Thanks for your help

1 Like

Please note:

:warning:️ req.parameter(…) has been renamed to req.parameters.next(…)

@tyler927 yep I’m waiting for all the changes to come through before re-recording it all!

@0xtim

func getHandler(_ req: Request) throws -> Future {
return try req.parameter(Acronym.self)
}

it still shows this error “Value of type ‘Request’ has no member ‘parameter’”