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 + Docker Compose + Ansible

 Docker + Docker Compose + Ansible ----------------------------------------------------------------------------------------------------------------------------- Flask Application Deployment using Ansible roles and Docker-Compose 1. Run the "setup.sh" file given in the path to install the required dependencies before starting the challenge. 2. A Flask application named "app.py" is given in the path . 3. Create an Ansible role named "Installation" in the path "/etc/ansible/roles" to install docker-compose. 4. Write an Ansible playbook named "creation.yaml" in the path , to perform the following tasks: 1. Using "file" and "copy" modules, create a Multi-stage "Dockerfile" in the path to  - Dockerize the given Flask application with "python:alpine3.7" as its base image, using the given 'requirements.txt' file.  - Build an image using "postgres" as its base image. 2. Using &q