Assessment created by Safwan kher(https://github.com/safwan-kher).
First part of README is by me for application instruction and result description. Second part is the original README by Safwan kher for the project requirements and concepts to implement.
README by Chizuru Yamauchi
- New Project: From IntelliJ IDEA, create a new Maven project.
- Dependencies: Add dependencies for Spring Boot, Spring Security, Spring Mail, Spring Data JPA, PostgreSQL, Thymeleaf, and Bootstrap 5 on pom.xml file.
- On terminal, git init -> git remote add origin git@URL -> git remote -v -> git pull origin main -> git branch -M main -> git add. -> git commit -m "----" -> git push origin main
-
On terminal, sudo -u postgres psql -> CREATE DATABASE database_name; -> \c database_name -> \q
-
Application Properties: Configure the PostgreSQL database connection in the
application.properties
file(and put the file name in .gitignore to secure the sensitive information.)
There is no user so far in the database and we have to sign up first.
After filling the sign-up form and click 'Sign Up', the user get the verification email
The user will get verification Email: Click the url to verify the email.
After clicking the link, the user will be verified('enabled' becomes 'true' from 'false' on database) and the user can log in with the username and password.
on database after verification:
on the web application:
Newly log in with a new username and a new password as a user(role).
Email Testing: Test passed.
Home as a user :logged in as a user (username = 'naruto') *Updated
Click 'Browser Products' and the user(naruto) can see product lists :(Jump to Product List and see all the products)
*Because naruto logged in as a role_user now, there is no feature of 'Add New Product'
Home as an admin
Click 'Admin Home' and jump to Admin Home Page and see all the option admin could do
Click Product List and see all the products. It is empty now because there is no product yet. But since the user logged in as an admin, the admin can add a new product with 'Add New Product' button.
Click Add Product and add a new product
Click 'Save' the product then the product list will show the product
Click 'View' to see the product detail
Click 'Convert Price' to go to the convert currency page to convert the price with product id number to different currencies
On actual URL, you can see the converted price
Click 'Edit' to edit the product
Assign role to the user as admin
After assigning the role, you can see the modification on the database
The user ('user_id 6' = demoChizuru) is now assigned as an admin(and also still as a user) on database. 'role_id 5' = ROLE_ADMIN)'
demoChizuru can access to 'Admin Home' and 'User Home' since demoChizuru got two roles(Admin and User) now (*Updated)
demoChizuru can go to 'Admin Home' and do the admin tasks
Click 'Manage Users' and see the user list
Click 'View' to see the user detail
demoChizuru can still go to 'User Home' and can view as a normal user(*Updated)
demoChizuru can view her own profile
When you want to log out click logout, and you will be logged out, then you can log in again as a user or admin.
README and instruction by Safwan kher.
In this assignment, you will build an online shop application using Java Spring Boot, Spring Security, JPA, Hibernate, and Thymeleaf. The application will allow users to sign up, log in, browse products, add products to their cart, and convert prices between different currencies using the ExchangeRate API.
-
User Management:
- Users should be able to sign up and log in.
- Users should have roles (
ROLE_USER
,ROLE_ADMIN
). - Admins should be able to assign the
ROLE_ADMIN
to other users.
-
Product Management:
- Admins should be able to add, update, and delete products.
- Users should be able to view products and add them to their cart.
-
Currency Conversion:
- Users should be able to convert product prices between different currencies using the ExchangeRate API.
-
Security:
- Implement authentication and authorization using Spring Security.
- Passwords should be securely stored using
BCryptPasswordEncoder
.
-
Database:
- Use JPA and Hibernate to manage entities and relationships.
- Use PostgreSQL as the database.
-
User Interface:
- Use Thymeleaf to create dynamic HTML templates for the user interface.
-
Spring Boot:
- Create a Spring Boot application with the necessary dependencies.
- Configure application properties.
-
Spring Security:
- Implement authentication and authorization.
- Use role-based access control.
-
JPA and Hibernate:
- Define entities and relationships.
- Use repositories to perform CRUD operations.
-
Thymeleaf:
- Create dynamic HTML templates.
- Use Thymeleaf expressions to populate content.
-
Controllers:
- Implement controllers to handle HTTP requests and return views.
- Handle form submissions.
-
Service Layer:
- Implement business logic in the service layer.
- Interact with repositories to perform database operations.
-
RESTful API Integration:
- Use
RestTemplate
to make HTTP requests to the ExchangeRate API.
- Use
-
Bidirectional Relationships:
- Implement bidirectional relationships between entities.
-
Error Handling:
- Implement basic error handling.
-
Create a new Spring Boot project with the following dependencies:
- Spring Web
- Spring Security
- Spring Data JPA
- PostgreSQL Driver
- Thymeleaf
- Spring Boot DevTools
-
Configure the
application.properties
file with the necessary settings:# Server Configuration server.port=8080 # Database Configuration spring.datasource.url=jdbc:postgresql://localhost:5432/onlineshop spring.datasource.username=your_username spring.datasource.password=your_password spring.datasource.driver-class-name=org.postgresql.Driver # JPA/Hibernate properties spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true # Email Configuration spring.mail.host=smtp.gmail.com spring.mail.port=587 spring.mail.username[email protected] spring.mail.password=your_email_password spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true # ExchangeRate-API Key exchangerate.api.key=your_api_key
-
Entities:
- Create
User
andRole
entities with a many-to-many relationship. - Create
VerificationToken
entity for email verification.
- Create
-
Repositories:
- Create
UserRepository
,RoleRepository
, andVerificationTokenRepository
.
- Create
-
Services:
- Implement
UserService
to handle user-related operations. - Implement email verification logic.
- Implement
-
Controllers:
- Implement
UserController
to handle user-related requests (signup, login, assign admin role, verify email).
- Implement
-
Security Configuration:
- Implement
SecurityConfig
to configure authentication and authorization.
- Implement
-
Entities:
- Create
Product
entity with fields likeid
,name
,description
,price
, andcurrency
.
- Create
-
Repositories:
- Create
ProductRepository
.
- Create
-
Services:
- Implement
ProductService
to handle product-related operations.
- Implement
-
Controllers:
- Implement
ProductController
to handle product-related requests (add, update, delete, view products).
- Implement
-
Service:
- Implement
CurrencyConverterService
to interact with the ExchangeRate API.
- Implement
-
Controller:
- Implement
CurrencyConverterController
to handle currency conversion requests.
- Implement
-
Templates:
- Create
currency-converter.html
to allow users to convert product prices between different currencies.
- Create
-
Templates:
- Create
login.html
,signup.html
,home.html
,admin-home.html
,product-list.html
,product-form.html
, andcurrency-converter.html
using Thymeleaf.
- Create
-
Dynamic Content:
- Use Thymeleaf expressions to dynamically populate content in the templates.
-
Sign Up and Log In:
- Test user signup and login functionality.
- Verify email and assign admin role.
-
Product Management:
- Test adding, updating, deleting, and viewing products.
-
Currency Conversion:
- Test converting product prices between different currencies.
-
Security:
- Test role-based access control.
Submit the following:
- Source code of the project.
- A README file with instructions on how to run the application.
- Screenshots or a video demonstrating the functionality of the application.
-
Spring Boot:
- Spring Boot simplifies the setup and development of Spring applications by providing default configurations and embedded servers. It allows you to create stand-alone, production-grade Spring-based applications with minimal configuration.
-
Spring Security:
- Spring Security provides authentication and authorization capabilities. It allows you to secure your application by implementing role-based access control and password encoding.
-
JPA and Hibernate:
- JPA (Java Persistence API) is a specification for object-relational mapping (ORM) in Java. Hibernate is an implementation of JPA. They allow you to map Java objects to database tables and perform CRUD operations.
-
Thymeleaf:
- Thymeleaf is a modern server-side Java template engine for web and standalone environments. It allows you to create dynamic HTML templates and populate them with data from your application.
-
Controllers:
- Controllers handle HTTP requests, interact with the service layer, and return views. They follow the Model-View-Controller (MVC) pattern.
-
Service Layer:
- The service layer contains business logic and interacts with repositories to perform database operations. It encapsulates the core functionality of the application.
-
RESTful API Integration:
- RESTful APIs allow you to interact with external services. In this assignment, you will use the ExchangeRate API to fetch conversion rates and supported currencies.
-
Bidirectional Relationships:
- Bidirectional relationships between entities allow you to navigate the relationship from both sides. For example, a
User
can have multipleRoles
, and aRole
can have multipleUsers
.
- Bidirectional relationships between entities allow you to navigate the relationship from both sides. For example, a
-
Error Handling:
- Error handling ensures that your application can gracefully handle unexpected situations. It includes throwing exceptions and providing meaningful error messages to the user.
By completing this assignment, you will gain hands-on experience with these concepts and learn how to build a secure, user-friendly online shop application with currency conversion capabilities.