Skip to content

Commit

Permalink
Merge pull request #193 from themoment-team/feature/rename
Browse files Browse the repository at this point in the history
Feature/rename - uncomfortable
  • Loading branch information
siwony authored Sep 7, 2021
2 parents a5b6602 + e65fd08 commit 062819f
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class UncomfortableEntity {
private String content;
@Column
private int goods;

@OneToOne(fetch = LAZY, cascade = CascadeType.ALL)
private AnswerDomain answerDomain;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<UncomfortableGetDto> top10(){
return responseService.getListResult(uncomfortableService.top30View());
/**
* 많은 학생들이 공감한 글 상위 30개를 선별하여 가져옵니다.
* @return getListResult
* @author 전지환, 정시원
*/
@GetMapping("/uncomfortable/rank")
public ListResult<UncomfortableGetDto> getTop30(){
return responseService.getListResult(uncomfortableService.getTop30());
}

// localhost:8080/v1/uncomfortable
/**
* 학교의 불편함 전체를 가져옵니다.
* @return getListResult
* @author 전지환, 정시원
*/
@GetMapping("/uncomfortable")
public ListResult<UncomfortableGetDto> viewAll(){
return responseService.getListResult(uncomfortableService.viewAll());
public ListResult<UncomfortableGetDto> 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<Long> amountUncomfortable(){
return responseService.getSingleResult(uncomfortableService.amountUncomfortableView());
public SingleResult<Long> getNumberOfUncomfortable(){
return responseService.getSingleResult(uncomfortableService.getNumberOfUncomfortable());
}

// localhost:8080/v1/uncomfortable/dateSinceProjectStart
/**
* 프로젝트 D-day를 세어 가져옵니다.
* @return getSingleResult
* @author 전지환, 정시원
*/
@GetMapping("/uncomfortable/dateSinceProjectStart")
public SingleResult<Integer> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@Repository
public interface UncomfortableRepository extends JpaRepository<UncomfortableEntity, Long>{
// idx로 uncomfortable 찾기.

Optional<UncomfortableEntity> findByBoardIdx(Long boardIdx);

@Query(value = "SELECT COUNT(table.boardIdx) " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<UncomfortableGetDto> top30View() {
/**
* 많은 학생들이 공감한 글 상위 30개를 선별하여 가져옵니다.
* @return List<UncomfortableGetDto>
*/
public List<UncomfortableGetDto> getTop30() {
return uncomfortableRepository.uncomfortableViewTopBy(PageRequest.of(0,30));
}

// 전체 페이지 보여주기.
public List<UncomfortableGetDto> viewAll(){
/**
* 학교의 불편함 전체를 가져옵니다.
* @return List<UncomfortableGetDto>
*/
public List<UncomfortableGetDto> 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;

Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ String objectToJson(Object object) throws JsonProcessingException {
).limit(40).collect(Collectors.toList());

tableRepo.saveAll(uncomfortableEntities);
List<UncomfortableGetDto> uncomfortableGetDtos = uncomfortableService.top30View();
List<UncomfortableGetDto> uncomfortableGetDtos = uncomfortableService.getTop30();
String top30Data = objectToJson(uncomfortableGetDtos);

//When
Expand All @@ -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)
);

Expand All @@ -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)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -68,7 +68,7 @@ public void cleanUp(){

// When
tableRepo.saveAll(uncomfortableEntities);
List<UncomfortableGetDto> viewTop30 = uncomfortableService.top30View();
List<UncomfortableGetDto> viewTop30 = uncomfortableService.getTop30();

// Then
assertEquals(viewTop30.size(), 30);
Expand All @@ -93,7 +93,7 @@ public void cleanUp(){

// When
tableRepo.saveAll(uncomfortableEntities);
List<UncomfortableGetDto> tableViewAll = uncomfortableService.viewAll();
List<UncomfortableGetDto> tableViewAll = uncomfortableService.getAllUncomfortable();

// Then
assertEquals(tableViewAll.size(), 10); // 10개를 저장했으므로 tableViewAll 의 개수는 10개여야 한다.
Expand All @@ -113,7 +113,7 @@ public void cleanUp(){

// When
tableRepo.saveAll(uncomfortableEntities);
Long amountUncomfortable = uncomfortableService.amountUncomfortableView();
Long amountUncomfortable = uncomfortableService.getNumberOfUncomfortable();

// then
assertEquals(amountUncomfortable, 10);
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -182,7 +182,7 @@ public void cleanUp(){
System.out.println(savedUncomfortableEntity.getBoardIdx());

assertThrows(GoodsNotCancelException.class, () ->{
uncomfortableService.cancelGood(savedUncomfortableEntity.getBoardIdx());
uncomfortableService.decreaseLike(savedUncomfortableEntity.getBoardIdx());
});

}
Expand Down

0 comments on commit 062819f

Please sign in to comment.