Skip to content

Commit

Permalink
Merge pull request #84 from My-Own-Weapon/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
HyeokJoon authored Jul 17, 2024
2 parents 897e75c + 3df4491 commit e8a17ef
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/chimaera/wagubook/entity/Share.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Share {
private Long id;
private String url; // 공유 url
private LocalDateTime localDateTime; // 공유 시간
@Lob
private HashMap<Long, Integer> voteStoreList;

}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/chimaera/wagubook/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public enum ErrorCode {
WRONG_NAME(CONFLICT, "이름은 한글로만 입력 가능합니다."),
WRONG_PHONE_NUMBER(CONFLICT, "휴대폰 번호는 숫자로만 입력 가능합니다."),
WRONG_PASSWORD(CONFLICT, "비밀번호는 영문, 숫자, 특수문자 포함 8자 이상입니다."),
OVER_MAX(BAD_REQUEST, "최대 10개까지만 투표에 추가할 수 있습니다."),
ALREADY_ADD(BAD_REQUEST, "이미 추가된 가게입니다."),


// 401 UNAUTHORIZED: 인증되지 않은 사용자
Expand All @@ -36,6 +38,8 @@ public enum ErrorCode {
NOT_FOUND_POST(NOT_FOUND, "해당 포스트를 찾을 수 없습니다."),
NOT_FOUND_MEMBER(NOT_FOUND, "해당 회원을 찾을 수 없습니다."),
NOT_FOUND_FOLLOW(NOT_FOUND, "성립되지 않은 팔로우 관계입니다."),
NOT_FOUND_SHARE(NOT_FOUND, "공유방을 찾을 수 없습니다."),
NOT_FOUND_STORE(NOT_FOUND, "해당 스토어를 찾을 수 없습니다."),


// 409 CONFLICT: 중복된 리소스 (요청이 현재 서버 상태와 충돌될 때)
Expand All @@ -51,4 +55,4 @@ public enum ErrorCode {

private final HttpStatus httpStatus;
private final String message;
}
}
45 changes: 36 additions & 9 deletions src/main/java/com/chimaera/wagubook/service/ShareService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.chimaera.wagubook.dto.response.StoreResponse;
import com.chimaera.wagubook.entity.Share;
import com.chimaera.wagubook.entity.Store;
import com.chimaera.wagubook.exception.CustomException;
import com.chimaera.wagubook.exception.ErrorCode;
import com.chimaera.wagubook.repository.share.ShareRepository;
import com.chimaera.wagubook.repository.store.StoreRepository;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -76,23 +78,37 @@ public String findShareId(String url, Long memberId) {
@Transactional
public String addVoteStore(String shareId, String storeId) {
//가게 찾기
Optional<Store> os =storeRepository.findById(Long.parseLong(storeId));
if(os.isEmpty()){
throw new CustomException(ErrorCode.NOT_FOUND_STORE);
}
Store findStore = storeRepository.findById(Long.parseLong(storeId)).get();
//가게 추가
Share share = shareRepository.findById(Long.parseLong(shareId)).get();


//공유방에서 리스트 찾기
Optional<Share> osh = shareRepository.findById(Long.parseLong(shareId));
if(osh.isEmpty()){
throw new CustomException(ErrorCode.NOT_FOUND_SHARE);
}
Share share = osh.get();
HashMap<Long, Integer> voteStoreList = share.getVoteStoreList();


//이미 포함하고 있으면
if(voteStoreList.containsKey(findStore.getId()))
return "이미 추가된 가게입니다.";
throw new CustomException(ErrorCode.ALREADY_ADD);

//최대 개수를 넘은 경우
if(voteStoreList.size() == 10){
return "최대 10개까지만 투표에 추가할 수 있습니다.";
}
if(voteStoreList.size() == 10)
throw new CustomException(ErrorCode.OVER_MAX);

voteStoreList.put(findStore.getId(), 0);
//변경사항 저장
shareRepository.save(share);
System.out.println("[after add]");
for (Map.Entry<Long, Integer> entry : voteStoreList.entrySet()) {
System.out.println("key : " + entry.getKey() + " value : " + entry.getValue());
}
return "투표에 추가되었습니다.";
}

Expand All @@ -106,9 +122,11 @@ public String deleteVoteStore(String shareId, String storeId) {
HashMap<Long, Integer> voteStoreList = share.getVoteStoreList();

//투표 리스트에 있으면 제거
if(voteStoreList.containsKey(findStore.getId())){
voteStoreList.remove(findStore.getId());

if(voteStoreList.remove(findStore.getId())!=null){
System.out.println("[after delete]");
for (Map.Entry<Long, Integer> entry : voteStoreList.entrySet()) {
System.out.println("key : " + entry.getKey() + " value : " + entry.getValue());
}
shareRepository.save(share);
return "투표에서 삭제되었습니다.";
}
Expand All @@ -124,6 +142,10 @@ public String like(String shareId, String storeId) {
voteStoreList.replace(key, voteStoreList.get(key)+1);
System.out.println("value : " + voteStoreList.get(key));

System.out.println("[after like]");
for (Map.Entry<Long, Integer> entry : voteStoreList.entrySet()) {
System.out.println("key : " + entry.getKey() + " value : " + entry.getValue());
}
shareRepository.save(share);
return "투표 성공";
}
Expand All @@ -136,6 +158,11 @@ public String likeCancel(String shareId, String storeId) {
Long key = Long.parseLong(storeId);
voteStoreList.replace(key, voteStoreList.get(key)-1);
shareRepository.save(share);

System.out.println("[after cancel]");
for (Map.Entry<Long, Integer> entry : voteStoreList.entrySet()) {
System.out.println("key : " + entry.getKey() + " value : " + entry.getValue());
}
return "투표 취소";
}

Expand Down

0 comments on commit e8a17ef

Please sign in to comment.