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: ...

Mini-Projects for Docker, Docker Swarm

 Mini-Projects for Docker Challenge 1 Welcome to the Docker challenge, your task is to follow the below steps and complete them successfully. Step 1: Pull latest nginx image Step 2: Create a new bridge 'bridge_sample' Step 3: Run a couple of images (Cont1 and Cont2) and connect these to the new bridge created. Now try to ping from cont1 to cont2 to verify connectivity. Step 4: Stop containers Step 5: Remove network, containers, and images using docker commands Note: Execute "history -w" in the terminal before submitting the scenario. Before moving to Answers try to solve yourself... Let's get started... Step 1: Pull latest nginx image docker pull nginx:latest Step 2: Create a new bridge 'bridge_sample' docker network create bridge_sample Step 3: Run containers and connect them to the new bridge docker run -d --name Cont1 --network bridge_sample nginx:latest docker run -d --name Cont2 --netw...

Building Docker Image with Maven

 Building Docker Image with Maven You are a DevOps Engineer working for ABC Infotech. A client has requested you to build a Docker image using Maven plugin, so that when you package your Maven project. Docker image will be built, and the generated artifacts will be deployed in local git repository  1. Create a Maven java project using CLI with the following configurations - groupId=javaApp  - artifactId maven-docker-image  2. Replace the 'Hello World!' message in the 'App.java' file with 'Hurray! You have successfully built a docker image using Maven' 3. Configure your 'pom.xml' to deploy the generated artifacts in the local git repository to build a docker image 4. The project should be deployed in the local repositary with the following details: - id = maven.repo - name = Maven Artifacts - URL = file:///home/labuser/Desktop/Remote/mavenArtifacts 5. The name of the jar file, repository name af the image should be same as the project artifactId...