9
CSCI 6962: Server-side Design and Programming Shopping Carts and Databases

CSCI 6962: Server-side Design and Programming Shopping Carts and Databases

Embed Size (px)

DESCRIPTION

Storing Shopping Carts Goal: Store cart in database between user sessions – If first user visit, cart empty – If user has created cart previously, retrieve it One solution: – Assign each cart a unique cart ID – Associate cart ID with key user field (such as ) – Store all cart items in single table – Each record contains corresponding cart ID

Citation preview

Page 1: CSCI 6962: Server-side Design and Programming Shopping Carts and Databases

CSCI 6962: Server-side Design and Programming

Shopping Carts and Databases

Page 2: CSCI 6962: Server-side Design and Programming Shopping Carts and Databases

Outline

• Cart IDs• Creating a customer table• Structure of the carts table

Page 3: CSCI 6962: Server-side Design and Programming Shopping Carts and Databases

Storing Shopping Carts

• Goal: Store cart in database between user sessions– If first user visit, cart empty– If user has created cart previously, retrieve it

• One solution:– Assign each cart a unique cart ID– Associate cart ID with key user field (such as email)– Store all cart items in single table– Each record contains corresponding cart ID

Page 4: CSCI 6962: Server-side Design and Programming Shopping Carts and Databases

Customer Table

• Key field (such as email address)• Cart ID of current cart• Perhaps other customer information (address, etc.)

Page 5: CSCI 6962: Server-side Design and Programming Shopping Carts and Databases

Signing In

• User prompted to “sign in” with key field– Will probably also ask for password (separate table)

• Query customer table for the key field• If not found, generate new cart ID– Should not be session ID (too public)– Can be random number – Query database to make sure not already used

• Insert new record with Key field and cart ID

Page 6: CSCI 6962: Server-side Design and Programming Shopping Carts and Databases

Carts Database

• Big table containing all items for all customers– Cart ID of the cart item belongs to– Item ID of that item – Other information that cannot be recreated from

the item ID (such as quantity)

Page 7: CSCI 6962: Server-side Design and Programming Shopping Carts and Databases

Loading a Cart

• Query Customer table for key field on user sign in• If user record found, extract corresponding cart ID• Query Carts table for all records with that cart ID• Loop through results:– Extract the Item ID from that record– Extract other information (such as quantity)– Use ID to construct an item object – Add that object to the Cart object

Page 8: CSCI 6962: Server-side Design and Programming Shopping Carts and Databases

Storing a Cart

• Key: When Cart ID generated/retrieved, store in session

• For each cart item insert record into Carts table:– Cart ID (from session)– Item ID (from cart item)– Other necessary data (quantity, etc.)

Page 9: CSCI 6962: Server-side Design and Programming Shopping Carts and Databases

Storing a Cart

• Problem: That user may already have a cart stored in the table– Simply adding the current cart would create duplicate

records for items saved previously• Solution:– Delete all records from Carts table with the same

Cart ID (removing previous cart)– Store all items in current cart as new records • Make sure both done as single transaction