Skip to content

Commit

Permalink
Merge pull request #143 from UMC-WOWMARKET/feat/like
Browse files Browse the repository at this point in the history
[Feat] Like API 수정 및 프로젝트 조회 수정 #134
  • Loading branch information
yoonsseo authored Dec 28, 2023
2 parents d4ce27e + d9a24bc commit db89836
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class ProjectController {

//주문폼: 상세 정보 조회
@GetMapping("/{project_id}")
public ProjectInfoResponseDto getProjectInfo(@PathVariable Long project_id) {
return projectService.getProjectInfo(project_id);
public ProjectInfoResponseDto getProjectInfo(@PathVariable Long project_id, @AuthenticationPrincipal User user) {
return projectService.getProjectInfo(project_id, user);
}

//주문폼: 굿즈 소개(이미지 3개) 조회
Expand Down Expand Up @@ -56,12 +56,4 @@ public ResponseEntity createDemandForm(@PathVariable Long project_id, @RequestBo
public ResponseEntity<?> likeProject(@PathVariable Long project_id, @AuthenticationPrincipal User user) {
return projectService.likeProject(project_id, user);
}

@DeleteMapping("/{project_id}/unlike")
public ResponseEntity<?> unlikeProject(@PathVariable Long project_id, @AuthenticationPrincipal User user) {
return projectService.unLikeProject(project_id, user);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ public class ProjectInfoResponseDto {
private int participant_number; //참여인원
private int achieved; //달성률 분자: 모든 주문상세의 '주문개수' 컬럼의 합
private int goal; //달성률 분모: 상품 테이블에서 프로젝트 번호로 조회하여 해당 프로젝트에 들어있는 상품의 목표치의 합
private boolean isLiked;


public ProjectInfoResponseDto(Project project, int achieved, int goal) {
public ProjectInfoResponseDto(Project project, int achieved, int goal, boolean isLiked) {
this.thumbnail = project.getThumbnail(); //대표이미지
this.category = project.getCategory().getName(); //카테고리
this.name = project.getProjectName(); //프로젝트 이름
Expand All @@ -33,5 +34,6 @@ public ProjectInfoResponseDto(Project project, int achieved, int goal) {
this.participant_number = project.getParticipant_number(); //참여인원
this.achieved = achieved;
this.goal = goal;
this.isLiked = isLiked;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ public ProjectImgResponseDto getProjectImg(Long project_id) {
}

//주문폼: 상세 정보 조회
public ProjectInfoResponseDto getProjectInfo(Long project_id) {
public ProjectInfoResponseDto getProjectInfo(Long project_id, User user) {
boolean isLiked = false;
Project project = projectRepository.findByProject_Id(project_id);
projectRepository.updateView(project_id); //상세정보 조회할 때마다 조회수 +1
if (user != null && likesRepository.findByUserAndProject(user, project_id).isPresent()) {
isLiked = true;
}
return new ProjectInfoResponseDto(project, itemRepository.getTotalOrderCountByProject(project),
itemRepository.getTotalGoalByProject(project));
itemRepository.getTotalGoalByProject(project), isLiked);
}

@Transactional
Expand All @@ -50,15 +54,6 @@ public ResponseEntity<?> likeProject(Long projectId, User user) {
projectRepository.updateProjectLike(projectId);
userRepository.updateProjectLike(user);
} else {
throw new ResponseStatusException(HttpStatus.CONFLICT, "이미 like한 프로젝트");
}
return ResponseEntity.ok().build();
}

@Transactional
public ResponseEntity<?> unLikeProject(Long projectId, User user) {
Optional<Likes> likeExist = likesRepository.findByUserAndProject(user, projectId);
if (likeExist.isPresent()) {
if (projectRepository.findById(projectId).get().getLikeCnt() > 0
&& user.getProjectLike() > 0) {
likesRepository.deleteLikes(likeExist.get().getId());
Expand All @@ -67,8 +62,6 @@ public ResponseEntity<?> unLikeProject(Long projectId, User user) {
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "like 수가 0이라 unlike할 수 없음");
}
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "like하지 않아서 unlike할 수 없음");
}
return ResponseEntity.ok().build();
}
Expand Down

0 comments on commit db89836

Please sign in to comment.