From 24d63def783ece381a332a689fcee594c4fb82b5 Mon Sep 17 00:00:00 2001 From: nmadhab Date: Wed, 25 Aug 2021 20:46:26 +0200 Subject: [PATCH 1/2] initial project setup --- src/main/java/com/educative/ecommerce/EcommerceApplication.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/educative/ecommerce/EcommerceApplication.java b/src/main/java/com/educative/ecommerce/EcommerceApplication.java index 7781ae7f..dc3d06f4 100644 --- a/src/main/java/com/educative/ecommerce/EcommerceApplication.java +++ b/src/main/java/com/educative/ecommerce/EcommerceApplication.java @@ -5,9 +5,7 @@ @SpringBootApplication public class EcommerceApplication { - public static void main(String[] args) { SpringApplication.run(EcommerceApplication.class, args); } - } From c312a2b813d3d48c3c840fc0b1e6aab6b2304e87 Mon Sep 17 00:00:00 2001 From: nmadhab Date: Tue, 31 Aug 2021 23:25:28 +0200 Subject: [PATCH 2/2] category create and display working --- pom.xml | 27 +++++++++ .../ecommerce/config/SwaggerConfig.java | 39 +++++++++++++ .../educative/ecommerce/config/Webconfig.java | 22 +++++++ .../controller/CategoryController.java | 31 ++++++++++ .../educative/ecommerce/model/Category.java | 58 +++++++++++++++++++ .../ecommerce/repository/CategoryRepo.java | 10 ++++ .../ecommerce/service/CategoryService.java | 24 ++++++++ src/main/resources/application.properties | 6 +- 8 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/educative/ecommerce/config/SwaggerConfig.java create mode 100644 src/main/java/com/educative/ecommerce/config/Webconfig.java create mode 100644 src/main/java/com/educative/ecommerce/controller/CategoryController.java create mode 100644 src/main/java/com/educative/ecommerce/model/Category.java create mode 100644 src/main/java/com/educative/ecommerce/repository/CategoryRepo.java create mode 100644 src/main/java/com/educative/ecommerce/service/CategoryService.java diff --git a/pom.xml b/pom.xml index a30f3aa3..c201ae9a 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,10 @@ org.springframework.boot spring-boot-starter-data-jpa + + javax.validation + validation-api + org.springframework.boot spring-boot-starter-web @@ -31,6 +35,29 @@ spring-boot-starter-test test + + + com.h2database + h2 + runtime + + + + io.springfox + springfox-bean-validators + 2.9.2 + + + io.springfox + springfox-swagger2 + 2.9.2 + + + io.springfox + springfox-swagger-ui + 2.9.2 + + diff --git a/src/main/java/com/educative/ecommerce/config/SwaggerConfig.java b/src/main/java/com/educative/ecommerce/config/SwaggerConfig.java new file mode 100644 index 00000000..ac119642 --- /dev/null +++ b/src/main/java/com/educative/ecommerce/config/SwaggerConfig.java @@ -0,0 +1,39 @@ +package com.educative.ecommerce.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.service.Contact; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +@Configuration +@EnableSwagger2 +public class SwaggerConfig { + + @Bean + public Docket productApi() { + return new Docket(DocumentationType.SWAGGER_2) + .apiInfo(getApiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage("com.educative.ecommerce")) + .paths(PathSelectors.any()) + .build(); + } + + private ApiInfo getApiInfo() { + Contact contact = new Contact("webtutsplus", "http://webtutsplus.com", "contact.webtutsplus@gmail.com"); + return new ApiInfoBuilder() + .title("Ecommerce API") + .description("Documentation Ecommerce api") + .version("1.0.0") + .license("Apache 2.0") + .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0") + .contact(contact) + .build(); + } +} diff --git a/src/main/java/com/educative/ecommerce/config/Webconfig.java b/src/main/java/com/educative/ecommerce/config/Webconfig.java new file mode 100644 index 00000000..c4e1e0dc --- /dev/null +++ b/src/main/java/com/educative/ecommerce/config/Webconfig.java @@ -0,0 +1,22 @@ +package com.educative.ecommerce.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +class Webconfig { + + @Bean + public WebMvcConfigurer corsConfigurer() { + return new WebMvcConfigurer() { + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOrigins("*") + .allowedMethods("GET", "PUT", "POST", "PATCH", "DELETE", "OPTIONS"); + } + }; + } +} diff --git a/src/main/java/com/educative/ecommerce/controller/CategoryController.java b/src/main/java/com/educative/ecommerce/controller/CategoryController.java new file mode 100644 index 00000000..4480a84b --- /dev/null +++ b/src/main/java/com/educative/ecommerce/controller/CategoryController.java @@ -0,0 +1,31 @@ +package com.educative.ecommerce.controller; + +import com.educative.ecommerce.model.Category; +import com.educative.ecommerce.service.CategoryService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/catgory") +public class CategoryController { + + @Autowired + CategoryService categoryService; + + @PostMapping("/create") + public String createCategory(@RequestBody Category category) { + categoryService.createCategory(category); + return "success"; + } + + @GetMapping("/list") + public List listCategory() { + return categoryService.listCategory(); + } +} diff --git a/src/main/java/com/educative/ecommerce/model/Category.java b/src/main/java/com/educative/ecommerce/model/Category.java new file mode 100644 index 00000000..533b3118 --- /dev/null +++ b/src/main/java/com/educative/ecommerce/model/Category.java @@ -0,0 +1,58 @@ +package com.educative.ecommerce.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.validation.constraints.NotBlank; + +@Entity +@Table(name = "category") +public class Category { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + @Column(name = "category_name") + private @NotBlank String categoryName; + + private @NotBlank String description; + + @Column(name = "image_url") + private @NotBlank String imageUrl; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getCategoryName() { + return categoryName; + } + + public void setCategoryName(String categoryName) { + this.categoryName = categoryName; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getImageUrl() { + return imageUrl; + } + + public void setImageUrl(String imageUrl) { + this.imageUrl = imageUrl; + } +} diff --git a/src/main/java/com/educative/ecommerce/repository/CategoryRepo.java b/src/main/java/com/educative/ecommerce/repository/CategoryRepo.java new file mode 100644 index 00000000..1cf9e75b --- /dev/null +++ b/src/main/java/com/educative/ecommerce/repository/CategoryRepo.java @@ -0,0 +1,10 @@ +package com.educative.ecommerce.repository; + +import com.educative.ecommerce.model.Category; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CategoryRepo extends JpaRepository { + +} diff --git a/src/main/java/com/educative/ecommerce/service/CategoryService.java b/src/main/java/com/educative/ecommerce/service/CategoryService.java new file mode 100644 index 00000000..47054b74 --- /dev/null +++ b/src/main/java/com/educative/ecommerce/service/CategoryService.java @@ -0,0 +1,24 @@ +package com.educative.ecommerce.service; + +import com.educative.ecommerce.model.Category; +import com.educative.ecommerce.repository.CategoryRepo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class CategoryService { + + @Autowired + CategoryRepo categoryRepo; + + public void createCategory(Category category) { + categoryRepo.save(category); + } + + public List listCategory() { + return categoryRepo.findAll(); + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b137891..bc2fdde8 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,5 @@ - +spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password=password +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect