-
Notifications
You must be signed in to change notification settings - Fork 74
[team-08][BE] todo-list 2주차 #4 #194
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
Changes from all commits
80fe8c5
2932f0c
295a122
299b0dc
a086102
0e034f2
74fba9d
f1a14ce
3b27df2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,18 @@ | ||
| package todolist.controller; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import todolist.domain.event.Action; | ||
| import todolist.dto.card.RequestCardDto; | ||
| import todolist.dto.card.ResponseCardDto; | ||
| import todolist.dto.card.ResponseCardsDto; | ||
| import todolist.dto.event.RequestEventDto; | ||
| import todolist.service.CardService; | ||
| import todolist.service.EventService; | ||
|
|
||
| @RestController | ||
| public class CardController { | ||
|
|
||
| private final CardService cardService; | ||
| private final EventService eventService; | ||
|
|
||
| @Autowired | ||
| public CardController(CardService cardService, EventService eventService) { | ||
| public CardController(CardService cardService) { | ||
| this.cardService = cardService; | ||
| this.eventService = eventService; | ||
| } | ||
|
|
||
| @GetMapping("/todos") | ||
|
|
@@ -29,26 +22,16 @@ public ResponseCardsDto getTodo() { | |
|
|
||
| @PostMapping("/todo") | ||
| public ResponseCardDto add(@RequestBody RequestCardDto requestCardDto) { | ||
| ResponseCardDto responseCardDto = cardService.addCard(requestCardDto); | ||
| eventService.addEvent(new RequestEventDto(responseCardDto), Action.ADD); | ||
| return responseCardDto; | ||
| return cardService.addCard(requestCardDto); | ||
| } | ||
|
|
||
| @PutMapping("/todo/{id}") | ||
| public void update(@PathVariable Long id, @RequestBody RequestCardDto requestCardDto) { | ||
| String prevSection = cardService.getPrevSection(id); | ||
| ResponseCardDto responseCardDto = cardService.updateCard(id, requestCardDto); | ||
|
|
||
| if (prevSection.equals(responseCardDto.getSection())) { | ||
| eventService.addEvent(new RequestEventDto(responseCardDto), Action.UPDATE); | ||
| } else { | ||
| eventService.addEvent(new RequestEventDto(prevSection, responseCardDto), Action.MOVE); | ||
| } | ||
| cardService.updateCard(id, requestCardDto); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 와 눈물이 나는 변화로군요 💯 |
||
| } | ||
|
|
||
| @DeleteMapping("/todo/{id}") | ||
| public void delete(@PathVariable Long id) { | ||
| ResponseCardDto responseCardDto = cardService.deleteCard(id); | ||
| eventService.addEvent(new RequestEventDto(responseCardDto), Action.REMOVE); | ||
| cardService.deleteCard(id); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,8 @@ | ||
| package todolist.controller; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import todolist.domain.event.Event; | ||
| import todolist.dto.event.ResponseEventDto; | ||
| import todolist.service.EventService; | ||
|
|
||
| import java.util.List; | ||
|
|
@@ -14,13 +12,12 @@ public class EventController { | |
|
|
||
| private final EventService eventService; | ||
|
|
||
| @Autowired | ||
| public EventController(EventService eventService) { | ||
| this.eventService = eventService; | ||
| } | ||
|
|
||
| @GetMapping("/events") | ||
| public List<ResponseEventDto> getEvents() { | ||
| public List<Event> getEvents() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이번에는 도메인 객체를 직접 넘겼지만, 결국 DTO 세트를 담아서 넘겨야 하는 시간이 올 것입니다. |
||
| return eventService.getEventList(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package todolist.domain.card; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public enum Section { | ||
| TODO("해야할 일"), | ||
| DOING("하고 있는 일"), | ||
| DONE("완료된 일"); | ||
|
|
||
|
|
||
| private String sectionTitle; | ||
|
|
||
| Section(String sectionTitle) { | ||
| this.sectionTitle = sectionTitle; | ||
| } | ||
|
|
||
| public String getSectionTitle() { | ||
| return sectionTitle; | ||
| } | ||
|
|
||
| public static Map<String, List<Card>> categorizeCards(List<Card> cardlist) { | ||
|
|
||
| Map<String, List<Card>> result = new HashMap<>() {{ | ||
| put(TODO.sectionTitle, new ArrayList<>()); | ||
| put(DOING.sectionTitle, new ArrayList<>()); | ||
| put(DONE.sectionTitle, new ArrayList<>()); | ||
| }}; | ||
|
|
||
| for (Card card : cardlist) { | ||
| String section = card.getSection(); | ||
| List<Card> responseSection = result.get(findSection(section).sectionTitle); | ||
| responseSection.add(card); | ||
| } | ||
| return result; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jypthemiracle 해당 메서드는 요청에 대한 응답값으로 넘겨줄 Map객체를 반환하는 메서드입니다. 하지만 서비스에서 해당 맵객체를 ResponseCardsDto로_ Wrapping하는데 굳이 또 감쌀 필요가 있는지 잘 모르겠습니다! |
||
| } | ||
|
|
||
| private static Section findSection(String sectionTitle) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. enum에 직접 비즈니스 로직을 넣어준 점 아주 좋습니다 👍 |
||
| return Arrays.stream(Section.values()) | ||
| .filter(s -> s.getSectionTitle().equals(sectionTitle)) | ||
| .findFirst() | ||
| .orElseThrow(() -> new IllegalArgumentException()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,10 +5,10 @@ | |
| import java.util.function.BiFunction; | ||
|
|
||
| public enum Action { | ||
| ADD((dto, action) -> new Event(dto.getTitle(), dto.getCurrentSection(), action)), | ||
| MOVE((dto, action) -> new Event(dto.getTitle(),dto.getPrevSection(), dto.getCurrentSection(), action)), | ||
| UPDATE((dto, action) -> new Event(dto.getTitle(), dto.getCurrentSection(), action)), | ||
| REMOVE((dto, action) -> new Event(dto.getTitle(), dto.getCurrentSection(), action)); | ||
| ADD((dto, action) -> new Event(dto, action)), | ||
| MOVE((dto, action) -> new Event(dto, action)), | ||
| UPDATE((dto, action) -> new Event(dto, action)), | ||
| REMOVE((dto, action) -> new Event(dto, action)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 훨씬 깔끔하죠? 💯 |
||
|
|
||
|
|
||
| private BiFunction<RequestEventDto, Action, Event> eventFunction; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,31 +2,19 @@ | |
|
|
||
| import todolist.domain.card.Card; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class ResponseCardsDto { | ||
|
|
||
| private Map<String, List<ResponseCardDto>> cards; | ||
| private Map<String, List<Card>> cards; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Card를 mapping하는 object를 별도로 wrapping해서 만들 수는 없을까요? |
||
|
|
||
| public ResponseCardsDto() { | ||
| cards = new HashMap<>(); | ||
| cards.put("해야할 일", new ArrayList<>()); | ||
| cards.put("하고 있는 일", new ArrayList<>()); | ||
| cards.put("완료된 일", new ArrayList<>()); | ||
| public ResponseCardsDto(Map<String, List<Card>> cards) { | ||
| this.cards = cards; | ||
| } | ||
|
|
||
| public Map<String, List<ResponseCardDto>> getCards() { | ||
| public Map<String, List<Card>> getCards() { | ||
| return cards; | ||
| } | ||
|
|
||
| public void categorizeCards(List<Card> cardlist) { | ||
| for (Card card : cardlist) { | ||
| String section = card.getSection(); | ||
| List<ResponseCardDto> responseSection = cards.get(section); | ||
| responseSection.add(card.toResponseCardDto()); | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,52 +2,65 @@ | |
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import todolist.domain.card.Card; | ||
| import todolist.domain.card.Section; | ||
| import todolist.domain.event.Action; | ||
| import todolist.dto.card.RequestCardDto; | ||
| import todolist.dto.card.ResponseCardDto; | ||
| import todolist.dto.card.ResponseCardsDto; | ||
| import todolist.dto.event.RequestEventDto; | ||
| import todolist.repository.CardRepository; | ||
|
|
||
| import java.util.*; | ||
| import java.util.List; | ||
|
|
||
| @Service | ||
| public class CardService { | ||
|
|
||
| private final EventService eventService; | ||
| private final CardRepository<Card> repository; | ||
|
|
||
| @Autowired | ||
| public CardService(CardRepository<Card> repository) { | ||
| public CardService(EventService eventService, CardRepository<Card> repository) { | ||
| this.eventService = eventService; | ||
| this.repository = repository; | ||
| } | ||
|
|
||
| public ResponseCardsDto getCards() { | ||
| List<Card> cards = repository.findAll(); | ||
|
|
||
| ResponseCardsDto responseCardsDto = new ResponseCardsDto(); | ||
| responseCardsDto.categorizeCards(cards); | ||
| return responseCardsDto; | ||
| return new ResponseCardsDto(Section.categorizeCards(cards)); | ||
| } | ||
|
|
||
| @Transactional | ||
| public ResponseCardDto addCard(RequestCardDto requestCardDto) { | ||
| Card card = repository.save(requestCardDto.toCard()); | ||
| eventService.addEvent(new RequestEventDto(card), Action.ADD); | ||
| return card.toResponseCardDto(); | ||
| } | ||
|
|
||
| @Transactional | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이번 프로젝트의 마지막 숙제입니다. |
||
| public ResponseCardDto updateCard(Long id, RequestCardDto requestCardDto) { | ||
| Card card = repository.findById(id); | ||
| String prevSection = card.getSection(); | ||
|
|
||
| card.update(requestCardDto); | ||
| repository.update(card); | ||
|
|
||
| if (card.isSectionUpdated(prevSection)) { | ||
| eventService.addEvent(new RequestEventDto(card), Action.UPDATE); | ||
| return card.toResponseCardDto(); | ||
| } | ||
|
|
||
| eventService.addEvent(new RequestEventDto(prevSection, card), Action.MOVE); | ||
| return card.toResponseCardDto(); | ||
| } | ||
|
|
||
| @Transactional | ||
| public ResponseCardDto deleteCard(Long id) { | ||
| Card card = repository.findById(id); | ||
| repository.delete(id); | ||
| return card.toResponseCardDto(); | ||
| } | ||
|
|
||
| public String getPrevSection(Long id) { | ||
| Card card = repository.findById(id); | ||
| return card.getSection(); | ||
| eventService.addEvent(new RequestEventDto(card), Action.REMOVE); | ||
|
|
||
| return card.toResponseCardDto(); | ||
| } | ||
| } | ||
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.
서비스 객체로 책임과 역할을 분리시켜, 컨트롤러는 거들 뿐이라는 사실을 상기했습니다. 👍