Springboot e-commerce
@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;
@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;
@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;
@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;
@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;
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer categoryId;
private String categoryName;
@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>{}
@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
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);
}
}
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
-- 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
Post a Comment