#CRUD Project Video Reference: https://www.youtube.com/watch?v=B5tcZoNyqGI
- Github: https://github.com/oktadev/okta-spring-boot-react-crud-example
- Blog: https://developer.okta.com/blog/2022/06/17/simple-crud-react-and-spring-boot
- Please do take note that only the the spring boot set up is followed from the tutorial, react app is created using vite
#Start Up
- Clone Ecomap using git & github
- If using eclipse: after clonning the repo, import the project as maven project
- Install Lombok Jar at https://projectlombok.org/download
- Click and run Lombok Jar
- Right click on project file to go to properties
- Under Java Build Path, Classpath: add external jars. Find and select the Lombok jar file under eclipse ide folder (typically under C:\Users[yourname]... folder)
- Update Maven by right click on project Maven > Update Project
#To run Ecomap locally
- Run Ecomap project using and ide (Eclipse ide recommended), it will be at http://localhost:8080/, this will be the backend
- Run Frontend using VSCode, frontend folder called ecomap-react
- Under ecomap-react in terminal, run: npm install (only needed once to load dependencies), run: npm run dev to start run project locally at http://localhost:4200/
- Create a .env file under ecomap react and add: VITE_API_BASE_URL=http://localhost:8080 (This is to call backend api to the forntend)
#Frontend Backend Flow
- Both the backend(Ecomap) and frontend(ecomap-react) needs to be running in order to call backend api to the frontend
- Axios is a HTTP Client for node.js and browser. I have created axiosClient.js file under ecomap-react
- For reference on how it works please do read login.jsx and signup.jsx under src/views
- Axios will be used to call backend api. For example in Login.jsx, axiosClient.post('/login', payload) will send the paylaod to http://localhost:8080/api (to the backend api)
- http://localhost:8080/api has been initialised at axiosClient.js with .env VITE_API_BASE_URL=http://localhost:8080
- ReactJS Router is used for developing Single Page Web Applications. I have created router.jsx.
- Based on the router, Login.jsx will render at http://localhost:8080/login and Signup.jsx will render at http://localhost:8080/signup, NotFound.jsx will render by default if a path is not defined
- main.jsx is the root react file any imports there will be imported to all view or components files
- index.html is the single web page for reactjs
#Frontend Layout and Authenthication
- React Context is a way to manage state(variable) globally. I have created UserProvider.jsx to help the authentication process for logged in and out users
- UserProvider.jsx will store the authenication token which is initilised at the backend which returned via api if /login api call is successful. Users will be authenticated after this
- In login.jsx: User Context is initialised if axiosClient.post('/login', payload) is successful, the User state can be accessed globally after this (As user session)
- In the router.jsx i have made 2 react components for layout: DefaultLayout.jsx and GuestLayout.jsx
- DefaultLayout.jsx is only rendered when user is logged in (token is not null)
- Child Components under DefaultLayout.jsx will inherit its components, this is create sidebars or navs for later, to create common components for only authethicated users
- GuestLayout.jsx is only rendered when user is not logged in (token is null), same concept as before
#Backend and Database
- Please do research on MVC concept for basic understanding, read on JPA and JDBC for futher understanding on how it works for spring boot
- src/main/java is where the main files are located
- cbse.EcoMap.model are classes which contains logic to connect with database tables, instance variables under these files represent the columns in the database
- cbse.EcoMap.repository is to create functions for for storing/retrieving data from the model/database, example in TeamRepository: Team findByName(String name) to get team by name
- cbse.EcoMap.controller is used for mapping and can be used for api retrieval. For example for the TeamController http://localhost:8080/api/teams is used to retrieve all teams in database teams table. Please do test this out by entering in the url of your browser when the backend is running locally
- I recommened reading about other features in spring boot such as service and component if u have time
- For more info/help on sring boot go to https://www.geeksforgeeks.org/spring-mvc/?ref=lbp
The end, goodluck :P