-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Category service, repository #18
Merged
yeonkyungJoo
merged 4 commits into
f-lab-edu:main
from
CHANEE-personal:CATEGORY-SERVICE
Feb 15, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
59 changes: 30 additions & 29 deletions
59
bbaemin-api/src/main/java/org/bbaemin/category/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 |
---|---|---|
@@ -1,57 +1,58 @@ | ||
package org.bbaemin.category.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.bbaemin.category.controller.request.CreateCategoryRequest; | ||
import org.bbaemin.category.controller.request.UpdateCategoryRequest; | ||
import org.bbaemin.category.controller.response.CategoryResponse; | ||
import org.bbaemin.category.service.CategoryService; | ||
import org.bbaemin.category.vo.Category; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v1/category") | ||
public class CategoryController { | ||
|
||
private final CategoryService categoryService; | ||
|
||
@GetMapping | ||
public List<CategoryResponse> listCategory() { | ||
return List.of(CategoryResponse.builder() | ||
.code("100") | ||
.name("돼지고기") | ||
.description("돼지고기") | ||
.parent("육류") | ||
.build()); | ||
return categoryService.listCategory(); | ||
} | ||
|
||
@GetMapping("/{idx}") | ||
public CategoryResponse getCategory(@PathVariable Long idx) { | ||
return CategoryResponse.builder() | ||
.code("100") | ||
.name("돼지고기") | ||
.description("돼지고기") | ||
.parent("육류") | ||
.build(); | ||
@GetMapping("/{categoryId}") | ||
public CategoryResponse getCategory(@PathVariable Long categoryId) { | ||
return categoryService.getCategory(categoryId); | ||
} | ||
|
||
@PostMapping | ||
public CategoryResponse createCategory(@RequestBody CreateCategoryRequest createCategoryRequest) { | ||
return CategoryResponse.builder() | ||
.code("100") | ||
.name("돼지고기") | ||
.description("돼지고기") | ||
.parent("육류") | ||
Category category = Category.builder() | ||
.code(createCategoryRequest.getCode()) | ||
.name(createCategoryRequest.getName()) | ||
.description(createCategoryRequest.getDescription()) | ||
.parentId(createCategoryRequest.getParentId()) | ||
.build(); | ||
|
||
return categoryService.createCategory(category); | ||
} | ||
|
||
@PutMapping("/{idx}") | ||
public CategoryResponse updateCategory(@PathVariable Long idx) { | ||
return CategoryResponse.builder() | ||
.code("100") | ||
.name("소고기") | ||
.description("소고기") | ||
.parent("육류") | ||
@PutMapping("/{categoryId}") | ||
public CategoryResponse updateCategory(@PathVariable Long categoryId, @RequestBody UpdateCategoryRequest updateCategoryRequest) { | ||
Category category = Category.builder() | ||
.code(updateCategoryRequest.getCode()) | ||
.name(updateCategoryRequest.getName()) | ||
.description(updateCategoryRequest.getDescription()) | ||
.parentId(updateCategoryRequest.getParentId()) | ||
.build(); | ||
|
||
return categoryService.updateCategory(categoryId, category); | ||
} | ||
|
||
@DeleteMapping("/{idx}") | ||
public Long deleteCategory(@PathVariable Long idx) { | ||
return idx; | ||
@DeleteMapping("/{categoryId}") | ||
public Long deleteCategory(@PathVariable Long categoryId) { | ||
return categoryService.deleteCategory(categoryId); | ||
} | ||
} |
6 changes: 5 additions & 1 deletion
6
bbaemin-api/src/main/java/org/bbaemin/category/controller/request/CreateCategoryRequest.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
13 changes: 13 additions & 0 deletions
13
bbaemin-api/src/main/java/org/bbaemin/category/controller/request/UpdateCategoryRequest.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,13 @@ | ||
package org.bbaemin.category.controller.request; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class UpdateCategoryRequest { | ||
|
||
private Integer code; | ||
private String name; | ||
private String description; | ||
|
||
private Long parentId; // 상위 카테고리 | ||
} |
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
123 changes: 123 additions & 0 deletions
123
bbaemin-api/src/main/java/org/bbaemin/category/repository/CategoryRepository.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,123 @@ | ||
package org.bbaemin.category.repository; | ||
|
||
import org.bbaemin.category.controller.response.CategoryResponse; | ||
import org.bbaemin.category.vo.Category; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Repository | ||
public class CategoryRepository { | ||
|
||
private static final List<CategoryResponse> categoryList = new ArrayList<>(); | ||
private static Long categoryId = 0L; | ||
|
||
static { | ||
categoryList.add( | ||
CategoryResponse.builder() | ||
.categoryId(++categoryId) | ||
.code(100) | ||
.name("육류") | ||
.description("육류") | ||
.parent(null) | ||
.build() | ||
); | ||
|
||
categoryList.add( | ||
CategoryResponse.builder() | ||
.categoryId(++categoryId) | ||
.code(101) | ||
.name("돼지고기") | ||
.description("돼지고기") | ||
.parent(100) | ||
.build() | ||
); | ||
} | ||
|
||
public CategoryResponse oneCategory(Long categoryId) { | ||
return categoryList.stream().filter(category -> category.getCategoryId().equals(categoryId)) | ||
.findFirst().orElseThrow(); | ||
} | ||
|
||
/** | ||
* <pre> | ||
* 1. MethodName : findAll | ||
* 2. ClassName : CategoryRepository.java | ||
* 3. Comment : 카테고리 리스트 조회 | ||
* 4. 작성자 : CHO | ||
* 5. 작성일 : 2023. 02. 08. | ||
* </pre> | ||
*/ | ||
public List<CategoryResponse> findAll() { | ||
return categoryList; | ||
} | ||
|
||
/** | ||
* <pre> | ||
* 1. MethodName : findById | ||
* 2. ClassName : CategoryRepository.java | ||
* 3. Comment : 카테고리 상세 조회 | ||
* 4. 작성자 : CHO | ||
* 5. 작성일 : 2023. 02. 08. | ||
* </pre> | ||
*/ | ||
public CategoryResponse findById(Long categoryId) { | ||
return oneCategory(categoryId); | ||
} | ||
|
||
/** | ||
* <pre> | ||
* 1. MethodName : save | ||
* 2. ClassName : CategoryRepository.java | ||
* 3. Comment : 카테고리 등록 | ||
* 4. 작성자 : CHO | ||
* 5. 작성일 : 2023. 02. 08. | ||
* </pre> | ||
*/ | ||
public CategoryResponse save(Category category) { | ||
return CategoryResponse.builder() | ||
.categoryId(++categoryId) | ||
.code(category.getCode()) | ||
.name(category.getName()) | ||
.description(category.getDescription()) | ||
.parent(category.getCode()) | ||
.build(); | ||
} | ||
|
||
/** | ||
* <pre> | ||
* 1. MethodName : update | ||
* 2. ClassName : CategoryRepository.java | ||
* 3. Comment : 카테고리 수정 | ||
* 4. 작성자 : CHO | ||
* 5. 작성일 : 2023. 02. 08. | ||
* </pre> | ||
*/ | ||
public CategoryResponse update(Long categoryId, Category category) { | ||
return CategoryResponse.builder() | ||
.categoryId(categoryId) | ||
.code(category.getCode()) | ||
.name(category.getName()) | ||
.description(category.getDescription()) | ||
.parent(category.getCode()) | ||
.build(); | ||
} | ||
|
||
/** | ||
* <pre> | ||
* 1. MethodName : deleteById | ||
* 2. ClassName : CategoryRepository.java | ||
* 3. Comment : 카테고리 삭제 | ||
* 4. 작성자 : CHO | ||
* 5. 작성일 : 2023. 02. 08. | ||
* </pre> | ||
*/ | ||
public Long deleteById(Long categoryId) { | ||
categoryList.stream().filter(category -> category.getCategoryId().equals(categoryId)) | ||
.collect(Collectors.toList()).remove(0); | ||
|
||
return categoryId; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
bbaemin-api/src/main/java/org/bbaemin/category/service/CategoryService.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,81 @@ | ||
package org.bbaemin.category.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.bbaemin.category.controller.response.CategoryResponse; | ||
import org.bbaemin.category.repository.CategoryRepository; | ||
import org.bbaemin.category.vo.Category; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CategoryService { | ||
|
||
private final CategoryRepository categoryRepository; | ||
|
||
/** | ||
* <pre> | ||
* 1. MethodName : listCategory | ||
* 2. ClassName : CategoryService.java | ||
* 3. Comment : 카테고리 리스트 조회 | ||
* 4. 작성자 : CHO | ||
* 5. 작성일 : 2023. 02. 08. | ||
* </pre> | ||
*/ | ||
public List<CategoryResponse> listCategory() { | ||
return categoryRepository.findAll(); | ||
} | ||
|
||
/** | ||
* <pre> | ||
* 1. MethodName : getCategory | ||
* 2. ClassName : CategoryService.java | ||
* 3. Comment : 카테고리 상세 조회 | ||
* 4. 작성자 : CHO | ||
* 5. 작성일 : 2023. 02. 08. | ||
* </pre> | ||
*/ | ||
public CategoryResponse getCategory(Long categoryId) { | ||
return categoryRepository.findById(categoryId); | ||
} | ||
|
||
/** | ||
* <pre> | ||
* 1. MethodName : createCategory | ||
* 2. ClassName : CategoryService.java | ||
* 3. Comment : 카테고리 등록 | ||
* 4. 작성자 : CHO | ||
* 5. 작성일 : 2023. 02. 08. | ||
* </pre> | ||
*/ | ||
public CategoryResponse createCategory(Category category) { | ||
return categoryRepository.save(category); | ||
} | ||
|
||
/** | ||
* <pre> | ||
* 1. MethodName : updateCategory | ||
* 2. ClassName : CategoryService.java | ||
* 3. Comment : 카테고리 수정 | ||
* 4. 작성자 : CHO | ||
* 5. 작성일 : 2023. 02. 08. | ||
* </pre> | ||
*/ | ||
public CategoryResponse updateCategory(Long categoryId, Category category) { | ||
return categoryRepository.update(categoryId, category); | ||
} | ||
|
||
/** | ||
* <pre> | ||
* 1. MethodName : deleteCategory | ||
* 2. ClassName : CategoryService.java | ||
* 3. Comment : 카테고리 삭제 | ||
* 4. 작성자 : CHO | ||
* 5. 작성일 : 2023. 02. 08. | ||
* </pre> | ||
*/ | ||
public Long deleteCategory(Long categoryId) { | ||
return categoryRepository.deleteById(categoryId); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
bbaemin-api/src/main/java/org/bbaemin/category/vo/Category.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,17 @@ | ||
package org.bbaemin.category.vo; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class Category { | ||
|
||
private Long categoryId; | ||
|
||
private Integer code; | ||
private String name; | ||
private String description; | ||
|
||
private Long parentId; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
위의 Repository에도 비슷한 리뷰를 남기긴 했습니다만, Service에서 다루는 데이터와 Repository, Controller에서 다루는 데이터들간에 차이가 있어야 하지 않을까요? 차이라기 보다는 각각의 Layer가 다루는 데이터들이 달라야 하지 않을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
VO 하나 만들어서 데이터 파싱 후 service, repository단에서 처리하도록 하겠습니다.