Web Applications / Chapter 3: Building an E-commerce App / Building an E-commerce App

E-Commerce Application

In this chapter, we are going to make an e-commerce app. An e-commerce app is a website where people buy and sell goods. For example, an online book store is an e-commerce app where people can buy and sell books. Our e-commerce app is called LWMart.

The LWMart homepage shows a list of items for sale. A logged in user can add items to the user's cart. The user can then go to the cart page and order the items in the cart. Finally, the user can visit the orders page to view the history of orders that the user placed.

Architectural Overview

LWMart stores users, items, carts, and orders in the database. The database data is stored in a computer called db.com. The database server, serve_db, also runs in db.com. The LWMart webserver is a Python application that runs in a separate computer called lwmart.com. When lwmart.com receives an HTTP request from a client, lwmart.com queries the database in db.com as necessary and sends a HTTP response back to the client. Client lwmart.com (Python Server) db.com (Database)

Components of LWMart

Database Schema

Let's consider how we want to store users, items, carts, and orders in the datbase. Let's reuse the users table from the LWBlog database. The items table contains the ID, item name, the item price, and the item description. Here is an example:

idnamepricedescription
101Smartphone800The best phone ever!
202Teddy Bear15Very fluffy!
303Shoes79The most comfortable shoes ever made!

Every user has a cart which contains 0 or more items. In database terminology, the items and the user (cart) has a many-to-one relationship. We could store the cart items in the users table, but SQL experts usually recommend creating a separate table to store this assocation. We also need to store some other data, such as the quantity of the item in the cart. Here is an example table called cart_items:

user_iditem_idquantityindex
1020110111
1020130322

The index is the location of the item in the cart relative to other items.

When the user places an order, LWMart stores the order information in the orders table. Here is an example:

iduser_idtotalcreated
12409810201782021-04-21T07:34:79Z
098344250248302023-02-21T07:34:79Z

Each order has a unique ID, the ID of the user that placed the order, the total order amount, and the date when the order was placed.

We also need to store the items in the order. This is also a many-to-one relationship. Here is an example table called order_items:

order_iditem_idquantityprice
124098303239
0983441011800
098344202215

The price is the price of the item at the time the order was placed.

The name of the database that contains the users, items, cart_items, orders, and order_items tables is called lwmart.

Retrieving the Home Page

When users visit lwmart.com, the browser sends the following request:

GET / HTTP/1.1

When the serve receives the request, it constructs the home page and sends it back to the client. The home page consists of all the items that are currently for sale. The server retrieves the items from the database by issuing the following request:

SELECT id,name,price,FROM items

Each item HTML should include the link to the item page. For example, if the item ID is 202 and the item name is "Teddy Bear," then the corresponding HTML snippet might be:

Which appears as follows:

Teddy Bear

Quiz (5 points)

Let's build the LWMart homepage.

The simulator below consists of 3 computers:

  • The e-commerce server (lwmart.com)
  • The client (client.com)
  • The database server (db.com)

db.com contains the database. Please start serve_db in TERMINAL_DB.

The LWMart server code should be stored in lwmart.com at /lwmart.py.

Please use do_http to send test requests to lwblog.com

Do your work in the simulator below. Click the "Save & Submit" button to save your work and submit it for grading.

Ready Logicwalk OS Simulator
Become a subscriber to save your progress, see the correct answer, and more!
Previous Lesson Next Lesson

Comments

Please log in to add comments