Skip to content

Commit

Permalink
feat(#53): converted Like Service java to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
KimDoubleB committed Jan 21, 2023
1 parent 388314f commit 1244b2f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 57 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package site.archive.service.like

import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import site.archive.common.exception.common.ResourceNotFoundException
import site.archive.domain.common.BaseTimeEntity
import site.archive.domain.like.Like
import site.archive.domain.like.LikeRepository

@Service
@Transactional(readOnly = true)
class LikeService(private val likeRepository: LikeRepository) {

@Transactional
fun save(userId: Long, archiveId: Long) {
likeRepository.findByUserIdAndArchiveId(userId, archiveId)
.ifPresentOrElse(BaseTimeEntity::softDeleteCancel) { likeRepository.save(Like.of(userId, archiveId)) }
}

@Transactional
fun save(userId: Long, archiveIds: List<Long>) {
archiveIds.forEach {
likeRepository.findByUserIdAndArchiveId(userId, it)
.ifPresentOrElse(BaseTimeEntity::softDeleteCancel) { likeRepository.save(Like.of(userId, it)) }
}
}

@Transactional
fun delete(userId: Long, archiveId: Long) {
likeRepository.findByUserIdAndArchiveId(userId, archiveId)
.ifPresentOrElse(likeRepository::delete) { throw ResourceNotFoundException("조건에 맞는 Like 데이터가 없습니다") }
}

@Transactional
fun delete(userId: Long, archiveIds: List<Long>) {
archiveIds.forEach {
likeRepository.findByUserIdAndArchiveId(userId, it)
.ifPresentOrElse(likeRepository::delete) { throw ResourceNotFoundException("조건에 맞는 Like 데이터가 없습니다") }
}
}

fun likeArchiveIds(userId: Long): List<Long> {
return likeRepository.findAllByUserId(userId)
.filter { !it.isDeleted }
.map { it.archive.id }
.toList()
}

}

0 comments on commit 1244b2f

Please sign in to comment.