How it works...

In this recipe, we focused on deleting the user data that was saved in the database. We activated DELETE requests to the /users/:id endpoint for deleting the previous records by the object ID.

Starting at the top, we referenced the external nickel crate using the extern keyword and loaded all of its macros with #[macro_use]. The nickel is the application object and surface that holds all public APIs. It's a struct, which implements all the fundamental methods for performing all the web application tasks.

In the main function, we first assign server mutable instances to a mutable variable and create a new nickel application object with Nickel::new(), which creates an instance of nickel with default error handling. Similarly, we create a mutable router instance and assign it to Nickel::router(), which will take care of handling the different endpoints.

We will be getting the DELETE /users/:id route working, for which we will be using the different crates that we downloaded initially, which are the units of rustc_serialize, bson, and MongoDB.

Next up, we create a complex data structure, which is a User struct that is encodable and decodable and that represents our user data fields.

This is the final step for this end-to-end API, where we allow the users collection elements to be deleted by their objectId. We can do this with the MongoDB Rust driver's delete_one method.

We first establish a connection with the MongoDB service so that we can store our data, which we just parsed from the input string. We achieve this with Client::connect("localhost", 27017), where 27017 is the port in which the MongoDB service is running, and the coll variable connected the particular collection in the database with client.db("rust-cookbook").collection("users"), where rust-cookbook is the database and users is the collection.

We get the objectId from the request parameters and assign it to object_id with request.param("id").unwrap(). Then, we use the ObjectId::with_string helper to decode the string representation of the objectId, after which it can be used in the delete_one method to remove the document for that user. With the DELETE /users/:id route in place, we should be able to remove users from the database when we make a request to it and include objectId as a parameter.

Using the server.utilize method, we add the endpoints to the server instance and register the handler that will be invoked among other handlers before each request, by passing the router instance. The server.listen method listens to the API requests on 127.0.0.1:9000, where it binds and listens for connections on the given host and port.

Consider posting multiple data entries using the POST web service to store data values in the MongoDB with different ObjectID, and delete them using the service developed in this recipe to better understand the delete web service working.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.144.216