Skip to content

Commit

Permalink
Merge pull request #35 from jurumarble/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
jonghyunhub authored Sep 4, 2023
2 parents 65c0436 + 7b64dd8 commit 7ca3663
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 58 deletions.
2 changes: 2 additions & 0 deletions src/main/java/co/kr/jurumarble/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public void addInterceptors(InterceptorRegistry registry) {
.addPathPatterns("/api/votes/normal")
.addPathPatterns("/api/votes/drink")
.addPathPatterns("/api/votes/{voteId}/")
.addPathPatterns("/api/votes/{voteId}/normal")
.addPathPatterns("/api/votes/{voteId}/drink")
.addPathPatterns("/api/votes/{voteId}/vote")
.addPathPatterns("/api/votes/{voteId}/voted")
.addPathPatterns("/api/votes/{voteId}/bookmark")
Expand Down
22 changes: 13 additions & 9 deletions src/main/java/co/kr/jurumarble/vote/controller/VoteController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import co.kr.jurumarble.comment.enums.Region;
import co.kr.jurumarble.vote.dto.GetIsUserVoted;
import co.kr.jurumarble.vote.dto.VoteData;
import co.kr.jurumarble.vote.dto.request.CreateDrinkVoteRequest;
import co.kr.jurumarble.vote.dto.request.CreateNormalVoteRequest;
import co.kr.jurumarble.vote.dto.request.DoVoteRequest;
import co.kr.jurumarble.vote.dto.request.UpdateVoteRequest;
import co.kr.jurumarble.vote.dto.request.*;
import co.kr.jurumarble.vote.dto.response.GetIsUserVotedResponse;
import co.kr.jurumarble.vote.dto.response.GetVoteListResponse;
import co.kr.jurumarble.vote.dto.response.GetVoteRecommendListResponse;
Expand Down Expand Up @@ -61,20 +58,27 @@ public ResponseEntity<GetVoteListResponse> getVotes(@RequestParam(required = fal
public ResponseEntity<GetVoteListResponse> getDrinkVotes(@RequestParam(required = false) String keyword, @RequestParam(required = false) Region region, @RequestParam SortByType sortBy, @RequestParam int page, @RequestParam int size) {
String regionName = (region != null) ? region.getName() : null;
Slice<VoteData> voteListData = voteService.findDrinkVotes(keyword, regionName, sortBy, page, size);
return new ResponseEntity(new GetVoteListResponse(voteListData), HttpStatus.OK);
return new ResponseEntity<>(new GetVoteListResponse(voteListData), HttpStatus.OK);
}

@Operation(summary = "일반 투표 단건 조회", description = "파라미터에 voteId 보내주시면 됩니다.")
@GetMapping("/{voteId}/")
public ResponseEntity<GetVoteResponse> getVote(@PathVariable Long voteId) {
GetVoteData data = voteService.getVote(voteId);
return new ResponseEntity(new GetVoteResponse(data), HttpStatus.OK);
return new ResponseEntity<>(new GetVoteResponse(data), HttpStatus.OK);
}

@Operation(summary = "일반 투표 수정", description = "파라미터에 voteId, 바디에 {title, detail, titleA, titleB} json 형식으로 보내주시면 됩니다.")
@PutMapping("/{voteId}/")
public ResponseEntity updateVote(@PathVariable("voteId") Long voteId, @RequestBody UpdateVoteRequest request, @RequestAttribute Long userId) {
voteService.updateVote(request.toServiceRequest(voteId, userId, request));
@PutMapping("/{voteId}/normal")
public ResponseEntity updateNormalVote(@PathVariable("voteId") Long voteId, @RequestBody UpdateNormalVoteRequest request, @RequestAttribute Long userId) {
voteService.updateNormalVote(request.toServiceRequest(voteId, userId));
return new ResponseEntity(HttpStatus.OK);
}

@Operation(summary = "전통주 투표 수정", description = "파라미터에 voteId, 바디에 {title, detail, titleA, titleB} json 형식으로 보내주시면 됩니다.")
@PutMapping("/{voteId}/drink")
public ResponseEntity updateDrinkVote(@PathVariable("voteId") Long voteId, @RequestBody UpdateDrinkVoteRequest request, @RequestAttribute Long userId) {
voteService.updateDrinkVote(request.toServiceRequest(voteId, userId));
return new ResponseEntity(HttpStatus.OK);
}

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/co/kr/jurumarble/vote/domain/Vote.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import co.kr.jurumarble.user.enums.GenderType;
import co.kr.jurumarble.user.enums.MbtiType;
import co.kr.jurumarble.vote.enums.VoteType;
import co.kr.jurumarble.vote.service.UpdateVoteServiceRequest;
import co.kr.jurumarble.vote.service.UpdateDrinkVoteServiceRequest;
import co.kr.jurumarble.vote.service.UpdateNormalVoteServiceRequest;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -78,7 +79,12 @@ public boolean isVoteOfUser(Long userId) {
return this.postedUserId.equals(userId);
}

public void update(UpdateVoteServiceRequest request) {
public void updateNormalVote(UpdateNormalVoteServiceRequest request) {
this.title = request.getTitle();
this.detail = request.getDetail();
}

public void updateDrinkVote(UpdateDrinkVoteServiceRequest request) {
this.title = request.getTitle();
this.detail = request.getDetail();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/co/kr/jurumarble/vote/domain/VoteContent.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package co.kr.jurumarble.vote.domain;

import co.kr.jurumarble.vote.service.UpdateVoteServiceRequest;
import co.kr.jurumarble.vote.service.UpdateNormalVoteServiceRequest;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -40,7 +40,7 @@ private VoteContent(Long id, String imageA, String imageB, String titleA, String
this.voteId = voteId;
}

public void update(UpdateVoteServiceRequest request) {
public void update(UpdateNormalVoteServiceRequest request) {
this.titleA = request.getTitleA();
this.titleB = request.getTitleB();
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/co/kr/jurumarble/vote/domain/VoteDrinkContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ private void validateDrinksDuplicated(Long drinkAId, Long drinkBId) {
}
}

public void updateFromDrinks(DrinksUsedForVote drinksUsedForVote) {
Drink drinkA = drinksUsedForVote.getDrinkA();
Drink drinkB = drinksUsedForVote.getDrinkB();
this.drinkAId = drinkA.getId();
this.drinkBId = drinkB.getId();
this.drinkAImage = drinkA.getImage();
this.drinkBImage = drinkB.getImage();
this.drinkAName = drinkA.getName();
this.drinkBName = drinkB.getName();
this.drinkAType = drinkA.getType();
this.drinkBType = drinkB.getType();
this.region = drinkA.getRegion();
}


public void mappingVote(Long voteId) {
this.voteId = voteId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package co.kr.jurumarble.vote.dto.request;

import co.kr.jurumarble.vote.service.UpdateDrinkVoteServiceRequest;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Positive;

@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UpdateDrinkVoteRequest {

@Schema(description = "투표 제목", example = "A, B 중 어떤게 나을까요?")
@NotBlank(message = "투표 제목은 필수입니다.")
private String title;

@Schema(description = "전통주 후보 A의 id (전통주 아이디는 다른값으로 넣어주셔야합니다.)")
@Positive(message = "전통주 아이디는 양수값의 정수여야합니다.")
private Long drinkAId;

@Schema(description = "전통주 후보 B의 id (전통주 아이디는 다른값으로 넣어주셔야합니다.)")
@Positive(message = "전통주 아이디는 양수값의 정수여야합니다.")
private Long drinkBId;

@Schema(description = "투표 상세글")
@NotBlank(message = "투표 상세글은 필수입니다.")
private String detail;

public UpdateDrinkVoteServiceRequest toServiceRequest(Long voteId, Long userId) {
return UpdateDrinkVoteServiceRequest.builder()
.voteId(voteId)
.userId(userId)
.title(title)
.detail(detail)
.drinkAId(drinkAId)
.drinkBId(drinkBId)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package co.kr.jurumarble.vote.dto.request;

import co.kr.jurumarble.vote.service.UpdateVoteServiceRequest;
import co.kr.jurumarble.vote.service.UpdateNormalVoteServiceRequest;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

Expand All @@ -9,7 +9,7 @@
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UpdateVoteRequest {
public class UpdateNormalVoteRequest {

@Schema(description = "투표 제목", example = "A, B 중 어떤게 나을까요?")
@NotBlank(message = "투표 제목은 필수입니다.")
Expand All @@ -28,22 +28,22 @@ public class UpdateVoteRequest {
private String titleB;

@Builder
public UpdateVoteRequest(String title, String detail, String titleA, String titleB) {
public UpdateNormalVoteRequest(String title, String detail, String titleA, String titleB) {
this.title = title;
this.detail = detail;
this.titleA = titleA;
this.titleB = titleB;
}

public UpdateVoteServiceRequest toServiceRequest(Long voteId, Long userId, UpdateVoteRequest request) {
public UpdateNormalVoteServiceRequest toServiceRequest(Long voteId, Long userId) {

return UpdateVoteServiceRequest.builder()
return UpdateNormalVoteServiceRequest.builder()
.voteId(voteId)
.userId(userId)
.title(request.getTitle())
.detail(request.getDetail())
.titleA(request.getTitleA())
.titleB(request.getTitleB())
.title(title)
.detail(detail)
.titleA(titleA)
.titleB(titleB)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package co.kr.jurumarble.vote.repository;

import co.kr.jurumarble.vote.domain.VoteContent;
import co.kr.jurumarble.vote.domain.VoteDrinkContent;
import org.springframework.data.jpa.repository.JpaRepository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface VoteEntityRepository {

List<VoteData> findDrinkVotesByTime(String keyword, String region, int pageNum, int pageSize);

List<VoteData> findDrinkVotesByPopularity(String keyword, String region, int pageNum, int pageSize);
List<VoteData> findDrinkVotesByPopularity(String keyword, String region, int pageNum, int pageSize);

Optional<VoteData> findVoteDataByVoteId(Long voteId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.List;
import java.util.Optional;

import static co.kr.jurumarble.drink.domain.entity.QDrink.drink;
import static co.kr.jurumarble.user.domain.QUser.user;
import static co.kr.jurumarble.vote.domain.QVote.vote;
import static co.kr.jurumarble.vote.domain.QVoteContent.voteContent;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package co.kr.jurumarble.vote.service;

import co.kr.jurumarble.drink.domain.dto.DrinkIdsUsedForVote;
import lombok.Builder;
import lombok.Getter;

@Getter
public class UpdateDrinkVoteServiceRequest {

private final Long voteId;
private final Long userId;
private final String title;
private final String detail;
private final Long drinkAId;
private final Long drinkBId;

@Builder
public UpdateDrinkVoteServiceRequest(Long voteId, Long userId, String title, String detail, Long drinkAId, Long drinkBId) {
this.voteId = voteId;
this.userId = userId;
this.title = title;
this.detail = detail;
this.drinkAId = drinkAId;
this.drinkBId = drinkBId;
}

public DrinkIdsUsedForVote extractDrinkIds() {
return new DrinkIdsUsedForVote(drinkAId, drinkBId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package co.kr.jurumarble.vote.service;

import lombok.Builder;
import lombok.Getter;

@Getter
public class UpdateNormalVoteServiceRequest {

private final Long voteId;
private final Long userId;
private final String title;
private final String detail;
private final String titleA;
private final String titleB;


@Builder
private UpdateNormalVoteServiceRequest(Long voteId, Long userId, String title, String detail, String titleA, String titleB) {
this.voteId = voteId;
this.userId = userId;
this.title = title;
this.detail = detail;
this.titleA = titleA;
this.titleB = titleB;
}
}

This file was deleted.

14 changes: 12 additions & 2 deletions src/main/java/co/kr/jurumarble/vote/service/VoteService.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,24 @@ public GetVoteData getVote(Long voteId) {
}

@Transactional
public void updateVote(UpdateVoteServiceRequest request) {
public void updateNormalVote(UpdateNormalVoteServiceRequest request) {
Vote vote = voteRepository.findById(request.getVoteId()).orElseThrow(VoteNotFoundException::new);
isVoteOfUser(request.getUserId(), vote);
VoteContent voteContent = voteContentRepository.findByVoteId(vote.getId()).orElseThrow(VoteNotFoundException::new);
vote.update(request);
vote.updateNormalVote(request);
voteContent.update(request);
}

@Transactional
public void updateDrinkVote(UpdateDrinkVoteServiceRequest request) {
Vote vote = voteRepository.findById(request.getVoteId()).orElseThrow(VoteNotFoundException::new);
isVoteOfUser(request.getUserId(), vote);
VoteDrinkContent voteDrinkContent = voteDrinkContentRepository.findByVoteId(vote.getId()).orElseThrow(VoteNotFoundException::new);
DrinksUsedForVote drinksUsedForVote = drinkFinder.findDrinksUsedForVote(request.extractDrinkIds());
vote.updateDrinkVote(request);
voteDrinkContent.updateFromDrinks(drinksUsedForVote);
}

public void isVoteOfUser(Long userId, Vote vote) {
if (!vote.isVoteOfUser(userId)) throw new UserNotAccessRightException();
}
Expand Down

0 comments on commit 7ca3663

Please sign in to comment.