Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
wellbeing-dough committed Sep 5, 2023
2 parents 93c9e10 + b9178f6 commit 334a120
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
public class VoteController {
private final VoteService voteService;

@Operation(summary = "일반 투표 생성", description = "헤더에 토큰 담고, 바디에 {title, titleA, titleB, imageA, imageB, filteredGender, filteredAge, filteredMbti} json 형식으로 보내주시면 됩니다.")
@Operation(summary = "일반 투표 생성", description = "헤더에 토큰 담고, 바디에 {title, titleA, titleB, imageA, imageB} json 형식으로 보내주시면 됩니다.")
@PostMapping("/normal")
public ResponseEntity createNormalVote(@Valid @RequestBody CreateNormalVoteRequest request, @RequestAttribute Long userId) {
voteService.createNormalVote(request.toServiceRequest(), userId);
return ResponseEntity.status(HttpStatus.CREATED).build();
}

@Operation(summary = "전통주 투표 생성", description = "헤더에 토큰 담고, 바디에 {title, titleA, titleB, imageA, imageB, filteredGender, filteredAge, filteredMbti} json 형식으로 보내주시면 됩니다.")
@Operation(summary = "전통주 투표 생성", description = "헤더에 토큰 담고, 바디에 {title, drinkAId, drinkBId} json 형식으로 보내주시면 됩니다.")
@PostMapping("/drink")
public ResponseEntity createDrinkVote(@Valid @RequestBody CreateDrinkVoteRequest request, @RequestAttribute Long userId) {
voteService.createDrinkVote(request.toServiceRequest(), userId);
Expand Down Expand Up @@ -117,8 +117,8 @@ public ResponseEntity doVote(@RequestBody DoVoteRequest request, @PathVariable("

@Operation(summary = "투표 검색어 추천", description = "파라미터에 keyword, category 보내주시면 됩니다.")
@GetMapping("/recommend")
public ResponseEntity recommendVote(@RequestParam String keyword) {
List<String> voteRecommendListData = voteService.getRecommendVoteList(keyword);
public ResponseEntity recommendVote(@RequestParam String keyword, @RequestParam int recommendCount) {
List<String> voteRecommendListData = voteService.getRecommendVoteList(keyword, recommendCount);
return new ResponseEntity(new GetVoteRecommendListResponse(voteRecommendListData), HttpStatus.OK);
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/co/kr/jurumarble/vote/domain/VoteValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ public class VoteValidator {

private final VoteResultRepository voteResultRepository;

public void validateParcitipateVote(Vote vote, User user) {
validPostedUserWhenParcitipateVote(vote, user);
validAlreadyParcitipatedVote(vote, user);
public void validateParticipateVote(Vote vote, User user) {
validPostedUserWhenParticipateVote(vote, user);
validAlreadyParticipatedVote(vote, user);
}

public void validPostedUserWhenParcitipateVote(Vote vote, User user) {
public void validPostedUserWhenParticipateVote(Vote vote, User user) {
if (vote.isVoteOfUser(user.getId())) throw new UserNotAccessRightException();
}

public void validAlreadyParcitipatedVote(Vote vote, User user) {
public void validAlreadyParticipatedVote(Vote vote, User user) {
if (voteResultRepository.existsByVoteIdAndVotedUserId(vote.getId(), user.getId()))
throw new AlreadyUserDoVoteException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class CreateNormalVoteRequest {
@NotBlank(message = "투표 제목은 필수입니다.")
private String title;

@Schema(description = "투표 상세", example = "A는 ~때문에 고민이고 B는 ~때문에 고민입니다")
private String detail;

@Schema(description = "A 항목의 제목")
@NotBlank(message = "투표 A항목의 제목은 필수입니다.")
private String titleA;
Expand All @@ -27,17 +30,15 @@ public class CreateNormalVoteRequest {
private String titleB;

@Schema(description = "A 이미지")
@NotBlank(message = "투표 A 이미지는 필수입니다.")
private String imageA;

@Schema(description = "B 이미지")
@NotBlank(message = "투표 B 이미지는 필수입니다.")
private String imageB;


@Builder
public CreateNormalVoteRequest(String title, String titleA, String titleB, String imageA, String imageB) {
public CreateNormalVoteRequest(String title, String detail, String titleA, String titleB, String imageA, String imageB) {
this.title = title;
this.detail = detail;
this.titleA = titleA;
this.titleB = titleB;
this.imageA = imageA;
Expand All @@ -47,6 +48,7 @@ public CreateNormalVoteRequest(String title, String titleA, String titleB, Strin
public CreateNormalVoteServiceRequest toServiceRequest() {
return CreateNormalVoteServiceRequest.builder()
.title(title)
.detail(detail)
.titleA(titleA)
.titleB(titleB)
.imageA(imageA)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface VoteEntityRepository {

Optional<VoteCommonData> findVoteCommonDataByVoteId(Long voteId);

List<Vote> findByTitleContains(String keyword);
List<Vote> findByTitleContains(String keyword, int recommendCount);

Long countByVoteAndChoiceAndGenderAndAgeAndMBTI(Long voteId, ChoiceType choiceType, GenderType gender, Integer classifyAge, MbtiType mbti);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private BooleanExpression getKeywordExpression(String keyword) {


@Override
public List<Vote> findByTitleContains(String keyword) {
public List<Vote> findByTitleContains(String keyword, int recommendCount) {
return jpaQueryFactory
.selectFrom(vote)
.innerJoin(voteContent)
Expand All @@ -219,7 +219,7 @@ public List<Vote> findByTitleContains(String keyword) {
.where(vote.title.like("%" + keyword + "%"))
.groupBy(vote.id)
.orderBy(voteResult.id.count().desc())
.limit(5)
.limit(recommendCount)
.fetch();
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/co/kr/jurumarble/vote/service/VoteService.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class VoteService {
public Long createNormalVote(CreateNormalVoteServiceRequest request, Long userId) {
userRepository.findById(userId).orElseThrow(UserNotFoundException::new);
VoteContent voteContent = request.toVoteContent();
Vote vote = request.toVote(userId);
Vote vote = request.toVote(userId, request.getDetail());
return voteGenerator.createNormalVote(vote, voteContent);
}

Expand Down Expand Up @@ -131,7 +131,7 @@ private void deleteVoteContent(Vote vote) {
public void doVote(DoVoteInfo info) {
Vote vote = voteRepository.findById(info.getVoteId()).orElseThrow(VoteNotFoundException::new);
User user = userRepository.findById(info.getUserId()).orElseThrow(UserNotFoundException::new);
voteValidator.validateParcitipateVote(vote, user);
voteValidator.validateParticipateVote(vote, user);
VoteResult voteResult = new VoteResult(vote.getId(), user.getId(), info.getChoice());
voteResultRepository.save(voteResult);
}
Expand Down Expand Up @@ -161,8 +161,8 @@ public Slice<VoteData> findVotesByPopularity(String keyword, PageRequest pageabl
return voteFinder.getVoteData(pageable, voteCommonDataByPopularity);
}

public List<String> getRecommendVoteList(String keyword) {
return voteRepository.findByTitleContains(keyword).stream()
public List<String> getRecommendVoteList(String keyword, int recommendCount) {
return voteRepository.findByTitleContains(keyword, recommendCount).stream()
.map(Vote::getTitle)
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
@Getter
public class CreateNormalVoteServiceRequest {
private String title;
private String detail;
private String titleA;
private String titleB;
private String imageA;
private String imageB;
private VoteType voteType;

@Builder
private CreateNormalVoteServiceRequest(String title, String titleA, String titleB, String imageA, String imageB, VoteType voteType) {
private CreateNormalVoteServiceRequest(String title, String detail, String titleA, String titleB, String imageA, String imageB, VoteType voteType) {
validVoteType(voteType);
this.title = title;
this.detail = detail;
this.titleA = titleA;
this.titleB = titleB;
this.imageA = imageA;
Expand All @@ -42,11 +44,12 @@ public VoteContent toVoteContent() {
.build();
}

public Vote toVote(Long userId) {
public Vote toVote(Long userId, String detail) {
return Vote.builder()
.postedUserId(userId)
.voteType(voteType)
.title(title)
.detail(detail)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void createVote() throws Exception {
// given
CreateNormalVoteRequest request = CreateNormalVoteRequest.builder()
.title("투표 제목")
.detail("투표 상세")
.titleA("A 항목 제목")
.titleB("B 항목 제목")
.imageA("A 항목 이미지")
Expand Down Expand Up @@ -73,6 +74,7 @@ void createVoteWithOutToken() throws Exception {
// given
CreateNormalVoteRequest request = CreateNormalVoteRequest.builder()
.title("투표 제목")
.detail("투표 상세")
.titleA("A 항목 제목")
.titleB("B 항목 제목")
.imageA("A 항목 이미지")
Expand All @@ -97,6 +99,7 @@ void createVoteWithExpiredToken() throws Exception {
// given
CreateNormalVoteRequest request = CreateNormalVoteRequest.builder()
.title("투표 제목")
.detail("투표 상세")
.titleA("A 항목 제목")
.titleB("B 항목 제목")
.imageA("A 항목 이미지")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void validPostedUserWhenParcitipateVote() {
// when // then
assertThrows(UserNotAccessRightException.class,
() -> {
voteValidator.validPostedUserWhenParcitipateVote(vote, postedUser);
voteValidator.validPostedUserWhenParticipateVote(vote, postedUser);
});
}

Expand All @@ -53,7 +53,7 @@ void validAlreadyParcitipatedVote() {

when(voteResultRepository.existsByVoteIdAndVotedUserId(vote.getId(), user.getId())).thenReturn(true);
// when // then
assertThrows(AlreadyUserDoVoteException.class, () -> voteValidator.validAlreadyParcitipatedVote(vote, user));
assertThrows(AlreadyUserDoVoteException.class, () -> voteValidator.validAlreadyParticipatedVote(vote, user));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void createVote() {
.build();
VoteContent voteContent = request.toVoteContent();

Vote vote = request.toVote(userId);
Vote vote = request.toVote(userId, request.getDetail());


when(userRepository.findById(userId)).thenReturn(Optional.of(user));
Expand Down

0 comments on commit 334a120

Please sign in to comment.