MP 2
MP 2
CENG 352
Database Management Systems
Spring 2023-2024
Project 2
1 Project Description
In this project you are asked to develop a simple e-commerce application (e.g. Trendyol, Ama-
zon, etc.). This is a typical application using a database. You will import data to a PostgreSQL
database. Database details will be the similar to the previous project, with additions.
Your platform will allow sellers to reach their clients with products. Firstly, you will construct
the database and import the data for this database. After those, you will create an application to
allow sellers and customers to use the service.
2 Database
2.1 Creating the database
The database name will be ceng352 2023 hw2 for this mini project. You will have 2 steps while
creating the database:
1
• Create tables and insert sample data by running SQL queries in construct db.sql.
• Import data from .csv files under data.zip directory. Import the data as-is, without doing
any kind of data processing or cleaning. Consider column delimiter as semicolon (,), consider
quote character as quote (”) and fill empty values with NULL while importing data to tables.
After these steps, you will get these tables in the database:
product_category
category_id, name
products
product_id, name, category_id, weight, price
customers
customer_id, name, surname, address, state, gender
orders
order_id, customer_id, order_time, shipping_time, status
shopping_carts
order_id, product_id, seller_id, amount
sellers
seller_id, password, session_count, plan_id
plans
plan_id, name, max_parallel_sessions
stocks
seller_id, product_id, stock_count
2
• Shopping carts have now seller id information within the entries. This will allow you to ma-
nipulate the seller’s stocks.
If max parallel sessions = 2, then the seller who is subscribed to this plan can connect to
the platform with at most 2 devices at the same time.
All sellers must have a plan and one seller is dedicated to one plan only. You are free to
invent your own plans.
3 The Software
For helpful tutorials and links, check these websites: link 1, link 2, link 3.
After the database is created, you will implement a simple program for the sellers to connect to the
platform. For simulating multiple device connections, you can open multiple terminal windows and
sign in from those terminals.
For simplicity, the platform will be a command line-like software written in Python3. You will
use psycopg2 to do PostgreSQL tasks like insert, update, read etc. These are the source files
inside source directory:
• main.py: This file is for retrieving inputs, processing requests and sending response messages
back to the user. main() function in this file acts like a service layer for the software. You
should not modify this file, you will run the software like:
• mp2.py: You will ONLY modify this file. You will implement the functions in this file
and make sellers available to sign in, sign out and read contents from the database. You will
return success message or error messages, depending on the situation.
• validators.py: This file is for validating commands. You don’t have to worry about valida-
tion, it is written for you and you can try entering incorrect commands to break the program.
• seller.py: This file contains the Seller class, which will be used to store currently authenti-
cated seller information.
• messages.py: You should ONLY use and return messages defined in this file, to get points
from this assignment.
• database.cfg: This file contains database configurations. Change them with your configu-
rations to connect the database on your local machine.
When the program starts, list of commands are shown and the program waits for command
input:
3
_> python main.py
At first, there is no signed in seller. Seller is shown as ANONYMOUS and you can only call
help, sign up, sign in, quit commands with anonymous seller. help command will remind you what
are the available commands provided by this software.
If you implement sign in command correctly and sign in with an existing seller information, the
look will change to authenticated seller like below:
Authenticated sellers can use the other commands too. These commands are available to au-
thenticated sellers only.
4 Tasks
There are programming and written tasks. You should find
comments in the code and replace them with your own implementations for programming tasks.
For written tasks, you are required to write a simple report.
4
You need to implement sign up() function of Mp2Client inside mp2.py. This is the command
to create a new seller. The anonymous seller enters seller information (seller id, password) and id
of the plan that the new seller will be subscribed to. You should create a new seller with provided
information in the database.
When you complete the implementation, the software should give output like this:
If a seller with same seller id exists before, or the program gets any kind of exception during
the execution, you should give an error message like this (assuming you have already a seller with
’johnwick’):
You need to implement sign in() function of Mp2Client inside mp2.py. This is the command
for a seller to sign in to the service. The seller types in seller credentials. You should implement
required authentication and session management logic using your seller database.
Remember to increment session count inside subscription table for the seller. Also check whether
the seller is out of sessions or not by looking at max parallel sessions of the seller’s plan.
If session count >= max parallel sessions, then the output should look like this:
For simulating multiple device connections, you can open multiple terminal windows and sign
in from those terminals.
5
4.1.3 Sign Out (6 pts)
>_ sign_out
You need to implement sign out() function of Mp2Client of Mp2Client inside mp2.py. This
is the command for an authenticated seller to sign out from the service. You should implement
required authentication and session management logic using your seller database.
Remember to decrement session count inside sellers table for the seller. Also check whether the
seller’s session count can be at least 0.
When there is an authenticated seller, show plans operation should look like this:
johnwick > show_plans
#|Name|Max Sessions
1|Basic|2
2|Advanced|4
3|Premium|6
When there is an authenticated seller, show subscription operation should look like this:
johnwick > show_subscription
#|Name|Max Sessions
1|Basic|2
6
4.1.6 Change Product Stock (9 pts)
>_ change_stock <product_id> <operation_type> <amount>
You need to implement change stock() function of Mp2Client inside mp2.py. This is the com-
mand to change the stock of a product in stocks table. The operation type can be either ‘add’
or ‘remove’. For instance, if the operation type is ‘add’ and the amount is ‘10’, then you need to
increase the stock. If the operation type is ‘remove’, then you need to decrease the stock.
When there is an authenticated seller, change stock operation should look like this. Imagine the
current stock is 6, then add 2 or remove 5 operations should print and the stock should be 3 at the
end:
johnwick > change_stock productX add 2
OK
johnwick > change_stock productX remove 5
OK
johnwick >
Here is an important rule for changing stock operation: you need to check if the stock amount
after the operation is below 0, when the operation type is ‘remove’. The stock value can not be
below 0.
Imagine the current stock is 6, then remove 12 operations should print an error:
johnwick > change_stock productX remove 12
ERROR: Can not execute the given command.
johnwick >
Assume that johnwick has a plan with id=1 and max parallel sessions=2 and wants to subscribe to a
plan with id=2 and max parallel sessions=4. This operation can be done since new max parallel sessions
is greater than old one. If max parallel sessions values are same, you should also allow that opera-
tion too.
johnwick > show_subscription
#|Name|Max Sessions
1|Basic|2
johnwick > subscribe 2
OK
johnwick > show_subscription
#|Name|Max Sessions
2|Advanced|4
7
However, you must reject the command if new max parallel sessions is smaller than old one.
When we try to change johnwick’s plan with the old plan id, following happens:
You need to implement ship() function of Mp2Client inside mp2.py. This is the command to
save that sellers shipped orders with given order ids, the stock amounts gets decreased on stocks
table. This command does not require sign in at all.
1. You should save ALL new stock amounts to the stocks table, or NONE of them in case
of an error during saving the stocks to the table. The errors could be; non-existing order,
insufficient amount of stocks etc.
2. The order status should be changed to SHIPPED and the shipping datetime should be placed
for the order.
3. The same stock rules apply from the change stock functionality. If a product is not in stock,
then you should halt whole shipment and do nothing.
You need to implement quit() function inside mp2.py. This is the command to quit the appli-
cation. You have to sign out before quitting if there is an authenticated seller. Manage
session count column properly.
You need to implement show cart() function of Mp2Client inside mp2.py. This is the command
to get list of items that the customer decides to buy. This command has a customer id parameter.
You should print all results from the shopping carts table, with the order id of the ‘CREATED’
order. Follow the pattern below while printing columns:
8
johnwick > show_cart customerX
Order Id|Seller Id|Product Id|Amount
orderX|sellerX|productX|3
orderX|sellerX|productY|1
orderX|sellerY|productZ|4
In Mp2, there can only be 1 order with status=’CREATED’ for each customer. This is to
indicate shopping carts on the fly, the customer can add-remove items to this specific order. The
order status will remain as ’CREATED’ until the items in the shopping cart is purchased.
Another important note about customer-side operations is that they can be called any time in
the application. They do not require sign in at all.
You need to implement change cart() function of Mp2Client inside mp2.py. This is the command
to add/remove items to the shopping cart. This command has customer id, product id, seller id,
operation type amount parameters. You should add/remove given amount of items to the shop-
ping cart, for an order with status=’CREATED’. If the item exists on the cart, then you should
increment/decrement it with given value. Suppose there are 2 items for ‘productX’ in the cart:
If the operation is ’add’, then you also need to check if that amount of items exist in the stocks
of the seller. If that is not the case, return STOCK UNAVAILABLE message.
• If there is NO shopping cart defined for the customer before, then you should create a new
order with random uuid and status = ’CREATED’. After this, you can use the new order id.
Do NOT put values to order time and shipping time, since this order is for having
a temporary, on the fly, shopping cart.
• If the new amount will be <= 0, you should remove the entry from the shopping carts table.
Keep the order with status=’CREATED’ for that customer.
9
• If the weight limit (15 kilograms) is reached for the new order, return WEIGHT LIMIT
message. Product weights are in kilograms in the database.
You need to implement purchase cart() function of Mp2Client inside mp2.py. This is the com-
mand to buy items in the shopping cart. This command has a customer id parameter. You should
change the order time & status to ’RECEIVED’ for the customer’s newly ’CREATED’ order and
reduce the seller stocks after this operation.
You also need to check if that amount of items exist in the stocks of the seller. If that is not
the case, return STOCK UNAVAILABLE message and do NOT change any stocks for any of the
items in the shopping cart.
If there is no order with status=’CREATED’ or no entries for such order in shopping cart table,
return EMPTY CART message.
For instance, you can say “I would use the isolation level ... and the access mode ... for ... action,
because ...”. Brief explanations are enough.
10
4.2.2 Transaction Isolation Levels Experiment (10 pts)
For reading the available subscription plans from plans table, submit 2 scripts named mp2 transact
ion reader.py and mp2 transaction writer.py. For both reader and writer, set a transaction
isolation level as READ COMMITTED, REPEATABLE READ and SERIALIZABLE.
Writer Python script should insert a new plan to the plans table and commit changes. Reader
Python script should display plans before writer script commits and after writer script commits.
To imitate sleeps, or long running processes, you can simply put an input(”Hit enter to continue”)
statement before commit & after commit. Similarly you can put the same statement between reads
from plans table.
Run the reader and the writer on separate terminal windows and observe the output for differ-
ent isolation levels. Comment about your observations, you will be graded for both scripts and your
comments.
5 Regulations
1. Implementation: Implement mp2.py only. If you think you need helper functions for the
tasks, please write them to mp2.py. You should NOT update any other files in the
source directory.
2. Response messages: Check messages.py and comments on the functions for response mes-
sages to be used. Different messages for responses are NOT ALLOWED.
3. Submission: Submission will be done via ODTUClass. Please follow the late submission
policy announced at the beginning of the semester. You should put your mp2.py implemen-
tation and your report file (.pdf, .txt) inside a .zip file with following name:
e1234567_mp2.zip
-> mp2.py
-> mp2_transaction_reader.py
-> mp2_transaction_writer.py
-> report PDF
Where you should replace “1234567” with your own student number. Please make sure that
the .zip file doesn’t contain any subdirectories, it should only contain mp2.py and the report
file.
4. Newsgroup: You must follow ODTUClass for discussions and possible clarifications on a
daily basis.
11