Skip to content

Commit

Permalink
Merge pull request #427 from Team-Ampersand/426-return-the-current-nu…
Browse files Browse the repository at this point in the history
…mber-of-likes

좋아요, 좋아요 취소 시 현재 좋아요 수 반환
  • Loading branch information
ta2ye0n authored Sep 17, 2024
2 parents 6f8a6dc + 927abcb commit 69f37de
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 39 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dotori.v2.domain.like.entity
package com.dotori.v2.domain.music.domain.entity

import javax.persistence.Column
import javax.persistence.Entity
Expand All @@ -9,7 +9,7 @@ import javax.persistence.Table

@Entity
@Table(name = "likes")
class Like (
class MusicLike (
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "like_id")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.dotori.v2.domain.music.domain.repository

import com.dotori.v2.domain.music.domain.entity.MusicLike
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 MusicLikeRepository : JpaRepository<MusicLike, Long> {
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("select l from MusicLike l where l.memberId = :memberId and l.musicId = :musicId")
fun findByMemberIdAndMusicId(@Param("memberId") memberId: Long, @Param("musicId") musicId: Long): MusicLike?
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dotori.v2.domain.music.presentation.admin

import com.dotori.v2.domain.music.presentation.data.res.MusicListResDto
import com.dotori.v2.domain.music.presentation.data.res.MusicLikeCountResDto
import com.dotori.v2.domain.music.service.DeleteMusicService
import com.dotori.v2.domain.music.service.FindMusicsService
import com.dotori.v2.domain.music.service.ToggleMusicLikeService
Expand Down Expand Up @@ -32,7 +33,6 @@ class AdminMusicController(
.run { ResponseEntity.status(HttpStatus.NO_CONTENT).build() }

@PatchMapping("/{music_id}/like")
fun toggleMusicLike(@PathVariable("music_id") musicId: Long): ResponseEntity<Void> =
toggleMusicLikeService.execute(musicId)
.run { ResponseEntity.status(HttpStatus.OK).build() }
fun toggleMusicLike(@PathVariable("music_id") musicId: Long): ResponseEntity<MusicLikeCountResDto> =
ResponseEntity.status(HttpStatus.OK).body(toggleMusicLikeService.execute(musicId))
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.dotori.v2.domain.music.presentation.councillor

import com.dotori.v2.domain.music.presentation.data.req.ApplyMusicReqDto
import com.dotori.v2.domain.music.presentation.data.res.MusicListResDto
import com.dotori.v2.domain.music.presentation.data.res.MusicLikeCountResDto
import com.dotori.v2.domain.music.service.ApplyMusicService
import com.dotori.v2.domain.music.service.DeleteMusicService
import com.dotori.v2.domain.music.service.FindMusicsService
Expand Down Expand Up @@ -41,7 +42,6 @@ class CouncillorMusicController(
.run { ResponseEntity.status(HttpStatus.NO_CONTENT).build() }

@PatchMapping("/{music_id}/like")
fun toggleMusicLike(@PathVariable("music_id") musicId: Long): ResponseEntity<Void> =
toggleMusicLikeService.execute(musicId)
.run { ResponseEntity.status(HttpStatus.OK).build() }
fun toggleMusicLike(@PathVariable("music_id") musicId: Long): ResponseEntity<MusicLikeCountResDto> =
ResponseEntity.status(HttpStatus.OK).body(toggleMusicLikeService.execute(musicId))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.dotori.v2.domain.music.presentation.data.res

data class MusicLikeCountResDto(
private val likeCount: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.dotori.v2.domain.music.presentation.developer

import com.dotori.v2.domain.music.presentation.data.req.ApplyMusicReqDto
import com.dotori.v2.domain.music.presentation.data.res.MusicListResDto
import com.dotori.v2.domain.music.presentation.data.res.MusicLikeCountResDto
import com.dotori.v2.domain.music.service.ApplyMusicService
import com.dotori.v2.domain.music.service.DeleteMusicService
import com.dotori.v2.domain.music.service.FindMusicsService
Expand Down Expand Up @@ -41,7 +42,6 @@ class DeveloperMusicController(
.run { ResponseEntity.status(HttpStatus.NO_CONTENT).build() }

@PatchMapping("/{music_id}/like")
fun toggleMusicLike(@PathVariable("music_id") musicId: Long): ResponseEntity<Void> =
toggleMusicLikeService.execute(musicId)
.run { ResponseEntity.status(HttpStatus.OK).build() }
fun toggleMusicLike(@PathVariable("music_id") musicId: Long): ResponseEntity<MusicLikeCountResDto> =
ResponseEntity.status(HttpStatus.OK).body(toggleMusicLikeService.execute(musicId))
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.dotori.v2.domain.music.presentation.member

import com.dotori.v2.domain.music.presentation.data.req.ApplyMusicReqDto
import com.dotori.v2.domain.music.presentation.data.res.MusicListResDto
import com.dotori.v2.domain.music.presentation.data.res.MusicLikeCountResDto
import com.dotori.v2.domain.music.service.ApplyMusicService
import com.dotori.v2.domain.music.service.DeleteMyMusicService
import com.dotori.v2.domain.music.service.FindMusicsService
Expand Down Expand Up @@ -41,7 +42,6 @@ class MemberMusicController(
.run { ResponseEntity.status(HttpStatus.NO_CONTENT).build() }

@PatchMapping("/{music_id}/like")
fun toggleMusicLike(@PathVariable("music_id") musicId: Long): ResponseEntity<Void> =
toggleMusicLikeService.execute(musicId)
.run { ResponseEntity.status(HttpStatus.OK).build() }
fun toggleMusicLike(@PathVariable("music_id") musicId: Long): ResponseEntity<MusicLikeCountResDto> =
ResponseEntity.status(HttpStatus.OK).body(toggleMusicLikeService.execute(musicId))
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dotori.v2.domain.music.service

import com.dotori.v2.domain.music.presentation.data.res.MusicLikeCountResDto

interface ToggleMusicLikeService {
fun execute(musicId: Long)
fun execute(musicId: Long) : MusicLikeCountResDto
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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.music.domain.entity.MusicLike
import com.dotori.v2.domain.music.domain.repository.MusicLikeRepository
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.presentation.data.res.MusicLikeCountResDto
import com.dotori.v2.domain.music.service.ToggleMusicLikeService
import com.dotori.v2.global.util.UserUtil
import org.springframework.data.repository.findByIdOrNull
Expand All @@ -15,19 +16,23 @@ import org.springframework.transaction.annotation.Transactional
@Service
@Transactional(readOnly = false, rollbackFor = [Exception::class])
class ToggleMusicLikeServiceImpl(
private val likeRepository: LikeRepository,
private val musicLikeRepository: MusicLikeRepository,
private val musicRepository: MusicRepository,
private val userUtil: UserUtil
): ToggleMusicLikeService {
override fun execute(musicId: Long) {
override fun execute(musicId: Long) : MusicLikeCountResDto {
val member = userUtil.fetchCurrentUser()
val music: Music = musicRepository.findByIdOrNull(musicId) ?: throw MusicNotFoundException()

updateLike(music, member)

return MusicLikeCountResDto (
likeCount = music.likeCount
)
}

private fun updateLike(music: Music, member: Member) {
val like = likeRepository.findByMemberIdAndMusicId(member.id, music.id)
val like = musicLikeRepository.findByMemberIdAndMusicId(member.id, music.id)

if (like == null) {
saveLike(music, member)
Expand All @@ -37,16 +42,16 @@ class ToggleMusicLikeServiceImpl(
}

private fun saveLike(music: Music, member: Member) {
val like = Like (
val musicLike = MusicLike (
musicId = music.id,
memberId = member.id
)
likeRepository.save(like)
musicLikeRepository.save(musicLike)
music.plusLikeCount()
}

private fun deleteLike (likeId: Long, music: Music) {
likeRepository.deleteById(likeId)
musicLikeRepository.deleteById(likeId)
music.minusLikeCount()
}

Expand Down

0 comments on commit 69f37de

Please sign in to comment.