Build an AngularJS App Powered by Python EVE: Part 2

In the previous part of this series, we implemented the sign-in and sign-up process using the Eve API we created in the first part of the series. We used Node.js and AngularJS to create and get our app running.

In this tutorial, we'll take our app development to the next level by using the Add, Edit and Delete APIs to create items in the user home page.

Getting Started

To get started, clone the source code of the previous tutorial from GitHub.

Once done, navigate to AngularEveApp_Part1 and install the dependencies.

Make sure that the Python Eve REST APIs are running. Start the app server.

Point your browser to http://localhost:3000 and you should have the application running.

Creating User Home

After the user signs in successfully, we'll take the user to the home page. So, let's start by creating userHome.html. Navigate to the public folder and create a new folder called userHome. Inside userHome create files called userHome.html and userHome.js. Open userHome.html and add the following HTML code:

Inside userHome.js define the module userHome and its template and controller. So, open userHome.js and add the following code:

Add a reference to userHome.js in the index.html page.

Inject the module userHome into the app myApp in index.js.

In the signIn function in signin.js, on the success callback of the $http request redirect to the /userHome view. 

Save the above changes and restart the node server. Point your browser to http://localhost:3000 and sign in using a valid username and password. Once successfully signed in, you should be able to view the user home page with the default view.

User Home Page with Default View

Creating an Add Task Page

Let's add a view to enable the user to add items. Navigate to the public folder and create a folder called addItem. Inside the addItem folder, create two files called addItem.html and addItem.js. Open addItem.html and add the following HTML code:

Next, open addItem.js and add the following code to define the route, template and controller.

Open userHome.html and modify the Add link href to point to the correct route.

Add a reference to addItem.js in index.html.

Inject the module addItem in the myApp in index.js.

Save the following changes and restart the node server. Once you have signed in, click on the Add link and you should be able to see the add item page.

Add Page

Adding Items Using the Eve API

In order to add items, we'll need the base64 authdata. So, on successful sign-in we'll keep the username and auth data inside an AngularJS service. Navigate to the public folder and create a folder called service. Inside the service folder create a file called service.js. Let's create a new service module called myAppService.

Add a new service to the myAppService module.

Inside the CommonProp service, we'll define a few functions to get and set the username and authData.

Include a reference to the service in index.html.

Next, inject the above created myAppService module in the signin module.

Inject the required CommonProp service in the SignInCtrl controller.

On $http success callback, add the following code to keep the username and authData in the service. 

Next, open up addItem.js and inject the myAppService module.

Inject the CommonProp service in the AddItemCtrl.

Open up addItem.html and add the ngModel and ngClick directive to the task text box and Add task button respectively.

Inside addItem.js define a new function called addItem.

Inside addItem function let's make our $http call to the Python Eve REST API. First, we'll need to get the username and the user auth data from the service.

Let's set the required headers to make the API call.

Once headers have been set, let's use $http to make the API call to add the item.

Save the changes and restart the node server. Point your browser to http://localhost:3000 and sign in. Click the Add link, enter a task and click Add Task. On successful API call you should be redirected to the user home page.

Get Tasks From the Eve API

When the user home page is loaded, we'll make an API call to fetch the items added by the particular user. In order to make the API call we'll need the username and the authData. So, inject the myAppService module in the userHome module.

Inject the CommonProp service to UserHomeCtrl.

Next, let's fetch the username and authData from CommonProp.

Set the required headers to make the API call.

In order to get the tasks created by a particular user, we'll make a $http GET request to http://127.0.0.1:5000/item?where={"username":"'+user+'"}'.

On successful API call, we'll parse the data returned and set the data into a $scope variable. 

First, let's create a tasks variable:

Now, let's parse the data returned and fill the tasks variable.

In userHome.html, we'll iterate over the tasks variable and render the tasks fetched for a particular user. We'll make use of the ngRepeat directive to iterate over the tasks variable.

Save the above changes and restart the server. Point your browser to http://localhost:3000 and sign in. On successful sign-in you should be able to see the tasks added by the signed-in user.

User Home with Tasks Listed

Editing & Deleting a Task Item

Editing Tasks

Let's add an edit and delete button to the list of tasks in the user home page, to enable editing and deleting of the tasks respectively. Open userHome.html and add the following HTML code to the .list-group div.

We'll be using bootstrap modals to show the task to be edited, so include jQuery and Bootstrap in index.html.

Add the following HTML code to userHome.html for modal pop-up for edit.

Next open up userHome.js and create a new function called editTask which will be invoked on edit button click.

As seen in the above code, we have assigned the title, id and tag of the particular record to the $scope.edit.task variable. If the user chooses to update the particular record, then we'll use the $scope.edit data for update. 

Save all the above changes and restart the server. Once signed in successfully, click on the edit button to edit a task, and you should have the modal pop-up.

Update Task popup

Earlier, we made a $http request from the UserHomeCtrl. Let's convert that into a proper function, since we'll need to call that in future. Here is the getAllTask function.

Next, create a function called update in userHome.js which we'll call to update the edited task. Inside the update function we'll be making a PATCH request to http://127.0.0.1:5000/item/(item-id) with the data to be updated. The Eve API expects a tag id header to be passed when making an update request, so we'll also be passing the tag id in the request header. Inside the update function, first we'll declare the required headers for the $http request.

Once headers have been defined, we'll make the PATCH $http request.

As seen in the above code, on successful update we have closed the edit modal and reloaded all the tasks related to the particular user.

Save all the changes and restart the server. Point your browser to http://localhost:3000 and sign in using valid credentials. Once signed in, try to edit a particular task by clicking the edit button.

Deleting Tasks

Before deleting a task we need to show a confirmation pop-up. So, in userHome.html add the following HTML code for the delete confirmation pop-up.

Next, we need to define a function called confirmDelete which we have already attached to the Delete button using the ngClick directive. Inside the confirmDelete function we'll keep the task ID and TAG which will be required when we call the Delete API. Here is the confirmDelete function:

Save the changes and restart the server. Sign in to the application and when on the user home page, click on the Delete button and you should be able to view the Delete confirmation pop-up.

Delete Confirmation Popup

Next, we'll define one more function to carry out the task deletion. We have already attached a function called deleteTask to the Delete button using the ngClick directive. Now, let's define the deleteTask function inside userHome.js.

Similar to the update function, calling the Delete request on the Eve API requires the TAG ID to be set in the header. So, first set the required headers as shown:

Next, we'll make a DELETE request to the REST API http://127.0.0.1:5000/item/(itemId)

As seen in the above code, on successful DELETE request callback we'll hide the delete confirm modal pop-up and reload the task entries by calling the getAllTask method. Here is the full deleteTask function:

Save the above changes and restart the server. Point your browser to http://localhost:3000 and sign in. When on the user home page, click the Delete button to delete a few tasks and it should be working.

Conclusion

In this part of the series, we saw how to use the Python Eve REST APIs created in the first part of the series in an AngularJS application. We implemented some simple CRUD operations from our AngularJS application using the Eve REST APIs. I hope this tutorial will serve as the basis for creating something awesome.

Feel free to post your comments, corrections or questions below. If you found this tutorial helpful, do visit my instructor page for other tutorials.

Source code from this tutorial is available on GitHub

Tags:

Comments

Related Articles