Skip to content
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
merged 4 commits into from
Feb 15, 2023
Merged
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
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.bbaemin.category.controller.request;

import lombok.Builder;
import lombok.Getter;

@Getter
public class CreateCategoryRequest {

private String code;
private Integer code;
private String name;
private String description;

Expand Down
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; // 상위 카테고리
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
@Builder
public class CategoryResponse {

private String code;
private Long categoryId;

private Integer code;
private String name;
private String description;

private String parent;
private Integer parent;
}
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;
}
}
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 {
Copy link
Collaborator

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가 다루는 데이터들이 달라야 하지 않을까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VO 하나 만들어서 데이터 파싱 후 service, repository단에서 처리하도록 하겠습니다.


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 bbaemin-api/src/main/java/org/bbaemin/category/vo/Category.java
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;
}
Loading