Skip to content

Commit

Permalink
Fix: fix schema
Browse files Browse the repository at this point in the history
  • Loading branch information
bin-pro committed Aug 10, 2024
1 parent 7c3755c commit 928626f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/blackshoe/esthete/entity/Like.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Like {
@JoinColumn(name = "user_id", foreignKey = @ForeignKey(name = "likes_fk_user_id"))
private User user;

@Column(name = "user_uuid", columnDefinition = "BINARY(16)", unique = true)
@Column(name = "user_uuid", columnDefinition = "BINARY(16)")
private UUID userId;

@CreatedDate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public enum FilterErrorResult {
NOT_FOUND_REPRESENTATION_IMG_URL(HttpStatus.NOT_FOUND, "대표 이미지 주소를 찾지 못했습니다."),
NOT_FOUND_FILTER_TAG(HttpStatus.NOT_FOUND, "필터 태그를 찾지 못했습니다."),
THUMBNAIL_IMG_DELETE_FAILED(HttpStatus.BAD_REQUEST, "썸네일 사진 S3 삭제에 실패했습니다."),
NOT_FOUND_LIKE(HttpStatus.NOT_FOUND,"좋아요를 누르지 않은 사용자입니다.");
NOT_FOUND_LIKE(HttpStatus.NOT_FOUND,"좋아요를 누르지 않은 사용자입니다."),
ALREADY_LIKE(HttpStatus.BAD_REQUEST,"이미 좋아요를 누른 사용자입니다."),
NOT_LIKE(HttpStatus.BAD_REQUEST,"좋아요를 누르지 않은 사용자입니다.");

private final HttpStatus httpStatus;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public interface LikeRepository extends JpaRepository<Like, Long> {
"WHERE l.userId = :userId")
Page<LikeDto.ReadResponse> readByUserId(UUID userId, Pageable pageable);

@Query("SELECT COUNT(l) FROM Like l WHERE l.user = :user AND l.filter = :filter")
Optional<Boolean> existsByUserAndFilter(User user, Filter filter);
@Query("SELECT CASE WHEN COUNT(l) > 0 THEN true ELSE false END FROM Like l WHERE l.user = :user AND l.filter = :filter")
boolean existsByUserAndFilter(User user, Filter filter);

@Query("SELECT l FROM Like l WHERE l.user = :user AND l.filter = :filter")
Optional<Like> findByUserAndFilter(User user, Filter filter);
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/blackshoe/esthete/service/LikeServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public void likeFilter(UUID userId, UUID filterId) {

final Filter filter = filterRepository.findByFilterId(filterId).orElseThrow
(() -> new FilterException(FilterErrorResult.NOT_FOUND_FILTER));

//ALREADY_LIKE
if(likeRepository.existsByUserAndFilter(user, filter)){
throw new FilterException(FilterErrorResult.ALREADY_LIKE);
}

filter.increaseLikeCount();

Like like = Like.builder()
Expand All @@ -69,6 +75,11 @@ public void unlikeFilter(UUID userId, UUID filterId) {
final Filter filter = filterRepository.findByFilterId(filterId).orElseThrow
(() -> new FilterException(FilterErrorResult.NOT_FOUND_FILTER));

//NOT_LIKE
if(!likeRepository.existsByUserAndFilter(user, filter)){
throw new FilterException(FilterErrorResult.NOT_LIKE);
}

filter.decreaseLikeCount();

Like like = likeRepository.findByUserAndFilter(user, filter).orElseThrow
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ CREATE TABLE IF NOT EXISTS `likes` (
`created_at` DATETIME(6) NULL,
`updated_at` DATETIME(6) NULL,
`filter_id` BIGINT NOT NULL,
`user_uuid` BINARY(16) NOT NULL UNIQUE,
`user_uuid` BINARY(16) NOT NULL,
CONSTRAINT `likes_fk_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE,
CONSTRAINT `likes_fk_filter_id` FOREIGN KEY (`filter_id`) REFERENCES `filters` (`filter_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Expand Down

0 comments on commit 928626f

Please sign in to comment.