├── .DS_Store ├── Ecommerce ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── ecommerce │ │ │ │ └── Ecommerce │ │ │ │ ├── exceptions │ │ │ │ ├── OrderException.java │ │ │ │ ├── LoginException.java │ │ │ │ ├── AdminException.java │ │ │ │ ├── CustomerException.java │ │ │ │ ├── InvalidKeyException.java │ │ │ │ ├── ProductNotFoundException.java │ │ │ │ ├── InsufficientQuantityException.java │ │ │ │ ├── MyErrorDetails.java │ │ │ │ └── GlobalExceptionHandler.java │ │ │ │ ├── models │ │ │ │ ├── AdminLoginDTO.java │ │ │ │ ├── CartDTO.java │ │ │ │ ├── Admin.java │ │ │ │ ├── ProductDTO.java │ │ │ │ ├── Product.java │ │ │ │ ├── Orders.java │ │ │ │ ├── CustomerLoginDTO.java │ │ │ │ ├── CurrentAdminSession.java │ │ │ │ ├── CurrentCustomerSession.java │ │ │ │ └── Customer.java │ │ │ │ ├── repositories │ │ │ │ ├── AdminDAO.java │ │ │ │ ├── CustomerDAO.java │ │ │ │ ├── AdminSessionDAO.java │ │ │ │ ├── CustomerSessionDao.java │ │ │ │ ├── OrderRepositoryDAO.java │ │ │ │ └── ProductRepositoryDAO.java │ │ │ │ ├── services │ │ │ │ ├── AdminLoginService.java │ │ │ │ ├── CustomerLoginService.java │ │ │ │ ├── CustomerService.java │ │ │ │ ├── AdminService.java │ │ │ │ ├── OrderService.java │ │ │ │ ├── ProductService.java │ │ │ │ ├── AdminLoginServiceImpl.java │ │ │ │ ├── CustomerLoginServiceImpl.java │ │ │ │ ├── CustomerServiceImpl.java │ │ │ │ ├── AdminServiceImpl.java │ │ │ │ ├── OrderServiceImpl.java │ │ │ │ └── ProductServiceImpl.java │ │ │ │ ├── EcommerceApplication.java │ │ │ │ └── controllers │ │ │ │ ├── AdminLoginController.java │ │ │ │ ├── CustomerLoginController.java │ │ │ │ ├── CustomerController.java │ │ │ │ ├── OrderController.java │ │ │ │ ├── AdminController.java │ │ │ │ └── ProductController.java │ │ └── resources │ │ │ └── application.properties │ └── test │ │ └── java │ │ └── com │ │ └── ecommerce │ │ └── Ecommerce │ │ └── EcommerceApplicationTests.java ├── .gitignore ├── pom.xml ├── mvnw.cmd └── mvnw └── README.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frozen-dev71/E-commerce-Backend/main/.DS_Store -------------------------------------------------------------------------------- /Ecommerce/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frozen-dev71/E-commerce-Backend/main/Ecommerce/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /Ecommerce/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.6/apache-maven-3.8.6-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar 3 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/exceptions/OrderException.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.exceptions; 2 | 3 | public class OrderException extends Exception{ 4 | public OrderException() { 5 | } 6 | 7 | public OrderException(String message) { 8 | super(message); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/exceptions/LoginException.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.exceptions; 2 | 3 | public class LoginException extends Exception{ 4 | 5 | public LoginException() { 6 | } 7 | 8 | public LoginException(String message) { 9 | super(message); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/exceptions/AdminException.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.exceptions; 2 | 3 | public class AdminException extends Exception{ 4 | 5 | public AdminException() { 6 | 7 | } 8 | 9 | public AdminException(String message) { 10 | super(message); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/exceptions/CustomerException.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.exceptions; 2 | 3 | public class CustomerException extends Exception { 4 | public CustomerException() { 5 | 6 | } 7 | 8 | public CustomerException(String message) { 9 | super(message); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/models/AdminLoginDTO.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.models; 2 | 3 | import lombok.*; 4 | 5 | @Getter 6 | @Setter 7 | @ToString 8 | @NoArgsConstructor 9 | @AllArgsConstructor 10 | public class AdminLoginDTO { 11 | private Integer adminId; 12 | private String password; 13 | } 14 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/exceptions/InvalidKeyException.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.exceptions; 2 | 3 | public class InvalidKeyException extends Exception{ 4 | 5 | public InvalidKeyException() { 6 | } 7 | 8 | public InvalidKeyException(String errmsg) { 9 | super(errmsg); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Ecommerce/src/test/java/com/ecommerce/Ecommerce/EcommerceApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class EcommerceApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/exceptions/ProductNotFoundException.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.exceptions; 2 | 3 | public class ProductNotFoundException extends Exception{ 4 | public ProductNotFoundException() { 5 | } 6 | 7 | public ProductNotFoundException(String message) { 8 | super(message); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/exceptions/InsufficientQuantityException.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.exceptions; 2 | 3 | public class InsufficientQuantityException extends Exception{ 4 | public InsufficientQuantityException() { 5 | } 6 | 7 | public InsufficientQuantityException(String message) { 8 | super(message); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/models/CartDTO.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.models; 2 | 3 | import lombok.*; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | @Getter 9 | @Setter 10 | @ToString 11 | @NoArgsConstructor 12 | @AllArgsConstructor 13 | public class CartDTO { 14 | private List list = new ArrayList<>(); 15 | 16 | private Double totalBill; 17 | } 18 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/repositories/AdminDAO.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.repositories; 2 | 3 | import com.ecommerce.Ecommerce.models.Admin; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface AdminDAO extends JpaRepository { 9 | Admin findByAdminId(Integer adminId); 10 | } 11 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/repositories/CustomerDAO.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.repositories; 2 | 3 | import com.ecommerce.Ecommerce.models.Customer; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface CustomerDAO extends JpaRepository { 9 | public Customer findByEmail(String email); 10 | } 11 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/services/AdminLoginService.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.services; 2 | 3 | import com.ecommerce.Ecommerce.exceptions.LoginException; 4 | import com.ecommerce.Ecommerce.models.AdminLoginDTO; 5 | 6 | public interface AdminLoginService { 7 | public String logIntoAccount(AdminLoginDTO dto) throws LoginException; 8 | 9 | public String logOutFromAccount(String key) throws LoginException; 10 | } 11 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/services/CustomerLoginService.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.services; 2 | 3 | import com.ecommerce.Ecommerce.exceptions.LoginException; 4 | import com.ecommerce.Ecommerce.models.CustomerLoginDTO; 5 | 6 | public interface CustomerLoginService { 7 | 8 | public String logIntoAccount(CustomerLoginDTO dto) throws LoginException; 9 | public String logOutFromAccount(String key) throws LoginException; 10 | } 11 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/repositories/AdminSessionDAO.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.repositories; 2 | 3 | import com.ecommerce.Ecommerce.models.CurrentAdminSession; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface AdminSessionDAO extends JpaRepository { 9 | public CurrentAdminSession findByUuid(String uuid); 10 | } 11 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/repositories/CustomerSessionDao.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.repositories; 2 | 3 | import com.ecommerce.Ecommerce.models.CurrentCustomerSession; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface CustomerSessionDao extends JpaRepository { 9 | public CurrentCustomerSession findByUuid(String uuid); 10 | } 11 | -------------------------------------------------------------------------------- /Ecommerce/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | #changing the server port 3 | server.port=8880 4 | 5 | #db specific properties 6 | spring.datasource.url=jdbc:mysql://localhost:3306/ecommercedb 7 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 8 | spring.datasource.username=root 9 | spring.datasource.password=my-secret-pw 10 | 11 | #ORM s/w specific properties 12 | spring.jpa.hibernate.ddl-auto=update 13 | spring.jpa.show-sql=true 14 | 15 | spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/EcommerceApplication.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 6 | 7 | @SpringBootApplication 8 | @EnableSwagger2 9 | public class EcommerceApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(EcommerceApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Ecommerce/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/repositories/OrderRepositoryDAO.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.repositories; 2 | 3 | import com.ecommerce.Ecommerce.models.Orders; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import java.util.List; 8 | 9 | @Repository 10 | public interface OrderRepositoryDAO extends JpaRepository { 11 | public List findBySessionId(String CustomerKey); 12 | public void deleteBySessionId(String customerKey); 13 | } 14 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/repositories/ProductRepositoryDAO.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.repositories; 2 | 3 | import com.ecommerce.Ecommerce.models.Product; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import java.util.List; 8 | 9 | @Repository 10 | public interface ProductRepositoryDAO extends JpaRepository { 11 | public List findByProductName(String name); 12 | 13 | public List findByProductId(String type); 14 | } 15 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/services/CustomerService.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.services; 2 | 3 | import com.ecommerce.Ecommerce.exceptions.AdminException; 4 | import com.ecommerce.Ecommerce.exceptions.CustomerException; 5 | import com.ecommerce.Ecommerce.models.Customer; 6 | 7 | import java.util.List; 8 | 9 | public interface CustomerService { 10 | public Customer createCustomer(Customer customer) throws CustomerException; 11 | 12 | public Customer updateCustomer(Customer customer, String key) throws CustomerException; 13 | 14 | public String deleteCustomerById(Integer customerId) throws CustomerException; 15 | 16 | public List getAllCustomersDetails(String key) throws CustomerException, AdminException; 17 | } 18 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/models/Admin.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.models; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.Id; 5 | import javax.validation.constraints.Min; 6 | import javax.validation.constraints.NotEmpty; 7 | import javax.validation.constraints.Size; 8 | import lombok.Data; 9 | import lombok.NoArgsConstructor; 10 | import lombok.ToString; 11 | 12 | @Entity 13 | @Data 14 | @NoArgsConstructor 15 | @ToString 16 | public class Admin { 17 | @Id 18 | @Min(value = 1, message = "Admin Id must be always greater than 0 !!") 19 | private Integer adminId; 20 | 21 | @NotEmpty(message = "password must not Empty or null!!") 22 | @Size(min = 5, max = 20, message = "Admin password should contain min 3 and max 20 chars!!") 23 | private String adminPassword; 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/services/AdminService.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.services; 2 | 3 | import com.ecommerce.Ecommerce.exceptions.AdminException; 4 | import com.ecommerce.Ecommerce.exceptions.CustomerException; 5 | import com.ecommerce.Ecommerce.models.Admin; 6 | import com.ecommerce.Ecommerce.models.Customer; 7 | 8 | import java.util.List; 9 | 10 | public interface AdminService { 11 | public Customer createCustomer(Customer customer) throws CustomerException; 12 | 13 | public Admin createAdmin(Admin admin) throws AdminException; 14 | 15 | public Admin updateAdmin(Admin admin, String key) throws AdminException; 16 | 17 | public Customer updateCustomer(Customer customer, String key) throws CustomerException; 18 | 19 | public String deleteAdminById(Integer adminId) throws AdminException; 20 | 21 | public List getAllAdminDetails() throws AdminException; 22 | } 23 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/models/ProductDTO.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.models; 2 | 3 | import javax.validation.constraints.Min; 4 | import javax.validation.constraints.NotBlank; 5 | import javax.validation.constraints.NotNull; 6 | import javax.validation.constraints.Pattern; 7 | import lombok.*; 8 | 9 | @Getter 10 | @Setter 11 | @ToString 12 | @NoArgsConstructor 13 | @AllArgsConstructor 14 | public class ProductDTO { 15 | 16 | @NotBlank(message = "Product Name is Required.") 17 | @Pattern(regexp = "^[A-Za-z]+$") 18 | private String productName; 19 | 20 | private String description; 21 | 22 | @NotNull(message = "Stock value can not be null") 23 | @Min(value = 0, message = "Stock value must be greater than or equal to 0") 24 | private Integer stock; 25 | 26 | @NotNull 27 | @Min(value = 1, message = "Cost must be greater than or equal to 1") 28 | private Double cost; 29 | } 30 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/models/Product.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.models; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.GenerationType; 6 | import javax.persistence.Id; 7 | import javax.validation.constraints.Min; 8 | import javax.validation.constraints.NotNull; 9 | import lombok.*; 10 | 11 | @Entity 12 | @Getter 13 | @Setter 14 | @ToString 15 | @NoArgsConstructor 16 | @AllArgsConstructor 17 | public class Product { 18 | @Id 19 | @GeneratedValue(strategy = GenerationType.AUTO) 20 | private Integer productId; 21 | 22 | @NotNull(message = "Product Name is Required.") 23 | private String productName; 24 | 25 | private String description; 26 | 27 | @NotNull(message = "Stock value can not be null") 28 | @Min(value = 0, message = "Stock value must be greater than equal to 0") 29 | private Integer stock; 30 | 31 | @NotNull 32 | @Min(value = 1, message = "Cost must be greater than or equal to 1") 33 | private Double cost; 34 | } 35 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/models/Orders.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.models; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.GenerationType; 6 | import javax.persistence.Id; 7 | import javax.validation.constraints.Min; 8 | import javax.validation.constraints.NotNull; 9 | import lombok.*; 10 | 11 | import java.time.LocalDateTime; 12 | 13 | @Entity 14 | @Getter 15 | @Setter 16 | @ToString 17 | @NoArgsConstructor 18 | @AllArgsConstructor 19 | public class Orders { 20 | @Id 21 | @GeneratedValue(strategy = GenerationType.AUTO) 22 | private Integer orderId; 23 | 24 | @NotNull 25 | private LocalDateTime dateAndTime; 26 | 27 | @NotNull(message = "Quantity is required.") 28 | @Min(value = 1, message = "Minimum quantity of Product must be greater than 0") 29 | private Integer quantity; 30 | 31 | private Double totalCost; 32 | 33 | private String productName; 34 | 35 | @NotNull 36 | private Integer productId; 37 | 38 | private String sessionId; 39 | } 40 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/services/OrderService.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.services; 2 | 3 | import com.ecommerce.Ecommerce.exceptions.CustomerException; 4 | import com.ecommerce.Ecommerce.exceptions.InsufficientQuantityException; 5 | import com.ecommerce.Ecommerce.exceptions.OrderException; 6 | import com.ecommerce.Ecommerce.exceptions.ProductNotFoundException; 7 | import com.ecommerce.Ecommerce.models.CartDTO; 8 | import com.ecommerce.Ecommerce.models.Orders; 9 | 10 | public interface OrderService { 11 | public Orders buyProductByProductId(String sessionId, Integer productId, String productName, Integer quantity) 12 | throws CustomerException, ProductNotFoundException, InsufficientQuantityException; 13 | 14 | public CartDTO visitYourCart(String customerKey) throws CustomerException, OrderException; 15 | 16 | public String paymentAmount(String customerKey, Double amount) throws CustomerException, OrderException; 17 | 18 | public Orders deleteProductByOrderId(String customerKey, Integer orderId) throws CustomerException, OrderException; 19 | } 20 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/services/ProductService.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.services; 2 | 3 | import com.ecommerce.Ecommerce.exceptions.ProductNotFoundException; 4 | import com.ecommerce.Ecommerce.models.Product; 5 | 6 | import java.util.List; 7 | 8 | public interface ProductService { 9 | public Product addProduct(Product product) throws ProductNotFoundException; 10 | 11 | public Product updateProduct(Product product) throws ProductNotFoundException; 12 | 13 | public List getAllProducts() throws ProductNotFoundException; 14 | 15 | public Product deleteProductById(Integer productId) throws ProductNotFoundException; 16 | 17 | public Product viewProductById(Integer productId) throws ProductNotFoundException; 18 | 19 | public List viewProductByName(String name) throws ProductNotFoundException; 20 | 21 | public Product changeQuantityOfProductByProductId(Integer productId, Integer newQuantity) 22 | throws ProductNotFoundException; 23 | 24 | public Product changePriceOfProductByProductId(Integer productId, Double newPrice) throws ProductNotFoundException; 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/models/CustomerLoginDTO.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.models; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Getter; 5 | import lombok.NoArgsConstructor; 6 | import lombok.Setter; 7 | 8 | @Getter 9 | @Setter 10 | @NoArgsConstructor 11 | @AllArgsConstructor 12 | public class CustomerLoginDTO { 13 | private String mobileNum; 14 | private String password; 15 | private String email; 16 | 17 | public String getEmail() { 18 | return email; 19 | } 20 | 21 | public void setEmail(String email) { 22 | email = email; 23 | } 24 | 25 | public String getMobileNo() { 26 | return mobileNum; 27 | } 28 | 29 | public void setMobileNo(String mobileNum) { 30 | this.mobileNum = mobileNum; 31 | } 32 | 33 | public String getPassword() { 34 | return password; 35 | } 36 | 37 | public void setPassword(String password) { 38 | this.password = password; 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return "LoginDTO [mobileNo=" + mobileNum + ", password=" + password + "]"; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/models/CurrentAdminSession.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.models; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.Id; 6 | import lombok.*; 7 | 8 | import java.time.LocalDateTime; 9 | 10 | @Entity 11 | @Getter 12 | @Setter 13 | @ToString 14 | @NoArgsConstructor 15 | @AllArgsConstructor 16 | public class CurrentAdminSession { 17 | 18 | @Id 19 | @Column(unique = true) 20 | private Integer userId; 21 | 22 | private String uuid; 23 | 24 | private LocalDateTime localDateTime; 25 | 26 | public Integer getUserId() { 27 | return userId; 28 | } 29 | 30 | public void setUserId(Integer userId) { 31 | this.userId = userId; 32 | } 33 | 34 | public String getUuid() { 35 | return uuid; 36 | } 37 | 38 | public void setUuid(String uuid) { 39 | this.uuid = uuid; 40 | } 41 | 42 | public LocalDateTime getLocalDateTime() { 43 | return localDateTime; 44 | } 45 | 46 | public void setLocalDateTime(LocalDateTime localDateTime) { 47 | this.localDateTime = localDateTime; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/models/CurrentCustomerSession.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.models; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.Id; 6 | import lombok.*; 7 | 8 | import java.time.LocalDateTime; 9 | 10 | @Entity 11 | @Getter 12 | @Setter 13 | @ToString 14 | @NoArgsConstructor 15 | @AllArgsConstructor 16 | public class CurrentCustomerSession { 17 | 18 | @Id 19 | @Column(unique = true) 20 | private Integer userId; 21 | 22 | private String uuid; 23 | 24 | private LocalDateTime localDateTime; 25 | 26 | public Integer getUserId() { 27 | return userId; 28 | } 29 | 30 | public void setUserId(Integer userId) { 31 | this.userId = userId; 32 | } 33 | 34 | public String getUuid() { 35 | return uuid; 36 | } 37 | 38 | public void setUuid(String uuid) { 39 | this.uuid = uuid; 40 | } 41 | 42 | public LocalDateTime getLocalDateTime() { 43 | return localDateTime; 44 | } 45 | 46 | public void setLocalDateTime(LocalDateTime localDateTime) { 47 | this.localDateTime = localDateTime; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/controllers/AdminLoginController.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.controllers; 2 | 3 | import com.ecommerce.Ecommerce.exceptions.LoginException; 4 | import com.ecommerce.Ecommerce.models.AdminLoginDTO; 5 | import com.ecommerce.Ecommerce.services.AdminLoginService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | @RestController 12 | @CrossOrigin("*") 13 | @RequestMapping("/admin") 14 | public class AdminLoginController { 15 | @Autowired 16 | private AdminLoginService adminLogin; 17 | 18 | @PostMapping("/login") 19 | public ResponseEntity logInAdmin(@RequestBody AdminLoginDTO dto) throws LoginException { 20 | String result = adminLogin.logIntoAccount(dto); 21 | return new ResponseEntity(result, HttpStatus.OK); 22 | } 23 | 24 | @PostMapping("/logout") 25 | public String logoutAdmin(@RequestParam(required = false) String key) throws LoginException { 26 | return adminLogin.logOutFromAccount(key); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/exceptions/MyErrorDetails.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.exceptions; 2 | 3 | import java.time.LocalDateTime; 4 | 5 | public class MyErrorDetails { 6 | private LocalDateTime timestamp; 7 | private String message; 8 | private String details; 9 | 10 | public MyErrorDetails() { 11 | // TODO Auto-generated constructor stub 12 | } 13 | 14 | public MyErrorDetails(LocalDateTime timestamp, String message, String details) { 15 | super(); 16 | this.timestamp = timestamp; 17 | this.message = message; 18 | this.details = details; 19 | } 20 | 21 | public LocalDateTime getTimestamp() { 22 | return timestamp; 23 | } 24 | 25 | public void setTimestamp(LocalDateTime timestamp) { 26 | this.timestamp = timestamp; 27 | } 28 | 29 | public String getMessage() { 30 | return message; 31 | } 32 | 33 | public void setMessage(String message) { 34 | this.message = message; 35 | } 36 | 37 | public String getDetails() { 38 | return details; 39 | } 40 | 41 | public void setDetails(String details) { 42 | this.details = details; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/controllers/CustomerLoginController.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.controllers; 2 | 3 | import com.ecommerce.Ecommerce.exceptions.LoginException; 4 | import com.ecommerce.Ecommerce.models.CustomerLoginDTO; 5 | import com.ecommerce.Ecommerce.services.CustomerLoginService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | @RestController 12 | @CrossOrigin("*") 13 | @RequestMapping("/customer") 14 | public class CustomerLoginController { 15 | 16 | @Autowired 17 | private CustomerLoginService customerLogin; 18 | 19 | @PostMapping("/login") 20 | public ResponseEntity logInCustomer(@RequestBody CustomerLoginDTO dto) throws LoginException { 21 | 22 | String result = customerLogin.logIntoAccount(dto); 23 | return new ResponseEntity(result, HttpStatus.OK); 24 | } 25 | 26 | @PostMapping("/logout") 27 | public String logoutCustomer(@RequestParam(required = false) String key) throws LoginException { 28 | return customerLogin.logOutFromAccount(key); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/controllers/CustomerController.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.controllers; 2 | 3 | import com.ecommerce.Ecommerce.exceptions.CustomerException; 4 | import com.ecommerce.Ecommerce.models.Customer; 5 | import com.ecommerce.Ecommerce.services.CustomerService; 6 | import javax.validation.Valid; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.http.HttpStatus; 9 | import org.springframework.http.ResponseEntity; 10 | import org.springframework.web.bind.annotation.*; 11 | 12 | @RestController 13 | public class CustomerController { 14 | @Autowired 15 | private CustomerService customerService; 16 | 17 | @PostMapping("/customers") 18 | public ResponseEntity registerCustomer(@Valid @RequestBody Customer customer) throws CustomerException { 19 | Customer savedCustomer = customerService.createCustomer(customer); 20 | return new ResponseEntity(savedCustomer, HttpStatus.CREATED); 21 | } 22 | @PutMapping("/customers") 23 | public ResponseEntity updateCustomer(@Valid @RequestBody Customer customer, 24 | @RequestParam(required = false) String key) throws CustomerException { 25 | Customer updatedCustomer = customerService.updateCustomer(customer, key); 26 | return new ResponseEntity(updatedCustomer, HttpStatus.OK); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/models/Customer.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.models; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.GenerationType; 6 | import javax.persistence.Id; 7 | import javax.validation.constraints.*; 8 | import lombok.*; 9 | 10 | @Entity 11 | @Getter 12 | @Setter 13 | @ToString 14 | @NoArgsConstructor 15 | @AllArgsConstructor 16 | public class Customer { 17 | 18 | @Id 19 | @GeneratedValue(strategy = GenerationType.AUTO) 20 | private Integer customerId; 21 | 22 | @NotEmpty 23 | @Size(min = 3, message = "Customer Name should contain 3 or more letters.") 24 | private String name; 25 | 26 | @NotEmpty 27 | @Size(min = 10, max = 10, message = "mobile Number must be exact 10 digit !!") 28 | @Digits(fraction = 0, integer = 10, message = "Mobile Number should only contains with numbers.") 29 | private String mobileNum; 30 | 31 | @NotEmpty 32 | @Size(min = 5, max = 15, message = "Customer password should contain minimum 5 and maximum 15 characters.") 33 | private String password; 34 | 35 | @NotEmpty 36 | @Email 37 | @Pattern(regexp = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$", message = "Please Enter valid Email Id included @ and proper Name !!") 38 | private String email; 39 | 40 | public Integer getCustomerId() { 41 | return customerId; 42 | } 43 | 44 | public void setCustomerId(Integer customerId) { 45 | this.customerId = customerId; 46 | } 47 | 48 | public String getName() { 49 | return name; 50 | } 51 | 52 | public void setName(String name) { 53 | this.name = name; 54 | } 55 | 56 | public String getMobileNum() { 57 | return mobileNum; 58 | } 59 | 60 | public void setMobileNum(String mobileNum) { 61 | this.mobileNum = mobileNum; 62 | } 63 | 64 | public String getPassword() { return password; } 65 | 66 | public void setPassword(String password) { this.password = password; } 67 | 68 | public String getEmail() { return email; } 69 | 70 | public void setEmail(String email) { this.email = email; } 71 | } 72 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/services/AdminLoginServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.services; 2 | 3 | import com.ecommerce.Ecommerce.exceptions.LoginException; 4 | import com.ecommerce.Ecommerce.models.Admin; 5 | import com.ecommerce.Ecommerce.models.AdminLoginDTO; 6 | import com.ecommerce.Ecommerce.models.CurrentAdminSession; 7 | import com.ecommerce.Ecommerce.repositories.AdminDAO; 8 | import com.ecommerce.Ecommerce.repositories.AdminSessionDAO; 9 | import net.bytebuddy.utility.RandomString; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.stereotype.Service; 12 | 13 | import java.time.LocalDateTime; 14 | import java.util.Optional; 15 | @Service 16 | public class AdminLoginServiceImpl implements AdminLoginService{ 17 | 18 | @Autowired 19 | private AdminDAO adminDAO; 20 | 21 | @Autowired 22 | private AdminSessionDAO adminSessionDAO; 23 | 24 | @Override 25 | public String logIntoAccount(AdminLoginDTO dto) throws LoginException { 26 | Optional opt = adminDAO.findById(dto.getAdminId()); 27 | Admin existAdmin = opt.get(); 28 | 29 | if (existAdmin == null) { 30 | 31 | throw new LoginException("Please Enter a valid AdminId"); 32 | 33 | } 34 | 35 | Optional validAdminSessionOpt = adminSessionDAO.findById(existAdmin.getAdminId()); 36 | 37 | if (validAdminSessionOpt.isPresent()) { 38 | throw new LoginException("Admin already logged-in with this AdminId"); 39 | } 40 | 41 | if (existAdmin.getAdminPassword().equals(dto.getPassword())) { 42 | 43 | String key = RandomString.make(6); 44 | 45 | CurrentAdminSession currentAdminSession = new CurrentAdminSession(existAdmin.getAdminId(), key, 46 | LocalDateTime.now()); 47 | 48 | adminSessionDAO.save(currentAdminSession); 49 | return currentAdminSession.toString(); 50 | } else 51 | throw new LoginException("Please Enter a valid Admin password"); 52 | } 53 | 54 | @Override 55 | public String logOutFromAccount(String key) throws LoginException { 56 | CurrentAdminSession validAdminSession = adminSessionDAO.findByUuid(key); 57 | if (validAdminSession == null) { 58 | throw new LoginException("Admin was not logged-in with this AdminId"); 59 | 60 | } 61 | adminSessionDAO.delete(validAdminSession); 62 | return "Admin Logged Out !"; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/services/CustomerLoginServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.services; 2 | 3 | import com.ecommerce.Ecommerce.exceptions.LoginException; 4 | import com.ecommerce.Ecommerce.models.CurrentCustomerSession; 5 | import com.ecommerce.Ecommerce.models.Customer; 6 | import com.ecommerce.Ecommerce.models.CustomerLoginDTO; 7 | import com.ecommerce.Ecommerce.repositories.CustomerDAO; 8 | import com.ecommerce.Ecommerce.repositories.CustomerSessionDao; 9 | import net.bytebuddy.utility.RandomString; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.stereotype.Service; 12 | 13 | import java.time.LocalDateTime; 14 | import java.util.Optional; 15 | @Service 16 | public class CustomerLoginServiceImpl implements CustomerLoginService{ 17 | 18 | @Autowired 19 | private CustomerDAO customerDAO; 20 | 21 | @Autowired 22 | private CustomerSessionDao customerSessionDao; 23 | 24 | @Override 25 | public String logIntoAccount(CustomerLoginDTO dto) throws LoginException { 26 | Customer existingCustomer = customerDAO.findByEmail(dto.getEmail()); 27 | 28 | if (existingCustomer == null) { 29 | 30 | throw new LoginException("Please Enter a valid Email"); 31 | 32 | } 33 | 34 | Optional validCustomerSessionOpt = customerSessionDao.findById(existingCustomer.getCustomerId()); 35 | 36 | if (validCustomerSessionOpt.isPresent()) { 37 | 38 | throw new LoginException("User already Logged In with this Email"); 39 | 40 | } 41 | 42 | if (existingCustomer.getPassword().equals(dto.getPassword())) { 43 | 44 | String key = RandomString.make(6); 45 | 46 | CurrentCustomerSession currentUserSession = new CurrentCustomerSession(existingCustomer.getCustomerId(), 47 | key, LocalDateTime.now()); 48 | 49 | customerSessionDao.save(currentUserSession); 50 | 51 | return currentUserSession.toString(); 52 | } else 53 | throw new LoginException("Please Enter a valid password"); 54 | } 55 | 56 | @Override 57 | public String logOutFromAccount(String key) throws LoginException { 58 | CurrentCustomerSession validCustomerSession = customerSessionDao.findByUuid(key); 59 | 60 | if (validCustomerSession == null) { 61 | throw new LoginException("User Not Logged In with this Email"); 62 | 63 | } 64 | 65 | customerSessionDao.delete(validCustomerSession); 66 | 67 | return "Logged Out !"; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Ecommerce/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.7.5 9 | 10 | 11 | com.ecommerce 12 | Ecommerce 13 | 0.0.1-SNAPSHOT 14 | Ecommerce 15 | Ecommerce project for Spring Boot 16 | 17 | 19 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-data-jpa 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-devtools 32 | 33 | 34 | com.mysql 35 | mysql-connector-j 36 | runtime 37 | 38 | 39 | org.projectlombok 40 | lombok 41 | true 42 | 43 | 44 | io.springfox 45 | springfox-boot-starter 46 | 3.0.0 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-starter-test 51 | test 52 | 53 | 54 | org.springframework.boot 55 | spring-boot-starter-web 56 | 57 | 58 | org.springframework.boot 59 | spring-boot-starter-validation 60 | 61 | 62 | 63 | 64 | 65 | 66 | org.springframework.boot 67 | spring-boot-maven-plugin 68 | 69 | 70 | 71 | org.projectlombok 72 | lombok 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # E-commerce Backend 2 | This REST API was created for an E-Commerce Project. This API handles all of the basic CRUD functions of an E-commerce Application platform, including validation at each stage. 3 | 4 | # About the Project 5 | - You can see API Documentation at API root end Point via Swagger. 6 | - Authentication and validation for customer and admin was made using the Session UUID. 7 | - Lombok library used so I minimized boilerplate code in the project. 8 | 9 | 10 | # Tech Stack 11 | + [X] Java 12 | + [X] Springboot 13 | + [X] Spring Data JPA 14 | + [X] Hibernate 15 | + [X] Lombok 16 | + [X] MySQL 17 | + [X] Swagger 18 | + [X] PostMan 19 | --- 20 | # Modules 21 | 22 | ## Admin Module operations 23 | 24 | + [X] Add new products 25 | + [X] Manage product quantities 26 | + [X] All of the Order's management 27 | + [X] Search by customer details ( name, e-mail, phone, id ) 28 | + [X] Customer management 29 | + [X] Search by order 30 | + [X] Admin and Customers ( different login ) 31 | 32 | ## Customer Module operations 33 | 34 | + [X] Login and Register 35 | + [X] See cart details and total price 36 | + [X] Add to cart 37 | + [X] Make a purchase, and track its status 38 | + [X] Search by category 39 | + [X] Payment 40 | 41 | ## API Root End Point 42 | http://localhost:8880/swagger-ui/ 43 | 44 | 45 | ## E-R Diagram 46 | 47 | ![e-r](https://user-images.githubusercontent.com/44535117/208772546-433860dc-1d5a-474b-a834-ec48086d0089.png) 48 | 49 | --- 50 | 51 | ## API Module End Points 52 | 53 | ### Sample API Response for Admin Login 54 | 55 | POST localhost:8080/admin/login 56 | 57 | - Request Body 58 |
 
 59 | 
 60 | {
 61 |   "adminId": 15,
 62 |   "password": "berkbeleli"
 63 | }
 64 | 
 65 | 
66 | - Response 67 |
 
 68 | 
 69 |    CurrentAdminSession(userId=15, uuid=E4Jiid, localDateTime=2022-12-21T00:39:58.270034)
 70 | 
 71 | 
72 | 73 | --- 74 | 75 | ### Swagger UI 76 | 77 | Screenshot 2022-12-21 at 12 43 46 AM 78 | 79 | --- 80 | 81 | ### Admin Controller 82 | Screenshot 2022-12-21 at 12 44 53 AM 83 | 84 | --- 85 | 86 | ### Customer Controller 87 | Screenshot 2022-12-21 at 12 45 17 AM 88 | 89 | 90 | --- 91 | 92 | ### Order Controller 93 | Screenshot 2022-12-21 at 12 45 35 AM 94 | 95 | 96 | --- 97 | 98 | ### Product Controller 99 | Screenshot 2022-12-21 at 12 45 46 AM 100 | 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/services/CustomerServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.services; 2 | 3 | import com.ecommerce.Ecommerce.exceptions.AdminException; 4 | import com.ecommerce.Ecommerce.exceptions.CustomerException; 5 | import com.ecommerce.Ecommerce.models.CurrentAdminSession; 6 | import com.ecommerce.Ecommerce.models.CurrentCustomerSession; 7 | import com.ecommerce.Ecommerce.models.Customer; 8 | import com.ecommerce.Ecommerce.repositories.AdminSessionDAO; 9 | import com.ecommerce.Ecommerce.repositories.CustomerDAO; 10 | import com.ecommerce.Ecommerce.repositories.CustomerSessionDao; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.stereotype.Service; 13 | 14 | import java.util.List; 15 | import java.util.Optional; 16 | @Service 17 | public class CustomerServiceImpl implements CustomerService{ 18 | 19 | @Autowired 20 | private CustomerDAO customerDAO; 21 | 22 | @Autowired 23 | private CustomerSessionDao customerSessionDao; 24 | 25 | @Autowired 26 | private AdminSessionDAO adminSessionDAO; 27 | 28 | @Override 29 | public Customer createCustomer(Customer customer) throws CustomerException { 30 | Customer existingCustomer = customerDAO.findByEmail(customer.getEmail()); 31 | 32 | if (existingCustomer != null) 33 | throw new CustomerException("Customer already registered with this Email"); 34 | 35 | return customerDAO.save(customer); 36 | } 37 | 38 | @Override 39 | public Customer updateCustomer(Customer customer, String key) throws CustomerException { 40 | CurrentCustomerSession loggedInUser = customerSessionDao.findByUuid(key); 41 | 42 | if (loggedInUser == null) { 43 | throw new CustomerException("Please provide a valid key to update a customer"); 44 | } 45 | 46 | if (customer.getCustomerId() == loggedInUser.getUserId()) { 47 | // If LoggedInUser id is same as the id of supplied Customer which we want to 48 | // update 49 | return customerDAO.save(customer); 50 | } else 51 | throw new CustomerException("Invalid Customer Details, please login first"); 52 | } 53 | 54 | @Override 55 | public String deleteCustomerById(Integer customerId) throws CustomerException { 56 | Optional opt = customerDAO.findById(customerId); 57 | 58 | if (opt.isPresent()) { 59 | Customer customer = opt.get(); 60 | customerDAO.delete(customer); 61 | return "Customer deleted successfully"; 62 | } else { 63 | throw new CustomerException("No customer available with this id"); 64 | } 65 | } 66 | 67 | @Override 68 | public List getAllCustomersDetails(String key) throws CustomerException, AdminException { 69 | CurrentAdminSession casDao = adminSessionDAO.findByUuid(key); 70 | 71 | if (casDao != null) { 72 | 73 | List list = customerDAO.findAll(); 74 | if (list.size() != 0) { 75 | return list; 76 | } else { 77 | throw new CustomerException("List is empty."); 78 | } 79 | } else { 80 | throw new AdminException("Wrong key."); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/services/AdminServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.services; 2 | 3 | import com.ecommerce.Ecommerce.exceptions.AdminException; 4 | import com.ecommerce.Ecommerce.exceptions.CustomerException; 5 | import com.ecommerce.Ecommerce.models.Admin; 6 | import com.ecommerce.Ecommerce.models.CurrentAdminSession; 7 | import com.ecommerce.Ecommerce.models.Customer; 8 | import com.ecommerce.Ecommerce.repositories.AdminDAO; 9 | import com.ecommerce.Ecommerce.repositories.AdminSessionDAO; 10 | import com.ecommerce.Ecommerce.repositories.CustomerDAO; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.stereotype.Service; 13 | 14 | import java.util.List; 15 | import java.util.Optional; 16 | @Service 17 | public class AdminServiceImpl implements AdminService{ 18 | 19 | @Autowired 20 | private AdminDAO adminDAO; 21 | 22 | @Autowired 23 | private AdminSessionDAO adminSessionDAO; 24 | 25 | @Autowired 26 | private CustomerDAO customerDAO; 27 | 28 | @Override 29 | public Customer createCustomer(Customer customer) throws CustomerException { 30 | Customer existingCustomer = customerDAO.findByEmail(customer.getEmail()); 31 | 32 | if (existingCustomer != null) 33 | throw new CustomerException("Customer Already Registered with this Email"); 34 | 35 | return customerDAO.save(customer); 36 | } 37 | 38 | @Override 39 | public Admin createAdmin(Admin admin) throws AdminException { 40 | Admin existingAdmin = adminDAO.findByAdminId(admin.getAdminId()); 41 | 42 | if (existingAdmin != null) 43 | throw new AdminException("Admin Already Registered with this AdminId"); 44 | 45 | return adminDAO.save(admin); 46 | } 47 | 48 | @Override 49 | public Admin updateAdmin(Admin admin, String key) throws AdminException { 50 | CurrentAdminSession loggedInAdmin = adminSessionDAO.findByUuid(key); 51 | 52 | if (loggedInAdmin == null) { 53 | throw new AdminException("Please provide a valid key to update a Admin"); 54 | } 55 | 56 | if (admin.getAdminId() == loggedInAdmin.getUserId()) { 57 | // If LoggedInUser id is same as the id of supplied Customer which we want to 58 | // update 59 | return adminDAO.save(admin); 60 | } else 61 | throw new AdminException("Invalid Admin Details, please login first"); 62 | } 63 | 64 | @Override 65 | public Customer updateCustomer(Customer customer, String key) throws CustomerException { 66 | CurrentAdminSession loggedInUser = adminSessionDAO.findByUuid(key); 67 | 68 | if (loggedInUser == null) { 69 | throw new CustomerException("Please provide a valid key to update this customer"); 70 | } 71 | 72 | return customerDAO.save(customer); 73 | } 74 | 75 | @Override 76 | public String deleteAdminById(Integer adminId) throws AdminException { 77 | Optional opt = adminDAO.findById(adminId); 78 | 79 | if (opt.isPresent()) { 80 | Admin admin = opt.get(); 81 | adminDAO.delete(admin); 82 | return "Admin deleted successfully"; 83 | } else { 84 | throw new AdminException("No admin available with this Admin id"); 85 | } 86 | } 87 | 88 | @Override 89 | public List getAllAdminDetails() throws AdminException { 90 | List list = adminDAO.findAll(); 91 | if (list.size() != 0) { 92 | return list; 93 | } else { 94 | throw new AdminException("List is empty..!"); 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/controllers/OrderController.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.controllers; 2 | 3 | import com.ecommerce.Ecommerce.exceptions.CustomerException; 4 | import com.ecommerce.Ecommerce.exceptions.InsufficientQuantityException; 5 | import com.ecommerce.Ecommerce.exceptions.OrderException; 6 | import com.ecommerce.Ecommerce.exceptions.ProductNotFoundException; 7 | import com.ecommerce.Ecommerce.models.CartDTO; 8 | import com.ecommerce.Ecommerce.models.CurrentCustomerSession; 9 | import com.ecommerce.Ecommerce.models.Orders; 10 | import com.ecommerce.Ecommerce.repositories.CustomerSessionDao; 11 | import com.ecommerce.Ecommerce.services.OrderService; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.http.HttpStatus; 14 | import org.springframework.http.ResponseEntity; 15 | import org.springframework.web.bind.annotation.*; 16 | 17 | @RestController 18 | @CrossOrigin("*") 19 | @RequestMapping("/orders") 20 | public class OrderController { 21 | @Autowired 22 | private OrderService orderService; 23 | 24 | @Autowired 25 | private CustomerSessionDao customerSessionDao; 26 | 27 | @GetMapping("/buyProduct/{customerKey}/{productId}/{productName}/{quantity}") 28 | public ResponseEntity buyProductByProductIdHandler(@PathVariable("customerKey") String key, 29 | @PathVariable("productId") Integer productId, @PathVariable("productName") String productName, @PathVariable("quantity") Integer quantity) 30 | throws CustomerException, ProductNotFoundException, InsufficientQuantityException { 31 | 32 | CurrentCustomerSession ccs = customerSessionDao.findByUuid(key); 33 | 34 | if (ccs == null) { 35 | 36 | throw new CustomerException("No customer exist with this key"); 37 | } else { 38 | 39 | Orders order = orderService.buyProductByProductId(key, productId,productName, quantity); 40 | 41 | return new ResponseEntity(order, HttpStatus.OK); 42 | } 43 | } 44 | 45 | @GetMapping("/{customerKey}") 46 | public ResponseEntity visitYourCartHandler(@PathVariable("customerKey") String key) 47 | throws OrderException, CustomerException { 48 | 49 | CurrentCustomerSession ccs = customerSessionDao.findByUuid(key); 50 | if (ccs == null) { 51 | throw new CustomerException("No customer exist with this key"); 52 | } else { 53 | 54 | CartDTO cartdto = orderService.visitYourCart(key); 55 | return new ResponseEntity(cartdto, HttpStatus.OK); 56 | } 57 | } 58 | 59 | @DeleteMapping("/{customerKey}/{amount}") 60 | public ResponseEntity payYourAmount(@PathVariable("customerKey") String key, 61 | @PathVariable("amount") Double amount) throws OrderException, CustomerException { 62 | 63 | CurrentCustomerSession css = customerSessionDao.findByUuid(key); 64 | 65 | if (css == null) { 66 | 67 | throw new CustomerException("No customer exists with this Key"); 68 | } else { 69 | 70 | String string = orderService.paymentAmount(key, amount); 71 | 72 | return new ResponseEntity(string, HttpStatus.OK); 73 | } 74 | } 75 | 76 | @DeleteMapping("/delete/{customerKey}/{orderId}") 77 | public ResponseEntity deleteProductByOrderIdHandler(@PathVariable("customerKey") String key, 78 | @PathVariable("orderId") Integer orderId) throws CustomerException, OrderException { 79 | 80 | CurrentCustomerSession ccs = customerSessionDao.findByUuid(key); 81 | 82 | if (ccs == null) { 83 | throw new CustomerException("No customer exist with this key"); 84 | } else { 85 | Orders order = orderService.deleteProductByOrderId(key, orderId); 86 | return new ResponseEntity(order, HttpStatus.OK); 87 | } 88 | 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/controllers/AdminController.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.controllers; 2 | 3 | import com.ecommerce.Ecommerce.exceptions.AdminException; 4 | import com.ecommerce.Ecommerce.exceptions.CustomerException; 5 | import com.ecommerce.Ecommerce.models.Admin; 6 | import com.ecommerce.Ecommerce.models.CurrentAdminSession; 7 | import com.ecommerce.Ecommerce.models.Customer; 8 | import com.ecommerce.Ecommerce.repositories.AdminSessionDAO; 9 | import com.ecommerce.Ecommerce.services.AdminService; 10 | import com.ecommerce.Ecommerce.services.CustomerService; 11 | import javax.validation.Valid; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.http.HttpStatus; 14 | import org.springframework.http.ResponseEntity; 15 | import org.springframework.web.bind.annotation.*; 16 | 17 | import java.util.List; 18 | 19 | @RestController 20 | @CrossOrigin("*") 21 | @RequestMapping("/admin") 22 | public class AdminController { 23 | 24 | @Autowired 25 | private CustomerService customerService; 26 | 27 | @Autowired 28 | private AdminService adminService; 29 | 30 | @Autowired 31 | private AdminSessionDAO adminSessionDAO; 32 | 33 | @PostMapping("/createadmin") 34 | public ResponseEntity createAdmin(@Valid @RequestBody Admin admin) throws AdminException { 35 | Admin savedAdmin = adminService.createAdmin(admin); 36 | return new ResponseEntity(savedAdmin, HttpStatus.CREATED); 37 | } 38 | 39 | @PostMapping("/customers/{adminkey}") 40 | public ResponseEntity saveCustomer(@PathVariable("adminkey") String key, 41 | @Valid @RequestBody Customer customer) throws CustomerException, AdminException { 42 | 43 | CurrentAdminSession loggedInAdmin = adminSessionDAO.findByUuid(key); 44 | if (loggedInAdmin == null) { 45 | throw new AdminException("Please provide a valid key"); 46 | } else { 47 | Customer savedCustomer = adminService.createCustomer(customer); 48 | return new ResponseEntity(savedCustomer, HttpStatus.CREATED); 49 | } 50 | } 51 | 52 | 53 | @PutMapping("/customers") 54 | public ResponseEntity updateCustomer(@Valid @RequestBody Customer customer, 55 | @RequestParam(required = false) String key) throws CustomerException, AdminException { 56 | 57 | CurrentAdminSession loggedInAdmin = adminSessionDAO.findByUuid(key); 58 | 59 | if (loggedInAdmin == null) { 60 | throw new AdminException("Please provide a valid key"); 61 | } else { 62 | 63 | Customer updatedCustomer = adminService.updateCustomer(customer, key); 64 | 65 | return new ResponseEntity(updatedCustomer, HttpStatus.OK); 66 | 67 | } 68 | 69 | } 70 | 71 | @DeleteMapping("/customers/{customerId}") 72 | public ResponseEntity deleteCustomer(@PathVariable("customerId") Integer customerId, 73 | @RequestParam(required = false) String key) throws CustomerException, AdminException { 74 | CurrentAdminSession loggedInAdmin = adminSessionDAO.findByUuid(key); 75 | 76 | if (loggedInAdmin == null) { 77 | throw new AdminException("Please provide a valid key"); 78 | } else { 79 | 80 | String message = customerService.deleteCustomerById(customerId); 81 | 82 | return new ResponseEntity(message, HttpStatus.OK); 83 | 84 | } 85 | } 86 | 87 | @GetMapping("/getcustomers/{adminkey}") 88 | public ResponseEntity> getAllCustomers(@PathVariable("adminkey") String key) 89 | throws CustomerException, AdminException { 90 | 91 | CurrentAdminSession loggedInAdmin = adminSessionDAO.findByUuid(key); 92 | 93 | if (loggedInAdmin == null) { 94 | throw new AdminException("Please provide a valid key"); 95 | } else { 96 | 97 | List list = customerService.getAllCustomersDetails(key); 98 | 99 | return new ResponseEntity>(list, HttpStatus.OK); 100 | 101 | } 102 | 103 | } 104 | 105 | 106 | } 107 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/services/OrderServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.services; 2 | 3 | import com.ecommerce.Ecommerce.exceptions.CustomerException; 4 | import com.ecommerce.Ecommerce.exceptions.InsufficientQuantityException; 5 | import com.ecommerce.Ecommerce.exceptions.OrderException; 6 | import com.ecommerce.Ecommerce.exceptions.ProductNotFoundException; 7 | import com.ecommerce.Ecommerce.models.CartDTO; 8 | import com.ecommerce.Ecommerce.models.Orders; 9 | import com.ecommerce.Ecommerce.models.Product; 10 | import com.ecommerce.Ecommerce.repositories.OrderRepositoryDAO; 11 | import com.ecommerce.Ecommerce.repositories.ProductRepositoryDAO; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.stereotype.Service; 14 | 15 | import java.time.LocalDateTime; 16 | import java.util.List; 17 | import java.util.Optional; 18 | 19 | @Service 20 | public class OrderServiceImpl implements OrderService{ 21 | 22 | @Autowired 23 | private OrderRepositoryDAO orderRepositoryDAO; 24 | 25 | @Autowired 26 | private ProductRepositoryDAO productRepositoryDAO; 27 | 28 | @Override 29 | public Orders buyProductByProductId(String sessionId, Integer productId, String productName, Integer quantity) throws CustomerException, ProductNotFoundException, InsufficientQuantityException { 30 | Optional opt = productRepositoryDAO.findById(productId); 31 | 32 | if (opt.isPresent()) { 33 | 34 | Product product = opt.get(); 35 | if (product.getStock() < quantity) { 36 | 37 | throw new InsufficientQuantityException("Product Stock is less than your quantity"); 38 | } else { 39 | 40 | Orders order = new Orders(); 41 | order.setDateAndTime(LocalDateTime.now()); 42 | order.setProductId(productId); 43 | order.setProductName(productName); 44 | order.setQuantity(quantity); 45 | order.setSessionId(sessionId); 46 | order.setTotalCost(quantity * product.getCost()); 47 | 48 | Orders obj = orderRepositoryDAO.save(order); 49 | 50 | product.setStock(product.getStock() - quantity); 51 | productRepositoryDAO.save(product); 52 | return obj; 53 | } 54 | 55 | } else { 56 | 57 | throw new ProductNotFoundException("No product found with this product Id"); 58 | } 59 | } 60 | 61 | @Override 62 | public CartDTO visitYourCart(String customerKey) throws CustomerException, OrderException { 63 | List list = orderRepositoryDAO.findBySessionId(customerKey); 64 | 65 | if (list.isEmpty()) { 66 | throw new OrderException("You do not have any item in your cart"); 67 | } 68 | 69 | Double sum = 0.00; 70 | 71 | for (Orders order : list) { 72 | 73 | sum += order.getTotalCost(); 74 | 75 | } 76 | 77 | CartDTO cart = new CartDTO(); 78 | cart.setList(list); 79 | cart.setTotalBill(sum); 80 | 81 | return cart; 82 | } 83 | 84 | @Override 85 | public String paymentAmount(String customerKey, Double amount) throws CustomerException, OrderException { 86 | List list = orderRepositoryDAO.findBySessionId(customerKey); 87 | Double sum = 0.00; 88 | 89 | for (Orders order : list) { 90 | 91 | sum += order.getTotalCost(); 92 | } 93 | 94 | int x = Double.compare(amount, sum); 95 | 96 | if (x == 0) { 97 | orderRepositoryDAO.deleteAll(); 98 | return "Payment Successfully done"; 99 | 100 | } else { 101 | throw new OrderException("Amount should be equal to : " + sum); 102 | } 103 | } 104 | 105 | @Override 106 | public Orders deleteProductByOrderId(String customerKey, Integer orderId) throws CustomerException, OrderException { 107 | 108 | Optional opt = orderRepositoryDAO.findById(orderId); 109 | 110 | if (opt.isPresent()) { 111 | orderRepositoryDAO.deleteById(orderId); 112 | return opt.get(); 113 | 114 | } else { 115 | throw new OrderException("No order Present with this Order Id"); 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/services/ProductServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.services; 2 | 3 | import com.ecommerce.Ecommerce.exceptions.ProductNotFoundException; 4 | import com.ecommerce.Ecommerce.models.Product; 5 | import com.ecommerce.Ecommerce.repositories.ProductRepositoryDAO; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.List; 10 | import java.util.Optional; 11 | 12 | @Service 13 | public class ProductServiceImpl implements ProductService{ 14 | 15 | @Autowired 16 | private ProductRepositoryDAO productRepositoryDAO; 17 | 18 | @Override 19 | public Product addProduct(Product product) throws ProductNotFoundException { 20 | if (productRepositoryDAO.findById(product.getProductId()).isPresent()) { 21 | throw new ProductNotFoundException("Product already exist with this ProductId"); 22 | } else { 23 | Product obj = productRepositoryDAO.save(product); 24 | return obj; 25 | } 26 | } 27 | 28 | @Override 29 | public Product updateProduct(Product product) throws ProductNotFoundException { 30 | Optional opt = productRepositoryDAO.findById(product.getProductId()); 31 | if (opt.isPresent()) { 32 | return productRepositoryDAO.save(product); 33 | } else { 34 | throw new ProductNotFoundException("No Product found with this ProductId"); 35 | } 36 | } 37 | 38 | @Override 39 | public List getAllProducts() throws ProductNotFoundException { 40 | List list = productRepositoryDAO.findAll(); 41 | if (list.isEmpty()) { 42 | throw new ProductNotFoundException("No product found."); 43 | } else { 44 | return list; 45 | } 46 | } 47 | 48 | @Override 49 | public Product deleteProductById(Integer productId) throws ProductNotFoundException { 50 | Optional opt = productRepositoryDAO.findById(productId); 51 | if (opt.isPresent()) { 52 | Product product = opt.get(); 53 | productRepositoryDAO.delete(product); 54 | return product; 55 | } else { 56 | throw new ProductNotFoundException("No product present with this ProductId"); 57 | } 58 | } 59 | 60 | @Override 61 | public Product viewProductById(Integer productId) throws ProductNotFoundException { 62 | Optional product = productRepositoryDAO.findById(productId); 63 | if (product.isPresent()) { 64 | return product.get(); 65 | } else { 66 | throw new ProductNotFoundException("Product does not exist with this ProductId"); 67 | } 68 | } 69 | 70 | @Override 71 | public List viewProductByName(String name) throws ProductNotFoundException { 72 | List list = productRepositoryDAO.findByProductName(name); 73 | if (list.isEmpty()) { 74 | throw new ProductNotFoundException("Product does not exist with this Name"); 75 | } else { 76 | return list; 77 | } 78 | } 79 | 80 | @Override 81 | public Product changeQuantityOfProductByProductId(Integer productId, Integer newQuantity) throws ProductNotFoundException { 82 | Optional opt = productRepositoryDAO.findById(productId); 83 | if (opt.isPresent()) { 84 | Product product = opt.get(); 85 | product.setStock(newQuantity); 86 | productRepositoryDAO.save(product); 87 | return product; 88 | } else { 89 | throw new ProductNotFoundException("Product does not exist with this ProductId"); 90 | } 91 | } 92 | 93 | @Override 94 | public Product changePriceOfProductByProductId(Integer productId, Double newPrice) throws ProductNotFoundException { 95 | Optional opt = productRepositoryDAO.findById(productId); 96 | 97 | if (opt.isPresent()) { 98 | Product product = opt.get(); 99 | product.setCost(newPrice); 100 | productRepositoryDAO.save(product); 101 | return product; 102 | } else { 103 | 104 | throw new ProductNotFoundException("Product does not exist with this ProductId"); 105 | } 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/exceptions/GlobalExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.exceptions; 2 | 3 | import javax.persistence.RollbackException; 4 | import org.springframework.web.bind.annotation.ControllerAdvice; 5 | import java.time.LocalDateTime; 6 | 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.MethodArgumentNotValidException; 10 | import org.springframework.web.bind.annotation.ExceptionHandler; 11 | import org.springframework.web.context.request.WebRequest; 12 | import org.springframework.web.servlet.NoHandlerFoundException; 13 | 14 | @ControllerAdvice 15 | public class GlobalExceptionHandler { 16 | @ExceptionHandler(CustomerException.class) 17 | public ResponseEntity customerExceptionHandler(CustomerException se, WebRequest req) { 18 | 19 | MyErrorDetails err = new MyErrorDetails(); 20 | err.setTimestamp(LocalDateTime.now()); 21 | err.setMessage(se.getMessage()); 22 | err.setDetails(req.getDescription(false)); 23 | 24 | return new ResponseEntity(err, HttpStatus.NOT_FOUND); 25 | 26 | } 27 | 28 | @ExceptionHandler(LoginException.class) 29 | public ResponseEntity loginExceptionHandler(LoginException se, WebRequest req) { 30 | 31 | MyErrorDetails err = new MyErrorDetails(); 32 | err.setTimestamp(LocalDateTime.now()); 33 | err.setMessage(se.getMessage()); 34 | err.setDetails(req.getDescription(false)); 35 | 36 | return new ResponseEntity(err, HttpStatus.BAD_REQUEST); 37 | 38 | } 39 | 40 | @ExceptionHandler(Exception.class) 41 | public ResponseEntity otherExceptionHandler(Exception se, WebRequest req) { 42 | 43 | MyErrorDetails err = new MyErrorDetails(); 44 | err.setTimestamp(LocalDateTime.now()); 45 | err.setMessage(se.getMessage()); 46 | err.setDetails(req.getDescription(false)); 47 | 48 | return new ResponseEntity(err, HttpStatus.INTERNAL_SERVER_ERROR); 49 | 50 | } 51 | 52 | ///////////////////////////////////////////// 53 | 54 | // exceptions for plant module 55 | 56 | @ExceptionHandler(ProductNotFoundException.class) 57 | public ResponseEntity seedNotfoundExceptionHandler(ProductNotFoundException snfe, WebRequest req) { 58 | 59 | MyErrorDetails err = new MyErrorDetails(); 60 | err.setTimestamp(LocalDateTime.now()); 61 | err.setDetails(req.getDescription(false)); 62 | err.setMessage(snfe.getMessage()); 63 | 64 | return new ResponseEntity(err, HttpStatus.BAD_REQUEST); 65 | } 66 | 67 | @ExceptionHandler(InvalidKeyException.class) 68 | public ResponseEntity invalidKeyExceptionHandler(InvalidKeyException keyex, WebRequest req) { 69 | 70 | MyErrorDetails err = new MyErrorDetails(); 71 | err.setTimestamp(LocalDateTime.now()); 72 | err.setDetails(req.getDescription(false)); 73 | err.setMessage(keyex.getMessage()); 74 | 75 | return new ResponseEntity(err, HttpStatus.BAD_REQUEST); 76 | } 77 | 78 | @ExceptionHandler(MethodArgumentNotValidException.class) 79 | public ResponseEntity invalidMethodArgumentExceptionHandler(MethodArgumentNotValidException error) { 80 | 81 | MyErrorDetails err = new MyErrorDetails(); 82 | err.setTimestamp(LocalDateTime.now()); 83 | err.setMessage("Invalid Argument: Please pass the valid argument."); 84 | err.setDetails(error.getBindingResult().getFieldError().getDefaultMessage()); 85 | 86 | return new ResponseEntity(err, HttpStatus.FORBIDDEN); 87 | } 88 | 89 | @ExceptionHandler(NoHandlerFoundException.class) 90 | public ResponseEntity noHandlerExceptionHandler(NoHandlerFoundException error, WebRequest wr) { 91 | 92 | System.out.println("Inside No handler Exception : Global exception handling"); 93 | MyErrorDetails err = new MyErrorDetails(LocalDateTime.now(), error.getMessage(), wr.getDescription(false)); 94 | 95 | return new ResponseEntity(err, HttpStatus.NOT_FOUND); 96 | } 97 | 98 | @ExceptionHandler(RollbackException.class) 99 | public ResponseEntity handleRollbackException(RollbackException exp, WebRequest req) { 100 | MyErrorDetails err = new MyErrorDetails(LocalDateTime.now(), 101 | "Improper arguments passed in json: Validation failed", req.getDescription(false)); 102 | 103 | return new ResponseEntity<>(err, HttpStatus.BAD_REQUEST); 104 | } 105 | 106 | } 107 | -------------------------------------------------------------------------------- /Ecommerce/src/main/java/com/ecommerce/Ecommerce/controllers/ProductController.java: -------------------------------------------------------------------------------- 1 | package com.ecommerce.Ecommerce.controllers; 2 | 3 | import com.ecommerce.Ecommerce.exceptions.AdminException; 4 | import com.ecommerce.Ecommerce.exceptions.ProductNotFoundException; 5 | import com.ecommerce.Ecommerce.models.CurrentAdminSession; 6 | import com.ecommerce.Ecommerce.models.Product; 7 | import com.ecommerce.Ecommerce.repositories.AdminSessionDAO; 8 | import com.ecommerce.Ecommerce.services.ProductService; 9 | import javax.validation.Valid; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.http.HttpStatus; 12 | import org.springframework.http.ResponseEntity; 13 | import org.springframework.web.bind.annotation.*; 14 | 15 | import java.util.List; 16 | 17 | @RestController 18 | public class ProductController { 19 | @Autowired 20 | private ProductService productService; 21 | 22 | @Autowired 23 | private AdminSessionDAO adminSessionDAO; 24 | 25 | // adding new product in database 26 | @PostMapping("admin/products/{adminkey}") 27 | public ResponseEntity addProductHandler(@PathVariable("adminkey") String key, 28 | @Valid @RequestBody Product product) throws ProductNotFoundException, AdminException { 29 | 30 | CurrentAdminSession loggedInAdmin = adminSessionDAO.findByUuid(key); 31 | 32 | if (loggedInAdmin == null) { 33 | throw new AdminException("Please provide a valid key"); 34 | } else { 35 | Product sObj = productService.addProduct(product); 36 | return new ResponseEntity(sObj, HttpStatus.CREATED); 37 | } 38 | } 39 | 40 | // updating existing product details 41 | 42 | @PutMapping("admin/products/{adminkey}") 43 | public ResponseEntity updateProductHandler(@Valid @RequestBody Product product, 44 | @PathVariable("adminkey") String key) throws ProductNotFoundException, AdminException { 45 | CurrentAdminSession loggedInAdmin = adminSessionDAO.findByUuid(key); 46 | 47 | if (loggedInAdmin == null) { 48 | throw new AdminException("Please provide a valid key"); 49 | } else { 50 | Product sObj = productService.updateProduct(product); 51 | 52 | return new ResponseEntity(sObj, HttpStatus.ACCEPTED); 53 | } 54 | 55 | } 56 | 57 | // getting all products details from database 58 | 59 | @GetMapping("/products") 60 | public ResponseEntity> getAllProductsHandler() throws ProductNotFoundException { 61 | 62 | List products = productService.getAllProducts(); 63 | 64 | return new ResponseEntity>(products, HttpStatus.OK); 65 | } 66 | 67 | // delete existing product by ProductId 68 | 69 | @DeleteMapping("admin/products/{id}/{adminkey}") 70 | public ResponseEntity deleteProductByIdHandler(@PathVariable("id") Integer productId, 71 | @PathVariable("adminkey") String key) throws ProductNotFoundException, AdminException { 72 | CurrentAdminSession loggedInAdmin = adminSessionDAO.findByUuid(key); 73 | 74 | if (loggedInAdmin == null) { 75 | throw new AdminException("Please provide a valid key"); 76 | } else { 77 | 78 | Product product = productService.deleteProductById(productId); 79 | 80 | return new ResponseEntity(product, HttpStatus.OK); 81 | 82 | } 83 | } 84 | 85 | // view product by ProductId 86 | 87 | @GetMapping("/products/{id}") 88 | public ResponseEntity viewProductByIdHandler(@PathVariable("id") Integer productId) 89 | throws ProductNotFoundException { 90 | 91 | Product product = productService.viewProductById(productId); 92 | 93 | return new ResponseEntity(product, HttpStatus.OK); 94 | 95 | } 96 | 97 | // view Product by Product name 98 | 99 | @GetMapping("/product/{name}") 100 | public ResponseEntity> viewProductByProductNameHandler(@PathVariable("name") String name) 101 | throws ProductNotFoundException { 102 | 103 | List list = productService.viewProductByName(name); 104 | 105 | return new ResponseEntity>(list, HttpStatus.OK); 106 | 107 | } 108 | 109 | // set new Quantity of product 110 | 111 | @PutMapping("admin/setProductQuantity/{id}/{quantity}/{adminkey}") 112 | public ResponseEntity setProductQuantityByProductIdHandler(@PathVariable("adminkey") String key, 113 | @PathVariable("id") Integer productid, @PathVariable("quantity") Integer quantity) 114 | throws ProductNotFoundException, AdminException { 115 | CurrentAdminSession loggedInAdmin = adminSessionDAO.findByUuid(key); 116 | if (loggedInAdmin == null) { 117 | throw new AdminException("Please provide a valid key"); 118 | } else { 119 | 120 | Product product = productService.changeQuantityOfProductByProductId(productid, quantity); 121 | 122 | return new ResponseEntity(product, HttpStatus.OK); 123 | 124 | } 125 | } 126 | 127 | // set new Price of product 128 | 129 | @PutMapping("admin/setProductPrice/{id}/{price}/{adminkey}") 130 | public ResponseEntity setProductPriceByProductIdHandler(@PathVariable("adminkey") String key, 131 | @PathVariable("id") Integer productId, @PathVariable("price") Double price) 132 | throws ProductNotFoundException, AdminException { 133 | CurrentAdminSession loggedInAdmin = adminSessionDAO.findByUuid(key); 134 | if (loggedInAdmin == null) { 135 | throw new AdminException("Please provide a valid key"); 136 | } else { 137 | 138 | Product product = productService.changePriceOfProductByProductId(productId, price); 139 | 140 | return new ResponseEntity(product, HttpStatus.OK); 141 | } 142 | } 143 | 144 | 145 | } 146 | -------------------------------------------------------------------------------- /Ecommerce/mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM https://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM set title of command window 39 | title %0 40 | @REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' 41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 42 | 43 | @REM set %HOME% to equivalent of $HOME 44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 45 | 46 | @REM Execute a user defined script before this one 47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 49 | if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %* 50 | if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %* 51 | :skipRcPre 52 | 53 | @setlocal 54 | 55 | set ERROR_CODE=0 56 | 57 | @REM To isolate internal variables from possible post scripts, we use another setlocal 58 | @setlocal 59 | 60 | @REM ==== START VALIDATION ==== 61 | if not "%JAVA_HOME%" == "" goto OkJHome 62 | 63 | echo. 64 | echo Error: JAVA_HOME not found in your environment. >&2 65 | echo Please set the JAVA_HOME variable in your environment to match the >&2 66 | echo location of your Java installation. >&2 67 | echo. 68 | goto error 69 | 70 | :OkJHome 71 | if exist "%JAVA_HOME%\bin\java.exe" goto init 72 | 73 | echo. 74 | echo Error: JAVA_HOME is set to an invalid directory. >&2 75 | echo JAVA_HOME = "%JAVA_HOME%" >&2 76 | echo Please set the JAVA_HOME variable in your environment to match the >&2 77 | echo location of your Java installation. >&2 78 | echo. 79 | goto error 80 | 81 | @REM ==== END VALIDATION ==== 82 | 83 | :init 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 121 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 122 | 123 | set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" 124 | 125 | FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( 126 | IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B 127 | ) 128 | 129 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 130 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data. 131 | if exist %WRAPPER_JAR% ( 132 | if "%MVNW_VERBOSE%" == "true" ( 133 | echo Found %WRAPPER_JAR% 134 | ) 135 | ) else ( 136 | if not "%MVNW_REPOURL%" == "" ( 137 | SET DOWNLOAD_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" 138 | ) 139 | if "%MVNW_VERBOSE%" == "true" ( 140 | echo Couldn't find %WRAPPER_JAR%, downloading it ... 141 | echo Downloading from: %DOWNLOAD_URL% 142 | ) 143 | 144 | powershell -Command "&{"^ 145 | "$webclient = new-object System.Net.WebClient;"^ 146 | "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ 147 | "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ 148 | "}"^ 149 | "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ 150 | "}" 151 | if "%MVNW_VERBOSE%" == "true" ( 152 | echo Finished downloading %WRAPPER_JAR% 153 | ) 154 | ) 155 | @REM End of extension 156 | 157 | @REM Provide a "standardized" way to retrieve the CLI args that will 158 | @REM work with both Windows and non-Windows executions. 159 | set MAVEN_CMD_LINE_ARGS=%* 160 | 161 | %MAVEN_JAVA_EXE% ^ 162 | %JVM_CONFIG_MAVEN_PROPS% ^ 163 | %MAVEN_OPTS% ^ 164 | %MAVEN_DEBUG_OPTS% ^ 165 | -classpath %WRAPPER_JAR% ^ 166 | "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^ 167 | %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 168 | if ERRORLEVEL 1 goto error 169 | goto end 170 | 171 | :error 172 | set ERROR_CODE=1 173 | 174 | :end 175 | @endlocal & set ERROR_CODE=%ERROR_CODE% 176 | 177 | if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost 178 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 179 | if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat" 180 | if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd" 181 | :skipRcPost 182 | 183 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 184 | if "%MAVEN_BATCH_PAUSE%"=="on" pause 185 | 186 | if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE% 187 | 188 | cmd /C exit /B %ERROR_CODE% 189 | -------------------------------------------------------------------------------- /Ecommerce/mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # https://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /usr/local/etc/mavenrc ] ; then 40 | . /usr/local/etc/mavenrc 41 | fi 42 | 43 | if [ -f /etc/mavenrc ] ; then 44 | . /etc/mavenrc 45 | fi 46 | 47 | if [ -f "$HOME/.mavenrc" ] ; then 48 | . "$HOME/.mavenrc" 49 | fi 50 | 51 | fi 52 | 53 | # OS specific support. $var _must_ be set to either true or false. 54 | cygwin=false; 55 | darwin=false; 56 | mingw=false 57 | case "`uname`" in 58 | CYGWIN*) cygwin=true ;; 59 | MINGW*) mingw=true;; 60 | Darwin*) darwin=true 61 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 62 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 63 | if [ -z "$JAVA_HOME" ]; then 64 | if [ -x "/usr/libexec/java_home" ]; then 65 | export JAVA_HOME="`/usr/libexec/java_home`" 66 | else 67 | export JAVA_HOME="/Library/Java/Home" 68 | fi 69 | fi 70 | ;; 71 | esac 72 | 73 | if [ -z "$JAVA_HOME" ] ; then 74 | if [ -r /etc/gentoo-release ] ; then 75 | JAVA_HOME=`java-config --jre-home` 76 | fi 77 | fi 78 | 79 | if [ -z "$M2_HOME" ] ; then 80 | ## resolve links - $0 may be a link to maven's home 81 | PRG="$0" 82 | 83 | # need this for relative symlinks 84 | while [ -h "$PRG" ] ; do 85 | ls=`ls -ld "$PRG"` 86 | link=`expr "$ls" : '.*-> \(.*\)$'` 87 | if expr "$link" : '/.*' > /dev/null; then 88 | PRG="$link" 89 | else 90 | PRG="`dirname "$PRG"`/$link" 91 | fi 92 | done 93 | 94 | saveddir=`pwd` 95 | 96 | M2_HOME=`dirname "$PRG"`/.. 97 | 98 | # make it fully qualified 99 | M2_HOME=`cd "$M2_HOME" && pwd` 100 | 101 | cd "$saveddir" 102 | # echo Using m2 at $M2_HOME 103 | fi 104 | 105 | # For Cygwin, ensure paths are in UNIX format before anything is touched 106 | if $cygwin ; then 107 | [ -n "$M2_HOME" ] && 108 | M2_HOME=`cygpath --unix "$M2_HOME"` 109 | [ -n "$JAVA_HOME" ] && 110 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 111 | [ -n "$CLASSPATH" ] && 112 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 113 | fi 114 | 115 | # For Mingw, ensure paths are in UNIX format before anything is touched 116 | if $mingw ; then 117 | [ -n "$M2_HOME" ] && 118 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 119 | [ -n "$JAVA_HOME" ] && 120 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 121 | fi 122 | 123 | if [ -z "$JAVA_HOME" ]; then 124 | javaExecutable="`which javac`" 125 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 126 | # readlink(1) is not available as standard on Solaris 10. 127 | readLink=`which readlink` 128 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 129 | if $darwin ; then 130 | javaHome="`dirname \"$javaExecutable\"`" 131 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 132 | else 133 | javaExecutable="`readlink -f \"$javaExecutable\"`" 134 | fi 135 | javaHome="`dirname \"$javaExecutable\"`" 136 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 137 | JAVA_HOME="$javaHome" 138 | export JAVA_HOME 139 | fi 140 | fi 141 | fi 142 | 143 | if [ -z "$JAVACMD" ] ; then 144 | if [ -n "$JAVA_HOME" ] ; then 145 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 146 | # IBM's JDK on AIX uses strange locations for the executables 147 | JAVACMD="$JAVA_HOME/jre/sh/java" 148 | else 149 | JAVACMD="$JAVA_HOME/bin/java" 150 | fi 151 | else 152 | JAVACMD="`\\unset -f command; \\command -v java`" 153 | fi 154 | fi 155 | 156 | if [ ! -x "$JAVACMD" ] ; then 157 | echo "Error: JAVA_HOME is not defined correctly." >&2 158 | echo " We cannot execute $JAVACMD" >&2 159 | exit 1 160 | fi 161 | 162 | if [ -z "$JAVA_HOME" ] ; then 163 | echo "Warning: JAVA_HOME environment variable is not set." 164 | fi 165 | 166 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 167 | 168 | # traverses directory structure from process work directory to filesystem root 169 | # first directory with .mvn subdirectory is considered project base directory 170 | find_maven_basedir() { 171 | 172 | if [ -z "$1" ] 173 | then 174 | echo "Path not specified to find_maven_basedir" 175 | return 1 176 | fi 177 | 178 | basedir="$1" 179 | wdir="$1" 180 | while [ "$wdir" != '/' ] ; do 181 | if [ -d "$wdir"/.mvn ] ; then 182 | basedir=$wdir 183 | break 184 | fi 185 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 186 | if [ -d "${wdir}" ]; then 187 | wdir=`cd "$wdir/.."; pwd` 188 | fi 189 | # end of workaround 190 | done 191 | echo "${basedir}" 192 | } 193 | 194 | # concatenates all lines of a file 195 | concat_lines() { 196 | if [ -f "$1" ]; then 197 | echo "$(tr -s '\n' ' ' < "$1")" 198 | fi 199 | } 200 | 201 | BASE_DIR=`find_maven_basedir "$(pwd)"` 202 | if [ -z "$BASE_DIR" ]; then 203 | exit 1; 204 | fi 205 | 206 | ########################################################################################## 207 | # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 208 | # This allows using the maven wrapper in projects that prohibit checking in binary data. 209 | ########################################################################################## 210 | if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then 211 | if [ "$MVNW_VERBOSE" = true ]; then 212 | echo "Found .mvn/wrapper/maven-wrapper.jar" 213 | fi 214 | else 215 | if [ "$MVNW_VERBOSE" = true ]; then 216 | echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." 217 | fi 218 | if [ -n "$MVNW_REPOURL" ]; then 219 | jarUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" 220 | else 221 | jarUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" 222 | fi 223 | while IFS="=" read key value; do 224 | case "$key" in (wrapperUrl) jarUrl="$value"; break ;; 225 | esac 226 | done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" 227 | if [ "$MVNW_VERBOSE" = true ]; then 228 | echo "Downloading from: $jarUrl" 229 | fi 230 | wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" 231 | if $cygwin; then 232 | wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` 233 | fi 234 | 235 | if command -v wget > /dev/null; then 236 | if [ "$MVNW_VERBOSE" = true ]; then 237 | echo "Found wget ... using wget" 238 | fi 239 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 240 | wget "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" 241 | else 242 | wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" 243 | fi 244 | elif command -v curl > /dev/null; then 245 | if [ "$MVNW_VERBOSE" = true ]; then 246 | echo "Found curl ... using curl" 247 | fi 248 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 249 | curl -o "$wrapperJarPath" "$jarUrl" -f 250 | else 251 | curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f 252 | fi 253 | 254 | else 255 | if [ "$MVNW_VERBOSE" = true ]; then 256 | echo "Falling back to using Java to download" 257 | fi 258 | javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" 259 | # For Cygwin, switch paths to Windows format before running javac 260 | if $cygwin; then 261 | javaClass=`cygpath --path --windows "$javaClass"` 262 | fi 263 | if [ -e "$javaClass" ]; then 264 | if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 265 | if [ "$MVNW_VERBOSE" = true ]; then 266 | echo " - Compiling MavenWrapperDownloader.java ..." 267 | fi 268 | # Compiling the Java class 269 | ("$JAVA_HOME/bin/javac" "$javaClass") 270 | fi 271 | if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 272 | # Running the downloader 273 | if [ "$MVNW_VERBOSE" = true ]; then 274 | echo " - Running MavenWrapperDownloader.java ..." 275 | fi 276 | ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") 277 | fi 278 | fi 279 | fi 280 | fi 281 | ########################################################################################## 282 | # End of extension 283 | ########################################################################################## 284 | 285 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 286 | if [ "$MVNW_VERBOSE" = true ]; then 287 | echo $MAVEN_PROJECTBASEDIR 288 | fi 289 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 290 | 291 | # For Cygwin, switch paths to Windows format before running java 292 | if $cygwin; then 293 | [ -n "$M2_HOME" ] && 294 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 295 | [ -n "$JAVA_HOME" ] && 296 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 297 | [ -n "$CLASSPATH" ] && 298 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 299 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 300 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 301 | fi 302 | 303 | # Provide a "standardized" way to retrieve the CLI args that will 304 | # work with both Windows and non-Windows executions. 305 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" 306 | export MAVEN_CMD_LINE_ARGS 307 | 308 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 309 | 310 | exec "$JAVACMD" \ 311 | $MAVEN_OPTS \ 312 | $MAVEN_DEBUG_OPTS \ 313 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 314 | "-Dmaven.home=${M2_HOME}" \ 315 | "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 316 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 317 | --------------------------------------------------------------------------------