Contributing to a larger open source project!
Tags: opensource, java, springboot, mysql
Published: 2023-11-29 04:01:40

---
series: Contributing to Java Spring Boot project
---

For the past two weeks I have started contributing to a new open source project, [E-commerce-project-springBoot](https://github.com/jaygajera17/E-commerce-project-springBoot)!

# About the Project

[![E-commerce-project-springBoot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zddvfe4h6k2fe2ryrbpm.png)](https://github.com/jaygajera17/E-commerce-project-springBoot)

This project is an e-commerce webapp which uses Java [Spring Boot](https://spring.io/projects/spring-boot) and [MySQL](https://www.mysql.com/). The webapp allows customers to register and buy products and allows admin to add, update, and delete products.

I chose this project because it is still in its early stages, and there are many bugs to fix and many features to be implemented. Nevertheless, it is still a project with a large code base and uses many technologies that I have not yet had the chance to work with.

# Installation

This repo was introduced to me by [Yumei](https://dev.to/wanggithub0), who helped me with the installation. She helped me figure out how to run the `basedata.sql` script to generate the MySQL database. She also helped me set up the project with the IDE, since I am not familiar with using IntelliJ for developing Java applications.

One thing I had to search on google for was how to grant my user access to the the database once the database was created from running the script:

```
GRANT ALL PRIVILEGES ON ecommjava.* TO 'katie'@'localhost';
```

---

I worked on two issues in one PR. This is because, halfway while working on one issue, I discovered another issue which was related.

## Issue 45: [No validation for duplicate username during registration](https://github.com/jaygajera17/E-commerce-project-springBoot/issues/45)

I raised this issue since users were allowed to register new accounts with the same username, resulting in many users with the same username in the database. This is obviously problematic.

## Solution:

- Change the [`basedata.sql`](https://github.com/jaygajera17/E-commerce-project-springBoot/pull/46/files#diff-77ff12c11fd7a7da7bba5ad2c5d646cad89e77e2dfa974dfd1aad7ece519a866) script to have a `UNIQUE` constraint on the username column of the customers table

![snippet1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vhcp06s5so0mnzmdp9l8.png)

  - In `models/User.java`, define a unique constraint on the username field with `@Column(unique = true)`

![snippet2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pehf3gwd0sq64o7wkkwg.png)

- In the [UserController](https://github.com/jaygajera17/E-commerce-project-springBoot/pull/46/files#diff-e2224687d3a27b3379ecf578fca854433d94a66b490cf66d516ae4b1374e765d), add validation to check for existing usernames

![snippet3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nf4xjdtcfpu1kgdzjd6g.png)

After I added validation to check for existing usernames, I decided to trigger an error message to display to the user. There was already code for this that I could reference from from the user login. This is where I discovered the second bug.

There is existing code to trigger an error message when a user or admin login fails; however, when testing the app, no such error message appears and an exception is thrown instead. The following is the issue I raised for this:

## Issue 44: [Error message not displaying when customer/admin login fails](https://github.com/jaygajera17/E-commerce-project-springBoot/issues/44)

## Solution:

- In the [userLogin.jsp](https://github.com/jaygajera17/E-commerce-project-springBoot/pull/46/files#diff-dae11f75d1fa04493ae2947c3d5c29d7dd80c528ebaa5716ba5f3036d32189ac) and [adminlogin.jsp](https://github.com/jaygajera17/E-commerce-project-springBoot/pull/46/files#diff-58f385f1d77302fb23c32763bcb2aa9490eadae6a3e3778b7493c6b9d2e0eda5) views, `{message}` was incorrect and should be `{msg}`. This is because the [UserController](https://github.com/jaygajera17/E-commerce-project-springBoot/blob/master2/JtProject/src/main/java/com/jtspringproject/JtSpringProject/controller/UserController.java) passes to the view a "msg" and not "message".

![UserController snippet1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5p94kifieitswq7llt61.png)

![adminlogin snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y76int9qzghp963s0hh6.png)

- Added an additional check in [AdminController](https://github.com/jaygajera17/E-commerce-project-springBoot/pull/46/files#diff-36d2fcf5ef2a2327c7ee049266da7e8257c4f5e4df37b46b80719474ff7a237c) for `user.getRole() != null` since without this, the program was throwing prematurely

![AdminController snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxa6rwgd67lxydq6m0xo.png)

- In [UserController](https://github.com/jaygajera17/E-commerce-project-springBoot/pull/46/files#diff-e2224687d3a27b3379ecf578fca854433d94a66b490cf66d516ae4b1374e765d), changed the check from `if(u.getUsername().equals(username))` to `if(username.equals(u.getUsername()))` since the former would throw if `u.getUsername()` returns null

![UserController snippet2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b5y1eu6o695l1mz4r6m4.png)

---

### I created a PR that contained both fixes:

## PR: [fixed error msg bug, added validation for duplicate usernames](https://github.com/jaygajera17/E-commerce-project-springBoot/pull/46)

---

I had not worked with MySQL or Java during hacktoberfest, so this was very new and exciting. While debugging these issues I learned a lot, especially about the [Spring Framework](https://spring.io/projects/spring-framework), a popular Java web framework. Here is a few things I learned that were memorable:

- Using Java to connect to MySQL database to insert and retrieve records
- Creating a ModelAndView object to display an html page based on a `.jsp` file (I have been calling this file a view, but it is actually a ModelAndView). Objects could be added to this ModelAndView object and these objects will be displayed on the page based on how the `.jsp` file is written.
- Setting up a MySQL database and tables by running a SQL script

I look forward to getting my PR reviewed and continuing to work more on this project!

_Dec 2 Update_: My [PR](https://github.com/jaygajera17/E-commerce-project-springBoot/pull/46) was merged!