Skip to content

Commit

Permalink
Merge pull request #38 from Central-MakeUs/fix/popcornDuplicate
Browse files Browse the repository at this point in the history
[fix]팝콘 오류 분기처리
  • Loading branch information
AlmondBreez3 authored Feb 1, 2024
2 parents 8c80008 + d002baf commit 927d33f
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.example.domains.popcornUser.entity.PopcornUser;
import com.example.domains.popcornUser.entity.enums.PopcornNegative;
import com.example.domains.popcornUser.entity.enums.PopcornPositive;
import com.example.domains.popcornUser.exceptions.NoPopcornReview;
import com.example.domains.screening.adaptor.ScreeningAdaptor;
import com.example.domains.screening.entity.Screening;
import com.example.domains.screening.validator.ScreeningValidator;
Expand Down Expand Up @@ -40,11 +41,23 @@ public class PostPopcornReviewUseCase {
public PopcornReviewResponse execute(Long popcornId, PostPopcornReviewRequest request) {
Long userId = SecurityUtil.getCurrentUserId();
User user = userAdaptor.findById(userId);
validatePopcorn(popcornId);
Popcorn popcorn = popcornAdaptor.findById(popcornId);
return reviewUpload(popcorn, user,request);
validateDuplicate(user,popcorn);
return reviewUpload(popcorn,user,request);
//save, userScreenId
}

private void validatePopcorn(Long id) {
if(!popcornAdaptor.checkIfExists(id).isPresent()){
throw NoPopcornReview.EXCEPTION;
};
}

private void validateDuplicate(User user, Popcorn popcorn) {
popcornUserAdaptor.checkIfExists(user.getId(),popcorn.getId());
}


private PopcornReviewResponse reviewUpload(Popcorn popcorn, User user, PostPopcornReviewRequest request) {
final PopcornUser newPopcornUser = PopcornUser.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.example.domains.diverseMovie.entity.QDiverseMovie.diverseMovie;
Expand Down Expand Up @@ -112,6 +113,9 @@ public List<Popcorn> findLastWeekPopcorns() {
public Popcorn findById(Long popcornId){
return popcornRepository.findById(popcornId).get();
}
public Optional<Popcorn> checkIfExists(Long popcornId){
return popcornRepository.findById(popcornId);
}

@Transactional
public void incrementPositiveCineMaster(Popcorn popcorn) {
Expand Down Expand Up @@ -490,4 +494,5 @@ public void saveToPopcornTest() {
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.example.domains.popcorn.entity.QPopcorn;
import com.example.domains.popcornUser.entity.PopcornUser;
import com.example.domains.popcornUser.entity.QPopcornUser;
import com.example.domains.popcornUser.exceptions.DuplicatePopcorn;
import com.example.domains.popcornUser.repository.PopcornUserRepository;
import com.example.domains.screeningReview.entity.QScreeningReview;
import com.example.domains.screeningReview.entity.ScreeningReview;
Expand Down Expand Up @@ -121,5 +122,11 @@ public int getParticipatedCount(Long popcornId) {
public int getParticipatedUserCount(Long popcornId) {
return popcornUserRepository.countDistinctUserIdByPopcornId(popcornId);
}

public void checkIfExists(Long id, Long popcornId) {
if(popcornUserRepository.existsByUserIdAndPopcornId(id,popcornId)==true){
throw DuplicatePopcorn.EXCEPTION;
}
}
// int total = popcornUserRepository.findAllByPopcornId(popcorn.getId()).size();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.domains.popcornUser.exceptions;

import com.example.domains.recommendedPopcorn.exceptions.NoMovieSearched;
import com.example.domains.recommendedPopcorn.exceptions.RecommendedErrorCode;
import com.example.error.BaseErrorException;

public class DuplicatePopcorn extends BaseErrorException {
public static final BaseErrorException EXCEPTION = new DuplicatePopcorn();

private DuplicatePopcorn () {
super(PopcornUserErrorCode.DUPLICATE_POPCORN_REVIEW);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.domains.popcornUser.exceptions;

import com.example.error.BaseErrorException;

public class NoPopcornReview extends BaseErrorException {
public static final BaseErrorException EXCEPTION = new NoPopcornReview();

private NoPopcornReview () {
super(PopcornUserErrorCode.NO_POPCORN_REVIEW);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.domains.popcornUser.exceptions;

import com.example.dto.ErrorReason;
import com.example.error.BaseErrorCode;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum PopcornUserErrorCode implements BaseErrorCode {
DUPLICATE_POPCORN_REVIEW(409,"POPCORN_REVIEW_409", "중복된 리뷰가 존재합니다"),
NO_POPCORN_REVIEW(404,"POPCORN_REVIEW_404" ,"팝콘이 존재하지 않습니다" );
private int status;
private String code;
private String reason;

@Override
public ErrorReason getErrorReason() {
return ErrorReason.of(status, code, reason);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ public interface PopcornUserRepository extends JpaRepository<PopcornUser, Long>
// 특정 popcornId에 해당하는 distinct한 userId 개수 조회
@Query("SELECT COUNT(DISTINCT p.user.id) FROM PopcornUser p WHERE p.popcorn.id = :popcornId")
int countDistinctUserIdByPopcornId(@Param("popcornId") Long popcornId);

boolean existsByUserIdAndPopcornId(Long userId, Long popcornId);
}

0 comments on commit 927d33f

Please sign in to comment.