Welcome to the EasyShop Ecommerce API repository! This project is a comprehensive Java-based application designed to manage an ecommerce website's functionality.
- Features
- A look into the application
- Check out the website!
- Interesting piece of code
- Error handling
- Login / Register
- Using SpringBoot JWT security, we are able to store users in the database with a hashed password.
- Filtering Products
- Using modules created from the front end, the API interacts with the database to retrieve relevant products.
- Filter by Category
- Filter by Min Price
- Filter by Max Price
- Filter by Color
- Using modules created from the front end, the API interacts with the database to retrieve relevant products.
- Adding to cart
- Logged in users are able to add items to their shopping cart.
- The user cart is linked to their login, so it will be persistent even if they log out.
- Logged in users are able to add items to their shopping cart.
- Order Processing
- Create Orders: Generate new order based on the items in the user's shopping cart...
This is the homepage when a user is not logged, in. They can view the products, but cannot add them to a cart.
After logging in, a user is able to add items to their cart.
The user's current cart.
Creating an order for the user based on items in the cart.
Clearing the cart if a user does not want to checkout.
I chose this piece of code because this post mapping checks the shopping cart to see whether an item is present, and if so it will update the quantity rather than a duplicate item.
@PostMapping("products/{product_id}")
public ShoppingCart addItemToCart(Principal principal, @PathVariable int product_id) {
try
{
// get the currently logged-in username
String userName = principal.getName();
// find database user by userId
User user = userDao.getByUserName(userName);
int userId = user.getId();
ShoppingCart shoppingCart = getCart(principal);
for (Map.Entry<Integer, ShoppingCartItem> item : shoppingCart.getItems().entrySet()) {
if (item.getValue().getProductId() == product_id) {
shoppingCartDao.updateItemInCart(userId, product_id);
return getCart(principal);
}
}
shoppingCartDao.addItemToCart(userId, product_id);
}
catch(Exception e)
{
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Oops... our bad.");
}
return getCart(principal);
}
If there was an error trying to load the cart
.catch(error => {
const data = {
error: 'Load cart failed.',
}
templateBuilder.append('error', data, 'errors')
})
If there was an error trying to clear the cart
.catch(error => {
const data = {
error: 'Empty cart failed.',
}
templateBuilder.append('error', data, 'errors')
})
If there was an error checking out the user's shopping cart
.catch(error => {
const data = {
error: 'Empty cart failed.',
}
templateBuilder.append('error', data, 'errors')
})