-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IP 기반 좋아요 기능 구현
- Loading branch information
Showing
13 changed files
with
206 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
animory/src/main/java/com/daggle/animory/domain/shortform/entity/PetVideoLike.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.daggle.animory.domain.shortform.entity; | ||
|
||
import com.daggle.animory.common.entity.BaseEntity; | ||
import com.daggle.animory.domain.pet.entity.PetVideo; | ||
import javax.persistence.Entity; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.ManyToOne; | ||
import javax.persistence.Table; | ||
import javax.validation.constraints.NotNull; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Entity | ||
@Table(name = "pet_video_like") | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class PetVideoLike extends BaseEntity { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Integer id; | ||
|
||
@NotNull | ||
private String ipAddress; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "pet_video_id") | ||
private PetVideo petVideo; | ||
|
||
@Builder | ||
public PetVideoLike(final String ipAddress, final PetVideo petVideo) { | ||
this.ipAddress = ipAddress; | ||
this.petVideo = petVideo; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ory/src/main/java/com/daggle/animory/domain/shortform/exception/AlreadyLikedPetVideo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.daggle.animory.domain.shortform.exception; | ||
|
||
public class AlreadyLikedPetVideo extends RuntimeException { | ||
@Override | ||
public String getMessage() { | ||
return ShortFormExceptionMessage.ALREADY_LIKED_PET_VIDEO.getMessage(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
animory/src/main/java/com/daggle/animory/domain/shortform/exception/NotLikedPetVideo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.daggle.animory.domain.shortform.exception; | ||
|
||
public class NotLikedPetVideo extends RuntimeException { | ||
@Override | ||
public String getMessage() { | ||
return ShortFormExceptionMessage.NOT_LIKED_PET_VIDEO.getMessage(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...rc/main/java/com/daggle/animory/domain/shortform/exception/ShortFormExceptionMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.daggle.animory.domain.shortform.exception; | ||
|
||
public enum ShortFormExceptionMessage { | ||
SHORT_FORM_NOT_FOUND("해당하는 숏폼이 존재하지 않습니다."), | ||
ALREADY_LIKED_PET_VIDEO("이미 좋아요를 누른 영상입니다."), | ||
NOT_LIKED_PET_VIDEO("좋아요를 누르지 않은 영상입니다."); | ||
private final String message; | ||
|
||
ShortFormExceptionMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
animory/src/main/java/com/daggle/animory/domain/shortform/exception/ShortFormNotFound.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.daggle.animory.domain.shortform.exception; | ||
|
||
public class ShortFormNotFound extends RuntimeException { | ||
@Override | ||
public String getMessage() { | ||
return ShortFormExceptionMessage.SHORT_FORM_NOT_FOUND.getMessage(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
.../src/main/java/com/daggle/animory/domain/shortform/repository/PetVideoLikeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.daggle.animory.domain.shortform.repository; | ||
|
||
import com.daggle.animory.domain.shortform.entity.PetVideoLike; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface PetVideoLikeRepository extends JpaRepository<PetVideoLike, Integer> { | ||
|
||
boolean existsByIpAddressAndPetVideoId(String ipAddress, Integer petVideoId); | ||
|
||
void deleteByIpAddressAndPetVideoId(String ipAddress, Integer petVideoId); | ||
} |
57 changes: 57 additions & 0 deletions
57
animory/src/main/java/com/daggle/animory/domain/shortform/service/PetVideoLikeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.daggle.animory.domain.shortform.service; | ||
|
||
import com.daggle.animory.domain.pet.entity.PetVideo; | ||
import com.daggle.animory.domain.shortform.entity.PetVideoLike; | ||
import com.daggle.animory.domain.shortform.exception.AlreadyLikedPetVideo; | ||
import com.daggle.animory.domain.shortform.exception.NotLikedPetVideo; | ||
import com.daggle.animory.domain.shortform.exception.ShortFormNotFound; | ||
import com.daggle.animory.domain.shortform.repository.PetVideoLikeRepository; | ||
import com.daggle.animory.domain.shortform.repository.PetVideoRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class PetVideoLikeService { | ||
final PetVideoRepository petVideoRepository; | ||
final PetVideoLikeRepository petVideoLikeRepository; | ||
|
||
public void updatePetVideoLikeCount(final String IPAddress, final int petVideoId) { | ||
validatePetVideoLikeDuplication(IPAddress, petVideoId); | ||
|
||
final PetVideo petVideo = petVideoRepository.findById(petVideoId) | ||
.orElseThrow(ShortFormNotFound::new); | ||
|
||
petVideo.updateLikeCount(); | ||
|
||
petVideoLikeRepository.save(PetVideoLike.builder() | ||
.ipAddress(IPAddress) | ||
.petVideo(petVideo) | ||
.build()); | ||
} | ||
|
||
public void deletePetVideoLikeCount(final String IPAddress, final int petVideoId) { | ||
validatePetVideoLikeDelete(IPAddress, petVideoId); | ||
|
||
final PetVideo petVideo = petVideoRepository.findById(petVideoId) | ||
.orElseThrow(ShortFormNotFound::new); | ||
|
||
petVideo.deleteLikeCount(); | ||
|
||
petVideoLikeRepository.deleteByIpAddressAndPetVideoId(IPAddress, petVideoId); | ||
} | ||
|
||
private void validatePetVideoLikeDuplication(final String IPAddress, final int petVideoId) { | ||
if (petVideoLikeRepository.existsByIpAddressAndPetVideoId(IPAddress, petVideoId)) { | ||
throw new AlreadyLikedPetVideo(); | ||
} | ||
} | ||
|
||
private void validatePetVideoLikeDelete(final String IPAddress, final int petVideoId) { | ||
if (!petVideoLikeRepository.existsByIpAddressAndPetVideoId(IPAddress, petVideoId)) { | ||
throw new NotLikedPetVideo(); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ry/domain/shortform/ShortFormService.java → ...n/shortform/service/ShortFormService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
animory/src/main/resources/db/migration/V4.0.0__create_petvideolike.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
CREATE TABLE pet_video_like | ||
( | ||
id INT NOT NULL, | ||
created_at datetime NULL, | ||
updated_at datetime NULL, | ||
ip_address VARCHAR(255) NOT NULL, | ||
pet_video_id INT NULL, | ||
CONSTRAINT pk_pet_video_like PRIMARY KEY (id) | ||
); | ||
|
||
ALTER TABLE pet_video_like | ||
ADD CONSTRAINT FK_PET_VIDEO_LIKE_ON_PET_VIDEO FOREIGN KEY (pet_video_id) REFERENCES pet_video (id); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters