-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add controller, config, update readme
- Loading branch information
Showing
3 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/config/SwaggerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
// } | ||
//} |
51 changes: 51 additions & 0 deletions
51
ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/controller/CategoryController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters