Step1-상품 관리 기능#27
Conversation
Hyeon9mak
left a comment
There was a problem hiding this comment.
안녕하세요 제우님, 1단계 미션 고생하셨습니다.
최소 요구사항에 맞춘 간단한 구현!! 👍 👍 사실 이번 미션에서 꼭 DB 가 필요하진 않죠 😄 👍
그래도 유지보수 하기 쉬운 코드를 위해 코멘트를 조금 남겨보았습니다!
궁금하신 점 DM, 코멘트 남겨주세요 🙇♂️
| @Controller | ||
| @RequestMapping("/admin") | ||
| public class AdminController { | ||
|
|
There was a problem hiding this comment.
@RestController 와 @Controller 의 차이는 무엇일까요?
There was a problem hiding this comment.
@RestController 에는 @responsebody가 추가되어 있어서
view를 리턴하는게 아니라 JSON을 리턴해요.
| package cart.domain.global; | ||
|
|
||
| public class Product { | ||
| private static Integer productId = 1; |
There was a problem hiding this comment.
- ID 는 외부에서 직접 주입 받는게 좋겠어요. Product 의 관심사는 아닌 것 같아요.
- 원자성 보장을 위해 Atomic 타입을 활용해봐도 좋겠네요 👍
There was a problem hiding this comment.
Atomic 타입을 처음 들어봐서 찾아봤는데요,
(https://www.baeldung.com/java-atomic-variables)
setter 가 없으면 외부 수정이 불가능한데, Atomic 사용 이유가 무엇인가요?
There was a problem hiding this comment.
꼭 setter 를 사용하지 않더라도 생성자를 통해서 주입해줄 수 있을 것 같아요.
atomic 은 동시에 여러 요청이 전달되었을 때 원자성 보장을 위해서 사용합니다.
| @RequestMapping("/admin") | ||
| public class AdminController { | ||
|
|
||
| private final List<Product> products; |
There was a problem hiding this comment.
ID 를 통해 Product 를 조회한다... 는 마치 key 를 통해 value 를 조회한다... 와 비슷해 보이는데요? 😉
There was a problem hiding this comment.
음, 이 부분 말씀 의도를 제가 캐치를 못한거 같아요.
There was a problem hiding this comment.
ID 를 key 로 사용하고 Product 를 value 로 사용하는 Map 사용은 어떨까요?
| @RequestMapping("/admin") | ||
| public class AdminController { | ||
|
|
||
| private final List<Product> products; |
There was a problem hiding this comment.
ID 를 key 로 사용하고 Product 를 value 로 사용하는 Map 사용은 어떨까요?
| @PostMapping("/product") | ||
| public String post( | ||
| @RequestBody Product product | ||
| ){ | ||
| products.add(new Product(product.getName(), product.getImage(), product.getPrice())); | ||
| adminService.create(product); | ||
| return "admin"; | ||
| } |
There was a problem hiding this comment.
모든 요청에서 꼭 admin 페이지를 전달할 필요는 없어보여요.
페이지 뷰에 대한 요청 컨트롤러와 product 관리에 관한 요청 컨트롤러를 분리해보면 어떨까요?
| package cart.domain.global; | ||
|
|
||
| public class Product { | ||
| private static Integer productId = 1; |
There was a problem hiding this comment.
꼭 setter 를 사용하지 않더라도 생성자를 통해서 주입해줄 수 있을 것 같아요.
atomic 은 동시에 여러 요청이 전달되었을 때 원자성 보장을 위해서 사용합니다.
Hyeon9mak
left a comment
There was a problem hiding this comment.
안녕하세요 제우님! 피드백 빠르게 잘 반영해주셨습니다.
조금만 더 살펴보면 좋을 부분들 코멘트 남겼습니다.
궁금하신 점 DM, 코멘트 남겨주세요 🙇♂️
| public void create(Product product) { | ||
| products.add(new Product(products.get(products.size() - 1).getId() + 1, product.getName(), product.getImage(), product.getPrice())); | ||
| } |
There was a problem hiding this comment.
products.size 에 의존하기보다 ID 를 별개로 생성해서 전달해주는 건 어떨까요?
| public class Product { | ||
| private static Integer productId = 1; | ||
| private Integer id; | ||
| private String name; | ||
| private String image; | ||
| private Integer price; | ||
|
|
There was a problem hiding this comment.
Product 는 id 를 통해 나머지 값들의 변화를 추적하므로, id 에 대해 동등성(equals & hash code)을 부여해서 관리해줘도 좋겠네요 😄 👍
별다른 설명이 없어서,
다른 라이브러리와 DB 연동 없이 진행해봤습니다.
잘 부탁 드립니다!