diff --git a/data b/data new file mode 100644 index 00000000..e69de29b diff --git a/pom.xml b/pom.xml index a30f3aa3..a2809d55 100644 --- a/pom.xml +++ b/pom.xml @@ -31,6 +31,34 @@ spring-boot-starter-test test + + + javax.validation + validation-api + + + + io.springfox + springfox-bean-validators + 2.9.2 + + + io.springfox + springfox-swagger2 + 2.9.2 + + + io.springfox + springfox-swagger-ui + 2.9.2 + + + + com.h2database + h2 + runtime + + diff --git a/src/main/java/com/educative/ecommerce/common/ApiResponse.java b/src/main/java/com/educative/ecommerce/common/ApiResponse.java new file mode 100644 index 00000000..55f90a2f --- /dev/null +++ b/src/main/java/com/educative/ecommerce/common/ApiResponse.java @@ -0,0 +1,25 @@ +package com.educative.ecommerce.common; + +import java.time.LocalDateTime; + +public class ApiResponse { + private final boolean success; + private final String message; + + public ApiResponse(boolean success, String message) { + this.success = success; + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public String getMessage() { + return message; + } + + public String getTimestamp() { + return LocalDateTime.now().toString(); + } +} 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..999899bb --- /dev/null +++ b/src/main/java/com/educative/ecommerce/config/SwaggerConfig.java @@ -0,0 +1,38 @@ +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/controllers/CategoryController.java b/src/main/java/com/educative/ecommerce/controllers/CategoryController.java new file mode 100644 index 00000000..706b0d67 --- /dev/null +++ b/src/main/java/com/educative/ecommerce/controllers/CategoryController.java @@ -0,0 +1,56 @@ +package com.educative.ecommerce.controllers; + + +import com.educative.ecommerce.common.ApiResponse; +import com.educative.ecommerce.model.Category; +import com.educative.ecommerce.service.CategoryService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +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 javax.validation.Valid; +import java.util.List; +import java.util.Objects; + +@RestController +@RequestMapping("/category") + +public class CategoryController { + + @Autowired + private CategoryService categoryService; + + @GetMapping("/") + public ResponseEntity> getCategories() { + List body = categoryService.listCategories(); + return new ResponseEntity<>(body, HttpStatus.OK); + } + + @PostMapping("/create") + public ResponseEntity createCategory(@Valid @RequestBody Category category) { + if (Objects.nonNull(categoryService.readCategory(category.getCategoryName()))) { + return new ResponseEntity(new ApiResponse(false, "category already exists"), HttpStatus.CONFLICT); + } + categoryService.createCategory(category); + return new ResponseEntity<>(new ApiResponse(true, "created the category"), HttpStatus.CREATED); + } + + @PostMapping("/update/{categoryID}") + public ResponseEntity updateCategory(@PathVariable("categoryID") Integer categoryID, @Valid @RequestBody Category category) { + // Check to see if the category exists. + if (Objects.nonNull(categoryService.readCategory(categoryID))) { + // If the category exists then update it. + categoryService.updateCategory(categoryID, category); + return new ResponseEntity(new ApiResponse(true, "updated the category"), HttpStatus.OK); + } + + // If the category doesn't exist then return a response of unsuccessful. + return new ResponseEntity<>(new ApiResponse(false, "category does not exist"), HttpStatus.NOT_FOUND); + } +} 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..2051d915 --- /dev/null +++ b/src/main/java/com/educative/ecommerce/model/Category.java @@ -0,0 +1,81 @@ +package com.educative.ecommerce.model; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.Table; +import javax.validation.constraints.NotBlank; +import java.util.Set; + +@Entity +@Table(name = "categories") +public class Category { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + @Column(name = "category_name") + private @NotBlank String categoryName; + + private @NotBlank String description; + + private @NotBlank String imageUrl; + + + public Category() { + } + + public Category(@NotBlank String categoryName, @NotBlank String description) { + this.categoryName = categoryName; + this.description = description; + } + + public Category(@NotBlank String categoryName, @NotBlank String description, @NotBlank String imageUrl) { + this.categoryName = categoryName; + this.description = description; + this.imageUrl = imageUrl; + } + + public String getCategoryName() { + return this.categoryName; + } + + public void setCategoryName(String categoryName) { + this.categoryName = categoryName; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + @Override + public String toString() { + return "User {category id=" + id + ", category name='" + categoryName + "', description='" + description + "'}"; + } + + public String getImageUrl() { + return imageUrl; + } + + public void setImageUrl(String imageUrl) { + this.imageUrl = imageUrl; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } +} diff --git a/src/main/java/com/educative/ecommerce/repository/Categoryrepository.java b/src/main/java/com/educative/ecommerce/repository/Categoryrepository.java new file mode 100644 index 00000000..49344378 --- /dev/null +++ b/src/main/java/com/educative/ecommerce/repository/Categoryrepository.java @@ -0,0 +1,12 @@ +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 Categoryrepository extends JpaRepository { + + Category findByCategoryName(String categoryName); + +} 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..2b3013d2 --- /dev/null +++ b/src/main/java/com/educative/ecommerce/service/CategoryService.java @@ -0,0 +1,41 @@ +package com.educative.ecommerce.service; + +import com.educative.ecommerce.model.Category; +import com.educative.ecommerce.repository.Categoryrepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.transaction.Transactional; +import java.util.List; +import java.util.Optional; + +@Service +public class CategoryService { + + @Autowired + private Categoryrepository categoryrepository; + + public List listCategories() { + return categoryrepository.findAll(); + } + + public void createCategory(Category category) { + categoryrepository.save(category); + } + + public Category readCategory(String categoryName) { + return categoryrepository.findByCategoryName(categoryName); + } + + public Optional readCategory(Integer categoryId) { + return categoryrepository.findById(categoryId); + } + + public void updateCategory(Integer categoryID, Category newCategory) { + Category category = categoryrepository.findById(categoryID).get(); + category.setCategoryName(newCategory.getCategoryName()); + category.setDescription(newCategory.getDescription()); + category.setImageUrl(newCategory.getImageUrl()); + categoryrepository.save(category); + } +} 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