Skip to main content

Springboot e-commerce

 Springboot e-commerce




User.java - renamed to EcomUser.java
Below is the code snippet how EcomUser.java should be, add constructor and getters and setters also.
Have many-to-many with Role
Have one-to-one with Cart
Have one-to-many with Product

@Entity

public class EcomUser {

        @Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Integer ecomUserId;

private String username;

private String password;


@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)

@JoinTable(name = "UserRole", joinColumns = @JoinColumn(name = "ecomUserId"), inverseJoinColumns = @JoinColumn(name = "roleId"))

private Set<Role> roles;



Role.java - shown below the class, add getter and setters also
Have many-to-many with User

@Entity

public class Role {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Integer roleId;

private String role;


@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "roles")

@JsonIgnore

private Set<EcomUser> ecomUser;



Cart.java
Have one-to-one with User
Have one-to-many with CartProduct

@Entity

public class Cart {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Integer cartId;

private Double totalAmount;

@OneToOne

@JoinColumn(name = "ecomUserId")

private EcomUser ecomUserId;


@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "cart")

private List<CartProduct> cartProducts;



CartProduct.java
Have many-to-one with Cart

@Entity

public class CartProduct {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Integer cpId;

@ManyToOne

@JoinColumn(name="cartId")

private Cart cart;

@ManyToOne

@JoinColumn(name="productId")

private Product product;

private Integer quantity = 1;



Product.java
Have many-to-one with User
Have many-to-one with Category

@Entity

public class Product {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Integer productId;

private String productName;

private Double price;

@ManyToOne(fetch = FetchType.EAGER)

@JoinColumn(name="seller_id")

private EcomUser seller;


@OneToOne

@JoinColumn(name="category_id")

private Category category;



Category.java
Have one-to-many with Product

@Entity

public class Category {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Integer categoryId;

private String categoryName;



All Repository should be:

@Repository

public interface EcomUserRepo extends JpaRepository<EcomUser, Integer>{}


@Repository

public interface CartRepo extends JpaRepository<Cart, Integer>{}


@Repository

public interface CartProductRepo extends JpaRepository<CartProduct, Integer>{}


@Repository

public interface ProductRepo extends JpaRepository<Product, Integer>{}


@Repository

public interface CategoryRepo extends JpaRepository<Category, Integer>{}


Controller class - CartController.java

@RestController

@RequestMapping(value= "/carts")

public class CartController {

    @Autowired

private CartService cartService;


@GetMapping

public String getAllCarts() {

List<Cart> allCarts = cartService.getAllCarts();

return allCarts.toString();

}

@GetMapping("/{id}")

public String getCartById(@PathVariable Integer id) {

return cartService.getCartById(id).toString();

}

@DeleteMapping("/{id}")

public void deleteCart(@PathVariable Integer id) {

cartService.deleteCart(id);

}

}


Service class - CartService.java

@Service

public class CartService {

@Autowired

CartRepo cartRepo;

public List<Cart> getAllCarts(){

return cartRepo.findAll();

}

public Cart getCartById(Integer id) {

return cartRepo.findById(id).orElse(null);

}

public Cart createUser(Cart cart) {

return cartRepo.save(cart);

}


public void deleteCart(Integer id) {

cartRepo.deleteById(id);

}

}


application.properties

sever.port=8000


spring.h2.console.path=/h2

spring.datasource.url=jdbc:h2:mem:testdb

spring.datasource.inititalization-mode=always


spring.h2.console.enabled=true

spring.jpa.defer-datasource-initialization=true


#spring.sql.init.data-locations=classpath:data.sql

#spring.jpa.hibernate.ddl-auto=none

#logging.level.org.springframework.jdbc.datasource.init.ScriptUtils=debug


data.sql

-- Insert Category

insert into Category (category_name) values ('Fashion');

insert into Category (category_name) values ('Electronics');

insert into Category (category_name) values ('Books');

insert into Category (category_name) values ('Groceries');

insert into Category (category_name) values ('Medicines');


-- Insert Roles

INSERT INTO role (role) VALUES ('CONSUMER');

INSERT INTO role (role) VALUES ('SELLER');


-- Insert Users

INSERT INTO ecom_user (username, password) VALUES ('jack', 'pass_word');

INSERT INTO ecom_user (username, password) VALUES ('bob', 'pass_word');

INSERT INTO ecom_user (username, password) VALUES ('apple', 'pass_word');

INSERT INTO ecom_user (username, password) VALUES ('glaxo', 'pass_word');


-- Insert into Cart

insert into Cart (total_amount, ecom_user_id) values (20, 1);

insert into Cart (total_amount, ecom_user_id) values (0, 2);


-- Insert UserRoles

INSERT INTO user_role (ecom_user_id, role_id) VALUES (1, 1);

INSERT INTO user_role (ecom_user_id, role_id) VALUES (2, 1);

INSERT INTO user_role (ecom_user_id, role_id) VALUES (3, 2);

INSERT INTO user_role (ecom_user_id, role_id) VALUES (4, 2);


-- Insert into Product

insert into Product (price, product_name, category_id, seller_id) values (29190, 'Apple iPad 10.2 8th Gen WiFi iOS Tablet', 2, 3);

insert into Product (price, product_name, category_id, seller_id) values (10, 'Crocin pain relief tablet', 5, 4);


-- Insert into CartProduct

insert into Cart_product (cart_id, product_id, quantity) values (1, 2, 2);




---THANK YOU---

Comments

Popular posts from this blog

Kubernetes2

  Challenge 2 ConfigMaps: Step-1: To create a ConfigMap named 'fresco-config' with the key-value pair SERVER_URL= https://www.fresco.me , use the following command: kubectl create configmap fresco-config --from-literal=SERVER_URL=https://www.fresco.me To verify if the ConfigMap is created, use the following command: kubectl get configmap fresco-config Step-2: To create an nginx pod with the environmental variable SERVER_URL_ENV using the ConfigMap created earlier, use the following YAML template: apiVersion: v1 kind: Pod metadata: name: fresco-nginx-pod spec: containers: - name: fresco-nginx-container image: nginx env: - name: SERVER_URL_ENV valueFrom: configMapKeyRef: name: fresco-config key: SERVER_URL Deploy the above file you can name it deployment.yaml and use the command: kubectl apply -f deployment.yaml To test your configuration by executing the following command: ...

Kubernetes1

  Challenge 1 Welcome to the Kubernetes challenge, your task is to follow the below steps and complete them successfully. Environment Setup Check whether docker & minikube are properly installed and configured. Start Minikube and execute this command to sync host docker with minikube docker minikube -p minikube docker-env and eval $(minikube docker-env) Step-1 Create a pod object using kubectl run command with google's sample image: gcr.io/google-samples/kubernetes-bootcamp:v1 and expose it on port 8080, name the pod as firstapp. Check if the pod creation is successful by running the command: kubectl get pod firstapp Step-2 Expose the application to the local VM by creating a Service object of type NodePort. Check if the service is created by running the command: kubectl get svc firstapp Step-3 Create another deployment using a 'YAML' file, create a deployment.yaml file that contains information of the number of replicas and the images to be ...

Docker + Kubernetes + Ansible

  Docker + Kubernetes + Ansible ----------------------------------------------------------------------------------------------------------------------------- Web Application Deployment using Kubernetes and Ansible A Flask application named "application.py" is given in the path Run the given setup.sh file given in the path to install the required dependencies be the challenge. 1. Start Minikube and sync host Docker with Minikube Docker. Note: If you get any errors while starting Minikube, please do try again by running the command to start Minikube. 2. Create a Dockerfile in the path and dockerize the given 'Flask' application as 'webapp-img' using 'python:alpine3.7' as its base image. 3. Create a 'deployment.yml' file in the path to create a Kubernetes deployment object named "web-deployment" with 3 replicas which uses the 'webapp-img' and runs a container named "webapp-container". Add Label "app=webapp" an...