To run the project:
Step 1: CD to the project root directory cart_api
Step 2: Run ./mvnw spring-boot:run
command
Lesson Coverage:
- Move complex logics in
/carts
to a@Service
class - Adding
User
entity - Use of
UserID
header as a mock authentication header
Firebase SDK has a major shift and no longer support
signInWithEmailAndPassword
from the backendFirebase Admin SDK
using Java.
Refer to the add
and decrement
methods in CartService.java where complex logics is moved into.
Refer to CartController.java where the code mainly focuses on handling http request and response (status).
A custom exception called NotFoundException.java is created to help us identify scenario where the productId
is not found.
Let's refer to our ERD again while creating our User
Entity class.
The SQL Statements required to generate the user
table:
create table user (
id int not null auto_increment,
email varchar(255) not null,
created_at timestamp not null default current_timestamp,
primary key (id)
);
alter table cart add column user_id int;
alter table cart add constraint fk_cart_user foreign key (user_id) references user(id);
If you need to check all foreign keys in the DB, use the following command:
SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = 'cartdb';
public ResponseEntity<List<Cart>> findAll(@RequestHeader("user-id") int userId)
See CartController.java for full details.
We should also implement this for every methods in CartController.java
. This will eventually affect CartService.java
as well. All request to /carts
must contains a user-id
header.
End