diff --git a/src/main/java/com/moment/the/uncomfortable/UncomfortableEntity.java b/src/main/java/com/moment/the/uncomfortable/UncomfortableEntity.java index f308c156..a1c1c052 100644 --- a/src/main/java/com/moment/the/uncomfortable/UncomfortableEntity.java +++ b/src/main/java/com/moment/the/uncomfortable/UncomfortableEntity.java @@ -21,7 +21,6 @@ public class UncomfortableEntity { private String content; @Column private int goods; - @OneToOne(fetch = LAZY, cascade = CascadeType.ALL) private AnswerDomain answerDomain; diff --git a/src/main/java/com/moment/the/uncomfortable/controller/UncomfortableController.java b/src/main/java/com/moment/the/uncomfortable/controller/UncomfortableController.java index ee41a804..6bbd2a29 100644 --- a/src/main/java/com/moment/the/uncomfortable/controller/UncomfortableController.java +++ b/src/main/java/com/moment/the/uncomfortable/controller/UncomfortableController.java @@ -16,59 +16,95 @@ @RequiredArgsConstructor @RequestMapping("/v1") public class UncomfortableController { - //Dependency Injection + private final UncomfortableService uncomfortableService; private final ResponseService responseService; - // localhost:8080/v1/uncomfortable + /** + * 학교의 불편함을 작성합니다. + * @param uncomfortableSetDto + * @return getSuccessResult + * @author 전지환, 정시원 + */ @PostMapping("/uncomfortable") - public CommonResult write(@Valid @RequestBody UncomfortableSetDto uncomfortableSetDto){ - uncomfortableService.write(uncomfortableSetDto); + public CommonResult addUncomfortable(@Valid @RequestBody UncomfortableSetDto uncomfortableSetDto){ + uncomfortableService.addUncomfortable(uncomfortableSetDto); return responseService.getSuccessResult(); } - // localhost:8080/v1/uncomfortable/top30 - @GetMapping("/uncomfortable/top30") - public ListResult top10(){ - return responseService.getListResult(uncomfortableService.top30View()); + /** + * 많은 학생들이 공감한 글 상위 30개를 선별하여 가져옵니다. + * @return getListResult + * @author 전지환, 정시원 + */ + @GetMapping("/uncomfortable/rank") + public ListResult getTop30(){ + return responseService.getListResult(uncomfortableService.getTop30()); } - // localhost:8080/v1/uncomfortable + /** + * 학교의 불편함 전체를 가져옵니다. + * @return getListResult + * @author 전지환, 정시원 + */ @GetMapping("/uncomfortable") - public ListResult viewAll(){ - return responseService.getListResult(uncomfortableService.viewAll()); + public ListResult getAllUncomfortable(){ + return responseService.getListResult(uncomfortableService.getAllUncomfortable()); } - // localhost:8080/v1/uncomfortable/{boardIdx} - @PutMapping("/uncomfortable/{boardIdx}") - public CommonResult goods(@PathVariable Long boardIdx){ - uncomfortableService.goods(boardIdx); + /** + * 해당 불편함의 좋아요를 증가시킵니다. + * @param boardIdx + * @return getSuccessResult + * @author 전지환, 정시원 + */ + @PutMapping("/uncomfortable/like/increase/{boardIdx}") + public CommonResult increaseLike(@PathVariable Long boardIdx){ + uncomfortableService.increaseLike(boardIdx); return responseService.getSuccessResult(); } - // localhost:8080/v1/uncomfortable/cancel/{boardIdx} - @PutMapping("/uncomfortable/cancel/{boardIdx}") - public CommonResult cancelGood(@PathVariable Long boardIdx){ - uncomfortableService.cancelGood(boardIdx); + /** + * 해당 불편함의 좋아요를 감소시킵니다. + * @param boardIdx + * @return getSuccessResult + * @author 전지환, 정시원 + */ + @PutMapping("/uncomfortable/like/decrease/{boardIdx}") + public CommonResult decreaseLike(@PathVariable Long boardIdx){ + uncomfortableService.decreaseLike(boardIdx); + return responseService.getSuccessResult(); + } + + /** + * 해당 불편함을 삭제합니다. + * @param boardIdx + * @return getSuccessResult + * @author 전지환, 정시원 + */ + @DeleteMapping("/uncomfortable/{boardIdx}") + public CommonResult deleteUncomfortable(@PathVariable Long boardIdx){ + uncomfortableService.deleteUncomfortable(boardIdx); return responseService.getSuccessResult(); } - // localhost:8080/v1/uncomfortable/amount + /** + * 불편함의 개수를 세어 가져옵니다. + * @return getSingleResult + * @author 전지환, 정시원 + */ @GetMapping("/uncomfortable/amount") - public SingleResult amountUncomfortable(){ - return responseService.getSingleResult(uncomfortableService.amountUncomfortableView()); + public SingleResult getNumberOfUncomfortable(){ + return responseService.getSingleResult(uncomfortableService.getNumberOfUncomfortable()); } - // localhost:8080/v1/uncomfortable/dateSinceProjectStart + /** + * 프로젝트 D-day를 세어 가져옵니다. + * @return getSingleResult + * @author 전지환, 정시원 + */ @GetMapping("/uncomfortable/dateSinceProjectStart") public SingleResult getDateSinceProjectStart(){ - return responseService.getSingleResult(uncomfortableService.dateSinceProjectStart()); - } - - // localhost:8080/v1/uncomfortable/{boardIdx} - @DeleteMapping("/uncomfortable/{boardIdx}") - public CommonResult deleteThisBoard(@PathVariable Long boardIdx){ - uncomfortableService.delete(boardIdx); - return responseService.getSuccessResult(); + return responseService.getSingleResult(uncomfortableService.getDateSinceProjectStart()); } } diff --git a/src/main/java/com/moment/the/uncomfortable/repository/UncomfortableRepository.java b/src/main/java/com/moment/the/uncomfortable/repository/UncomfortableRepository.java index cc3f5d5b..af10c0ee 100644 --- a/src/main/java/com/moment/the/uncomfortable/repository/UncomfortableRepository.java +++ b/src/main/java/com/moment/the/uncomfortable/repository/UncomfortableRepository.java @@ -12,7 +12,7 @@ @Repository public interface UncomfortableRepository extends JpaRepository{ - // idx로 uncomfortable 찾기. + Optional findByBoardIdx(Long boardIdx); @Query(value = "SELECT COUNT(table.boardIdx) " + diff --git a/src/main/java/com/moment/the/uncomfortable/service/UncomfortableService.java b/src/main/java/com/moment/the/uncomfortable/service/UncomfortableService.java index 030ad8c0..68d6e159 100644 --- a/src/main/java/com/moment/the/uncomfortable/service/UncomfortableService.java +++ b/src/main/java/com/moment/the/uncomfortable/service/UncomfortableService.java @@ -22,42 +22,48 @@ public class UncomfortableService { private final UncomfortableRepository uncomfortableRepository; - // 작성하기. + /** + * 학교의 불편함을 작성합니다. + * @param uncomfortableSetDto + * @return UncomfortableEntity + */ @Transactional - public UncomfortableEntity write(UncomfortableSetDto uncomfortableSetDto){ + public UncomfortableEntity addUncomfortable(UncomfortableSetDto uncomfortableSetDto){ return uncomfortableRepository.save(uncomfortableSetDto.toEntity()); } - // Top 30 보여주기. - public List top30View() { + /** + * 많은 학생들이 공감한 글 상위 30개를 선별하여 가져옵니다. + * @return List + */ + public List getTop30() { return uncomfortableRepository.uncomfortableViewTopBy(PageRequest.of(0,30)); } - // 전체 페이지 보여주기. - public List viewAll(){ + /** + * 학교의 불편함 전체를 가져옵니다. + * @return List + */ + public List getAllUncomfortable(){ return uncomfortableRepository.uncomfortableViewAll(); } - // 전체 게시물 개수 보여주기. - public Long amountUncomfortableView(){ - return uncomfortableRepository.amountUncomfortable(); - } - - // 프로젝트 시작 이후 날짜 보여주기. - public int dateSinceProjectStart(){ - return calculateAfterDate(); - } - - // 좋아요 수 증가. + /** + * 해당 불편함의 좋아요를 증가시킵니다. + * @param boardIdx + */ @Transactional - public void goods(Long boardIdx){ + public void increaseLike(Long boardIdx){ UncomfortableEntity uncomfortableEntity = uncomfortableRepository.findByBoardIdx(boardIdx).orElseThrow(NoPostException::new); uncomfortableEntity.updateGoods(uncomfortableEntity.getGoods()+1); } - // 좋아요 수 감소. + /** + * 해당 불편함의 좋아요를 감소시킵니다. + * @param boardIdx + */ @Transactional - public void cancelGood(Long boardIdx) { + public void decreaseLike(Long boardIdx) { UncomfortableEntity uncomfortableEntity = uncomfortableRepository.findByBoardIdx(boardIdx).orElseThrow(NoPostException::new); int goodsResult = uncomfortableEntity.getGoods() - 1; @@ -68,17 +74,38 @@ public void cancelGood(Long boardIdx) { } } + /** + * 해당 불편함을 삭제합니다. + * @param boardIdx + */ @Transactional - public void delete(long boardIdx){ + public void deleteUncomfortable(long boardIdx){ uncomfortableRepository.deleteById(boardIdx); } - // day 수 계산하기 + /** + * 불편함의 개수를 세어 가져옵니다. + * @return Long + */ + public Long getNumberOfUncomfortable(){ + return uncomfortableRepository.amountUncomfortable(); + } + + /** + * 프로젝트 D-day를 세어 가져옵니다. + * @return int + */ + public int getDateSinceProjectStart(){ + return calculateAfterDate(); + } + + /** + * D-day를 계산하는 메서드. + * @return int + */ public static int calculateAfterDate(){ - /** - * today: 오늘 날짜 - * theMomentStart: the-moment 시작 날짜 - */ + // today: 오늘 날짜 + // theMomentStart: the-moment 시작 날짜 LocalDate today = LocalDate.now(); LocalDate theMomentStart = LocalDate.of(2021, 6, 7); diff --git a/src/test/java/com/moment/the/controller/release/UncomfortableControllerTest.java b/src/test/java/com/moment/the/controller/release/UncomfortableControllerTest.java index b4026918..6c584993 100644 --- a/src/test/java/com/moment/the/controller/release/UncomfortableControllerTest.java +++ b/src/test/java/com/moment/the/controller/release/UncomfortableControllerTest.java @@ -134,7 +134,7 @@ String objectToJson(Object object) throws JsonProcessingException { ).limit(40).collect(Collectors.toList()); tableRepo.saveAll(uncomfortableEntities); - List uncomfortableGetDtos = uncomfortableService.top30View(); + List uncomfortableGetDtos = uncomfortableService.getTop30(); String top30Data = objectToJson(uncomfortableGetDtos); //When @@ -159,7 +159,7 @@ String objectToJson(Object object) throws JsonProcessingException { //When resultActions = mockMvc.perform( - put("/v1/uncomfortable/" + tableIdx.longValue()) + put("/v1/uncomfortable/like/increase/" + tableIdx.longValue()) .contentType(MediaType.APPLICATION_JSON) ); @@ -181,7 +181,7 @@ String objectToJson(Object object) throws JsonProcessingException { //When resultActions = mockMvc.perform( - put("/v1/uncomfortable/cancel/" + tableIdx.longValue()) + put("/v1/uncomfortable/like/decrease/" + tableIdx.longValue()) .contentType(MediaType.APPLICATION_JSON) ); diff --git a/src/test/java/com/moment/the/service/AnswerServiceTest.java b/src/test/java/com/moment/the/service/AnswerServiceTest.java index 556f053b..710ccafe 100644 --- a/src/test/java/com/moment/the/service/AnswerServiceTest.java +++ b/src/test/java/com/moment/the/service/AnswerServiceTest.java @@ -68,7 +68,7 @@ AdminDomain adminLogin(String adminId, String password) { UncomfortableEntity createTable(){ String TABLE_CONTENT = "급식이 맛이 없어요 급식에 질을 높여주세요!"; UncomfortableSetDto uncomfortableSetDto = new UncomfortableSetDto(TABLE_CONTENT); - UncomfortableEntity uncomfortableEntity = uncomfortableService.write(uncomfortableSetDto); + UncomfortableEntity uncomfortableEntity = uncomfortableService.addUncomfortable(uncomfortableSetDto); return uncomfortableEntity; } diff --git a/src/test/java/com/moment/the/service/UncomfortableServiceTest.java b/src/test/java/com/moment/the/service/UncomfortableServiceTest.java index 0c8e49cd..18f3b1e7 100644 --- a/src/test/java/com/moment/the/service/UncomfortableServiceTest.java +++ b/src/test/java/com/moment/the/service/UncomfortableServiceTest.java @@ -46,7 +46,7 @@ public void cleanUp(){ .build(); // when - UncomfortableEntity writeTable = uncomfortableService.write(uncomfortableSetDto); + UncomfortableEntity writeTable = uncomfortableService.addUncomfortable(uncomfortableSetDto); UncomfortableEntity savedTable = tableRepo.findByBoardIdx(writeTable.getBoardIdx()).orElseThrow(() -> new IllegalArgumentException("Table을 찾을 수 없습니다. (테스트실패)")); tableRepo.delete(savedTable); @@ -68,7 +68,7 @@ public void cleanUp(){ // When tableRepo.saveAll(uncomfortableEntities); - List viewTop30 = uncomfortableService.top30View(); + List viewTop30 = uncomfortableService.getTop30(); // Then assertEquals(viewTop30.size(), 30); @@ -93,7 +93,7 @@ public void cleanUp(){ // When tableRepo.saveAll(uncomfortableEntities); - List tableViewAll = uncomfortableService.viewAll(); + List tableViewAll = uncomfortableService.getAllUncomfortable(); // Then assertEquals(tableViewAll.size(), 10); // 10개를 저장했으므로 tableViewAll 의 개수는 10개여야 한다. @@ -113,7 +113,7 @@ public void cleanUp(){ // When tableRepo.saveAll(uncomfortableEntities); - Long amountUncomfortable = uncomfortableService.amountUncomfortableView(); + Long amountUncomfortable = uncomfortableService.getNumberOfUncomfortable(); // then assertEquals(amountUncomfortable, 10); @@ -130,7 +130,7 @@ public void cleanUp(){ Period period = startTheMoment.until(currentDate); // Then - assertEquals(uncomfortableService.dateSinceProjectStart(), period.getDays()+1);; + assertEquals(uncomfortableService.getDateSinceProjectStart(), period.getDays()+1);; } @Test @@ -143,7 +143,7 @@ public void cleanUp(){ // When UncomfortableEntity savedUncomfortableEntity = tableRepo.save(uncomfortableEntity); - uncomfortableService.goods(savedUncomfortableEntity.getBoardIdx()); + uncomfortableService.increaseLike(savedUncomfortableEntity.getBoardIdx()); UncomfortableEntity savedGoodsUncomfortableEntity = tableRepo.findByBoardIdx(savedUncomfortableEntity.getBoardIdx()).orElseThrow(() -> new IllegalArgumentException("좋아요를 받은 TableEntity를 찾을 수 없습니다.")); // Then @@ -161,7 +161,7 @@ public void cleanUp(){ // When UncomfortableEntity savedUncomfortableEntity = tableRepo.save(uncomfortableEntity); - uncomfortableService.cancelGood(savedUncomfortableEntity.getBoardIdx()); + uncomfortableService.decreaseLike(savedUncomfortableEntity.getBoardIdx()); UncomfortableEntity savedCancelGoodUncomfortableEntity = tableRepo.findByBoardIdx(savedUncomfortableEntity.getBoardIdx()).orElseThrow(() -> new IllegalArgumentException("좋아요를 취소한 TableEntity를 찾을 수 없습니다.")); // Given @@ -182,7 +182,7 @@ public void cleanUp(){ System.out.println(savedUncomfortableEntity.getBoardIdx()); assertThrows(GoodsNotCancelException.class, () ->{ - uncomfortableService.cancelGood(savedUncomfortableEntity.getBoardIdx()); + uncomfortableService.decreaseLike(savedUncomfortableEntity.getBoardIdx()); }); }