Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added data
Empty file.
28 changes: 28 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,34 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

</dependencies>

<build>
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/educative/ecommerce/common/ApiResponse.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/educative/ecommerce/config/SwaggerConfig.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/educative/ecommerce/config/Webconfig.java
Original file line number Diff line number Diff line change
@@ -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");
}
};
}
}
Original file line number Diff line number Diff line change
@@ -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<List<Category>> getCategories() {
List<Category> body = categoryService.listCategories();
return new ResponseEntity<>(body, HttpStatus.OK);
}

@PostMapping("/create")
public ResponseEntity<ApiResponse> createCategory(@Valid @RequestBody Category category) {
if (Objects.nonNull(categoryService.readCategory(category.getCategoryName()))) {
return new ResponseEntity<ApiResponse>(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<ApiResponse> 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<ApiResponse>(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);
}
}
81 changes: 81 additions & 0 deletions src/main/java/com/educative/ecommerce/model/Category.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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, Integer> {

Category findByCategoryName(String categoryName);

}
41 changes: 41 additions & 0 deletions src/main/java/com/educative/ecommerce/service/CategoryService.java
Original file line number Diff line number Diff line change
@@ -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<Category> listCategories() {
return categoryrepository.findAll();
}

public void createCategory(Category category) {
categoryrepository.save(category);
}

public Category readCategory(String categoryName) {
return categoryrepository.findByCategoryName(categoryName);
}

public Optional<Category> 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);
}
}
6 changes: 5 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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