-
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.
Merge pull request #423 from Team-Ampersand/413-music-like-cancel-fun…
…ction 음악 좋아요, 좋아요 삭제 기능
- Loading branch information
Showing
11 changed files
with
146 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.dotori.v2.domain.like.entity | ||
|
||
import javax.persistence.Column | ||
import javax.persistence.Entity | ||
import javax.persistence.GeneratedValue | ||
import javax.persistence.GenerationType | ||
import javax.persistence.Id | ||
import javax.persistence.Table | ||
|
||
@Entity | ||
@Table(name = "likes") | ||
class Like ( | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "like_id") | ||
val id: Long = 0, | ||
|
||
@Column(name = "music_id", nullable = false) | ||
val musicId: Long, | ||
|
||
@Column(name = "member_id", nullable = false) | ||
val memberId: Long, | ||
) |
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/com/dotori/v2/domain/like/repository/LikeRepository.kt
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,16 @@ | ||
package com.dotori.v2.domain.like.repository | ||
|
||
import com.dotori.v2.domain.member.domain.entity.Member | ||
import com.dotori.v2.domain.like.entity.Like | ||
import com.dotori.v2.domain.music.domain.entity.Music | ||
import feign.Param | ||
import javax.persistence.LockModeType | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.data.jpa.repository.Lock | ||
import org.springframework.data.jpa.repository.Query | ||
|
||
interface LikeRepository : JpaRepository<Like, Long> { | ||
@Lock(LockModeType.PESSIMISTIC_WRITE) | ||
@Query("select l from Like l where l.memberId = :memberId and l.musicId = :musicId") | ||
fun findByMemberIdAndMusicId(@Param("memberId") memberId: Long, @Param("musicId") musicId: Long): Like? | ||
} |
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
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/com/dotori/v2/domain/music/exception/NotValidMusicLikeException.kt
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,7 @@ | ||
package com.dotori.v2.domain.music.exception | ||
|
||
import com.dotori.v2.global.error.ErrorCode | ||
import com.dotori.v2.global.error.exception.BasicException | ||
|
||
class NotValidMusicLikeException : BasicException(ErrorCode.NOT_VALID_MUSIC_LIKE) { | ||
} |
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
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
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/com/dotori/v2/domain/music/service/ToggleMusicLikeService.kt
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,5 @@ | ||
package com.dotori.v2.domain.music.service | ||
|
||
interface ToggleMusicLikeService { | ||
fun execute(musicId: Long) | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/kotlin/com/dotori/v2/domain/music/service/impl/ToggleMusicLikeServiceImpl.kt
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,53 @@ | ||
package com.dotori.v2.domain.music.service.impl | ||
|
||
import com.dotori.v2.domain.like.entity.Like | ||
import com.dotori.v2.domain.like.repository.LikeRepository | ||
import com.dotori.v2.domain.member.domain.entity.Member | ||
import com.dotori.v2.domain.music.domain.entity.Music | ||
import com.dotori.v2.domain.music.domain.repository.MusicRepository | ||
import com.dotori.v2.domain.music.exception.MusicNotFoundException | ||
import com.dotori.v2.domain.music.service.ToggleMusicLikeService | ||
import com.dotori.v2.global.util.UserUtil | ||
import org.springframework.data.repository.findByIdOrNull | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Service | ||
@Transactional(readOnly = false, rollbackFor = [Exception::class]) | ||
class ToggleMusicLikeServiceImpl( | ||
private val likeRepository: LikeRepository, | ||
private val musicRepository: MusicRepository, | ||
private val userUtil: UserUtil | ||
): ToggleMusicLikeService { | ||
override fun execute(musicId: Long) { | ||
val member = userUtil.fetchCurrentUser() | ||
val music: Music = musicRepository.findByIdOrNull(musicId) ?: throw MusicNotFoundException() | ||
|
||
updateLike(music, member) | ||
} | ||
|
||
private fun updateLike(music: Music, member: Member) { | ||
val like = likeRepository.findByMemberIdAndMusicId(member.id, music.id) | ||
|
||
if (like == null) { | ||
saveLike(music, member) | ||
} else { | ||
deleteLike(like.id, music) | ||
} | ||
} | ||
|
||
private fun saveLike(music: Music, member: Member) { | ||
val like = Like ( | ||
musicId = music.id, | ||
memberId = member.id | ||
) | ||
likeRepository.save(like) | ||
music.plusLikeCount() | ||
} | ||
|
||
private fun deleteLike (likeId: Long, music: Music) { | ||
likeRepository.deleteById(likeId) | ||
music.minusLikeCount() | ||
} | ||
|
||
} |
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