Skip to content

Commit

Permalink
add controller, config, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 27, 2023
1 parent 801676e commit 8ff3bbc
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//package com.yen.ShoppingCart.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.yen.ShoppingCart"))
// .paths(PathSelectors.any())
// .build();
// }
//
// private ApiInfo getApiInfo() {
// Contact contact = new Contact("yen_dev", "http://yen_dev.com", "[email protected]");
// 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();
// }
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.yen.ShoppingCart.controller;

import com.yen.ShoppingCart.common.ApiResponse;
import com.yen.ShoppingCart.model.Category;
import com.yen.ShoppingCart.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.*;

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(@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, @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);
}

}
2 changes: 1 addition & 1 deletion ShoppingCart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ java -jar <built_jar>

| API | Type | Purpose | Example cmd | Comment|
| ----- | -------- | ---- | ----- | ---- |
| Test | | | |
| http://localhost:9999/swagger-ui.html | | | |



Expand Down

0 comments on commit 8ff3bbc

Please sign in to comment.