DEV Community

Katie Liu
Katie Liu

Posted on • Edited on

Contributing to a larger open source project!

For the past two weeks I have started contributing to a new open source project, E-commerce-project-springBoot!

About the Project

E-commerce-project-springBoot

This project is an e-commerce webapp which uses Java Spring Boot and MySQL. 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, 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';
Enter fullscreen mode Exit fullscreen mode

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

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 script to have a UNIQUE constraint on the username column of the customers table

snippet1

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

snippet2

  • In the UserController, add validation to check for existing usernames

snippet3

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

Solution:

UserController snippet1

adminlogin snippet

  • Added an additional check in AdminController for user.getRole() != null since without this, the program was throwing prematurely

AdminController snippet

  • In UserController, 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


I created a PR that contained both fixes:

PR: fixed error msg bug, added validation for duplicate usernames


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, 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 was merged!

Top comments (0)