Skip to content

Commit

Permalink
✨feature: feed save 등 기타 문제 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
daehyeon authored and kohmlab committed Mar 8, 2023
1 parent 0997801 commit 01046d2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface CustomFeedRepository {

fun findHomeByUserIdWithContentAndClap(userId: String): Flux<Feed>

fun saveFeed(userId: String): Mono<Feed>
fun saveFeed(userId: String): Mono<Long>
}

@Repository
Expand All @@ -30,37 +30,37 @@ class CustomFeedRepositoryImpl(
val feedId = list[0]["id"] as Long
val userId = list[0]["userId"] as String
val contents = list.stream().map {
val id = it["contentId"] as Long
val feedId = it["feedId"] as Int
val caption = it["caption"] as String
val url = it["url"] as String
val status = it["contentStatus"] as String
// println("id: $id, feedId: $feedId, caption: $caption, url: $url, status: $status")
Content(
id = id,
caption = caption,
url = url,
status = status,
feedId = feedId.toLong()
)
}.collect(Collectors.toList()).distinctBy{ it.id }
// println("feedId: $feedId, contents.count(): ${contents.count()} contents.distinct(): ${contents.distinct()}")
try {
val id = it["contentId"] as Long
val feedIdForContent = it["feedId"] as Int
val caption = it["caption"] as String
val url = it["url"] as String
val status = it["contentStatus"] as String
Content(
id = id,
caption = caption,
url = url,
status = status,
feedId = feedIdForContent.toLong()
)
} catch (e: Exception) {
null
}
}.collect(Collectors.toList()).filterNotNull().distinctBy{ it.id }
val claps = list.stream().map {
try {
val id = it["clapId"] as Long
val userId = it["userId"] as String
val feedId = it["feedId"] as Int
// println("id: $id, userId: $userId, feedId: $feedId")
val userIdForClap = it["userId"]
val feedIdForClap = it["feedId"] as Int
Clap(
id = id,
feedId = feedId.toLong(),
userId = userId
feedId = feedIdForClap.toLong(),
userId = userIdForClap.toString()
)
} catch (e: Exception) {
null
}
}.collect(Collectors.toList()).filterNotNull().distinctBy{ it.id }
// println("feedId: $feedId, claps.count(): ${claps.count()}, claps: $claps")
Feed(id = feedId, userId = userId, contents = contents, claps = claps)
}
return feedMapper
Expand Down Expand Up @@ -98,7 +98,7 @@ class CustomFeedRepositoryImpl(
.all()
.bufferUntilChanged { it -> it["id"] }
.map(feedMapper)
.last()
.singleOrEmpty()
}

override fun findHomeByUserIdWithContentAndClap(userId: String): Flux<Feed> {
Expand Down Expand Up @@ -135,14 +135,14 @@ class CustomFeedRepositoryImpl(
.map(feedMapper)
}

override fun saveFeed(userId: String): Mono<Feed> {
override fun saveFeed(userId: String): Mono<Long> {
val query = "INSERT INTO FeedTB(userId, status) VALUES(:userId, '0');"
return client.sql(query)
.filter {statement, _ -> statement.returnGeneratedValues("id").execute()}
.bind("userId", userId)
.fetch()
.first()
// .map{ findByIdWithContentAndClap(it.get("id")) }
.flatMap { findByIdWithContentAndClap(it["id"] as Long) }
.map { it["id"] as Long }
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.nexters.pimo.feed.adapter.out.persistence

import com.fasterxml.jackson.databind.ObjectMapper
import com.nexters.pimo.common.exception.BadRequestException
import com.nexters.pimo.feed.adapter.`in`.web.dto.ContentInput
import com.nexters.pimo.feed.adapter.`in`.web.dto.FeedInput
Expand All @@ -10,13 +9,12 @@ import com.nexters.pimo.feed.application.port.out.FindPort
import com.nexters.pimo.feed.application.port.out.SavePort
import com.nexters.pimo.feed.domain.Clap
import com.nexters.pimo.feed.domain.Content
import com.nexters.pimo.feed.domain.Feed
import com.nexters.pimo.feed.domain.Report
import org.springframework.stereotype.Repository
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import reactor.core.scheduler.Schedulers
import reactor.kotlin.core.publisher.switchIfEmpty
import reactor.kotlin.core.publisher.toMono

@Repository
class FeedPersistenceAdapter(
Expand All @@ -38,46 +36,19 @@ class FeedPersistenceAdapter(
.map { it.toDto(userId) }

override fun save(input: FeedInput): Mono<Boolean> =
feedRepository.saveFeed(input.userId)
.flatMap {
println(it)
contentRepository.saveAll(Flux.fromIterable(input.contents.map { content ->
Content(
feedId = it.id,
caption = content.caption,
url = content.url
)
})).then()
}.flatMap { Mono.just(true) }
// findById(1, "admin1")
// .flatMap {
// println(it)
// Mono.just(true)
// }
// feedRepository.save(
// Feed(
// userId = input.userId,
// )
// )
// .flatMap {
// println(it)
// Mono.just(true)
////// val contentsForInput = input.contents.map { content ->
////// Content(
////// feedId = it.id,
////// caption = content.caption,
////// url = content.url
////// )
////// }
//// println(it)
//// contentRepository.saveAll(input.contents.map { content ->
//// Content(
//// feedId = it.id,
//// caption = content.caption,
//// url = content.url
//// )
//// }).last().flatMap{ Mono.just(true) }
// }
feedRepository.saveFeed(input.userId).map {
input.contents.map { content ->
Content(
// feedId = it["id"] as Long,
feedId = it,
caption = content.caption,
url = content.url
)
}
}
.publishOn(Schedulers.boundedElastic())
.map { contentRepository.saveAll(it).subscribe() }
.flatMap { Mono.just(true) }

override fun update(feedId: Long, contents: List<ContentInput>, userId: String): Mono<FeedDto> =
contentRepository.deleteAllByFeedId(feedId).flatMap {
Expand All @@ -93,8 +64,8 @@ class FeedPersistenceAdapter(
override fun deleteById(feedId: Long): Mono<Boolean> =
feedRepository.findByIdWithContentAndClap(feedId)
.switchIfEmpty { throw BadRequestException("피드가 존재하지 않습니다.") }
.flatMap { feedRepository.deleteById(feedId) }
.flatMap { Mono.just(true) }
.flatMap { feedRepository.deleteByIdAndStatus(feedId, "0") }
// .flatMap { Mono.just(true) }

override fun clap(feedId: Long, userId: String): Mono<Boolean> =
feedRepository.findByIdWithContentAndClap(feedId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
package com.nexters.pimo.feed.adapter.out.persistence

import com.nexters.pimo.feed.domain.Feed
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.reactive.ReactiveCrudRepository
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono

interface FeedRepository : ReactiveCrudRepository<Feed, Long>, CustomFeedRepository {
fun findAllByUserIdAndStatus(userId: String, status: String): Flux<Feed>
fun findByIdAndStatus(id: Long, status: String): Mono<Feed>
fun findFeedByIdAndStatus(id: Long, status: String): Mono<Feed>

// @Query(
// "select feed.id, feed.userId, feed.status, feed.updatedAt, feed.createdAt " +
// "from FeedTB feed " +
// "join FollowTB follow " +
// "on follow.followerUserId = feed.userId and follow.followeeUserId = :userId " +
// "where feed.status = '0' "+
// "order by feed.createdAt"
// )
// fun findHomeByUserId(userId: String): Flux<Feed>
fun deleteByIdAndStatus(id: Long, status: String): Mono<Boolean>
}

0 comments on commit 01046d2

Please sign in to comment.