Challenge myself, do more features in a java spring boot project
Tags: opensource, java, programming, beginners
Published: 2023-12-11 21:38:48

After adding the [userHome](https://dev.to/wanggithub0/try-to-add-new-feather-to-a-java-spring-boot-project-lge), I wanna challenge myself more.

After the user login, he can see all the products. He should can add the products into his cart, and click the button to get all his carts. So I tried to add the link cart button and show the carts page.

I tried to get cart at first, but I found the class define in the model is not same with the one created in database. In model, the author only defines the Cart and it has a ManyToMany relationship to products.

 ```
@Entity(name="CART")
public class Cart {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @ManyToOne
    @JoinColumn(name="customer_id")
    private User customer;

    @ManyToMany
    @JoinTable(
            joinColumns = @JoinColumn(name = "cart_id"),
            inverseJoinColumns = @JoinColumn(name = "product_id")
    )
    private List<Product> products;
```

But in the database, the CART table only has two columns "id" and "customer_id". I also found another table "CART_PRODUCT" which record the "cart_id" and "product_id". I think the database table structure is more reasonable, so I have to change all the model, dao, controller for Cart and add them for CartProduct.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilxusj3lqk2kzik8jbhy.png)


So I tried to revise Cart class which only contains id and customer and add CartProduct class which contains id, cart and product, also add the gettings and settings:
```
@Entity(name="CART_PRODUCT")
public class CartProduct {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @ManyToOne
    @JoinColumn(name="cart_id")
    private Cart cart;

    @ManyToOne
    @JoinTable(name="product_id")
    private Product product;


    public CartProduct() {
        product = null;
    }
    public CartProduct(Cart cart, Product product) {
        this.cart=cart;
        this.product = product;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Cart getCart() {
        return cart;
    }

    public void setCart(Cart cart) {
        this.cart = cart;
    }
    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }
}
```

I also add the cartProductDao where I can connect to the database to get, update, delete cartProduct:
```
@Repository
public class cartProductDao {
    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sf) {
        this.sessionFactory = sf;
    }

    @Transactional
    public CartProduct addCartProduct(CartProduct cartProduct) {
        this.sessionFactory.getCurrentSession().save(cartProduct);
        return cartProduct;
    }

    @Transactional
    public List<CartProduct> getCartProducts() {
        return this.sessionFactory.getCurrentSession().createQuery("from CART_PRODUCT ").list();
    }

    @Transactional
    public List<Product> getProductByCartID(Integer cart_id) {
        String sql = "SELECT product_id FROM cart_product WHERE cart_id = :cart_id";
        List<Integer> productIds = this.sessionFactory.getCurrentSession()
                .createNativeQuery(sql)
                .setParameter("cart_id", cart_id)
                .list();

        sql = "SELECT * FROM product WHERE id IN (:product_ids)";
        return this.sessionFactory.getCurrentSession()
                .createNativeQuery(sql, Product.class)
                .setParameterList("product_ids", productIds)
                .list();
    }

    @Transactional
    public void updateCartProduct(CartProduct cartProduct) {
        this.sessionFactory.getCurrentSession().update(cartProduct);
    }

    @Transactional
    public void deleteCartProduct(CartProduct cartProduct) {
        this.sessionFactory.getCurrentSession().delete(cartProduct);
    }
}
```
At last, I just created a [PR](https://github.com/jaygajera17/E-commerce-project-springBoot/pull/56) to add cartProduct and revise cart file.

During this process, I learned a bunch about connecting Spring Boot, which is like a cool tool for making computer programs, with MySQL, a database thingy. It was kinda tricky because the class the person who made the tools used before was not exactly the same as the database, so I had to do a lot of extra stuff to make them work together. I was trying to show a page with carts, but it got a bit complicated. I didn't finish it in time because of all the extra work, but I think I can do more later when I have more time. It was a bit challenging, but I'm excited to learn even more!