-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: dojin <[email protected]>
- Loading branch information
Showing
22 changed files
with
616 additions
and
17 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
3 changes: 3 additions & 0 deletions
3
api/src/main/resources/db/migration/V9__gallery_target_id_idx.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,3 @@ | ||
ALTER TABLE gallery ADD `version` BIGINT; | ||
|
||
CREATE UNIQUE INDEX galley_idx_target_id ON gallery(target_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
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
97 changes: 97 additions & 0 deletions
97
gallery/src/main/kotlin/me/nalab/gallery/app/GalleryPreviewDtoMapper.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,97 @@ | ||
package me.nalab.gallery.app | ||
|
||
import me.nalab.gallery.domain.response.GalleryPreviewDto | ||
import me.nalab.survey.application.common.feedback.dto.BookmarkDto | ||
import me.nalab.survey.application.common.feedback.dto.ChoiceFormQuestionFeedbackDto | ||
import me.nalab.survey.application.common.feedback.dto.FeedbackDto | ||
import me.nalab.survey.application.common.feedback.dto.ShortFormQuestionFeedbackDto | ||
import me.nalab.survey.application.common.survey.dto.ChoiceFormQuestionDto | ||
import me.nalab.survey.application.common.survey.dto.ChoiceFormQuestionDtoType | ||
import me.nalab.survey.application.common.survey.dto.SurveyDto | ||
import me.nalab.survey.application.common.survey.dto.TargetDto | ||
|
||
fun toGalleryPreviewDto( | ||
target: TargetDto, | ||
survey: SurveyDto, | ||
feedbacks: List<FeedbackDto>, | ||
): GalleryPreviewDto { | ||
return GalleryPreviewDto( | ||
target = GalleryPreviewDto.Target( | ||
targetId = target.id.toString(), | ||
nickname = target.nickname, | ||
position = target.position, | ||
), | ||
survey = GalleryPreviewDto.Survey( | ||
surveyId = survey.id.toString(), | ||
feedbackCount = feedbacks.size, | ||
bookmarkedCount = 0, | ||
feedbacks = findLatestBookmarkedReply(feedbacks), | ||
tendencies = findTendencies(survey, feedbacks), | ||
) | ||
) | ||
} | ||
|
||
private fun findLatestBookmarkedReply(feedbacks: List<FeedbackDto>): List<String> { | ||
return feedbacks.filterBookmarkedFeedback() | ||
.mapBookmarkedWithReply() | ||
.sortedByDescending { (bookmark, _) -> bookmark.bookmarkedAt } | ||
.firstOrNull() | ||
?.second ?: emptyList() | ||
} | ||
|
||
private fun List<FeedbackDto>.filterBookmarkedFeedback(): List<FeedbackDto> { | ||
return this.filter { feedback -> | ||
feedback.formQuestionFeedbackDtoableList.any { formQuestionFeedback -> | ||
formQuestionFeedback.bookmarkDto.isBookmarked | ||
} | ||
} | ||
} | ||
|
||
private fun List<FeedbackDto>.mapBookmarkedWithReply(): List<Pair<BookmarkDto, List<String>>> { | ||
return this.flatMap { bookmarkedFeedback -> | ||
bookmarkedFeedback.formQuestionFeedbackDtoableList | ||
.filterIsInstance<ShortFormQuestionFeedbackDto>() | ||
.map { shortQuestionFeedback -> shortQuestionFeedback.bookmarkDto to shortQuestionFeedback.replyList } | ||
} | ||
} | ||
|
||
private fun findTendencies( | ||
survey: SurveyDto, | ||
feedbacks: List<FeedbackDto>, | ||
): List<GalleryPreviewDto.Survey.Tendency> { | ||
val tendencyQuestion = survey.formQuestionDtoableList | ||
.filterIsInstance<ChoiceFormQuestionDto>() | ||
.find { it.choiceFormQuestionDtoType == ChoiceFormQuestionDtoType.TENDENCY } | ||
?: error("필수 유형 Tendency 를 찾을 수 없습니다.") | ||
|
||
val countPerTendency = mutableMapOf<String, Int>() | ||
|
||
feedbacks.mapTendencyFeedbacks(tendencyQuestion) | ||
.forEach { tendencyFeedback -> | ||
tendencyQuestion.choiceDtoList | ||
.filter { choice -> | ||
tendencyFeedback.selectedChoiceIdSet.contains(choice.id) | ||
} | ||
.map { it.content } | ||
.forEach { tendencyContent -> | ||
countPerTendency[tendencyContent] = | ||
countPerTendency.getOrDefault(tendencyContent, 0) + 1 | ||
} | ||
} | ||
|
||
return countPerTendency.map { (name, count) -> | ||
GalleryPreviewDto.Survey.Tendency(name, count) | ||
} | ||
} | ||
|
||
private fun List<FeedbackDto>.mapTendencyFeedbacks( | ||
tendencyQuestion: ChoiceFormQuestionDto, | ||
): List<ChoiceFormQuestionFeedbackDto> { | ||
return this.flatMap { | ||
it.formQuestionFeedbackDtoableList | ||
.filterIsInstance<ChoiceFormQuestionFeedbackDto>() | ||
.filter { choiceQuestionFeedback -> | ||
choiceQuestionFeedback.questionId == tendencyQuestion.id | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
gallery/src/main/kotlin/me/nalab/gallery/app/GalleryRegisterApp.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,51 @@ | ||
package me.nalab.gallery.app | ||
|
||
import me.nalab.core.idgenerator.idcore.IdGenerator | ||
import me.nalab.core.time.TimeUtil | ||
import me.nalab.gallery.domain.Gallery | ||
import me.nalab.gallery.domain.GalleryService | ||
import me.nalab.gallery.domain.Job | ||
import me.nalab.gallery.domain.response.GalleryDto | ||
import me.nalab.survey.application.common.survey.dto.SurveyDto | ||
import me.nalab.survey.application.common.survey.dto.TargetDto | ||
import me.nalab.survey.application.port.`in`.web.findfeedback.FeedbackFindUseCase | ||
import me.nalab.survey.application.port.`in`.web.survey.find.SurveyFindUseCase | ||
import me.nalab.survey.application.port.`in`.web.target.find.TargetFindUseCase | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class GalleryRegisterApp( | ||
private val idGenerator: IdGenerator, | ||
private val targetFindUseCase: TargetFindUseCase, | ||
private val surveyFindUseCase: SurveyFindUseCase, | ||
private val feedbackFindUseCase: FeedbackFindUseCase, | ||
private val galleryService: GalleryService, | ||
) { | ||
|
||
fun registerGalleryByTargetId(targetId: Long, job: String): GalleryDto { | ||
val target = targetFindUseCase.findTarget(targetId) | ||
val survey = surveyFindUseCase.getSurveyByTargetId(targetId) | ||
|
||
val gallery = galleryService.registerGallery(toGallery(job, target, survey)) | ||
|
||
val feedbacks = feedbackFindUseCase.findAllFeedbackDtoBySurveyId(survey.id) | ||
|
||
return toGalleryDto(gallery, target, survey, feedbacks) | ||
} | ||
|
||
private fun toGallery( | ||
job: String, | ||
target: TargetDto, | ||
survey: SurveyDto, | ||
): Gallery { | ||
return Gallery( | ||
id = idGenerator.generate(), | ||
targetId = target.id, | ||
job = Job.valueOf(job.uppercase()), | ||
surveyId = survey.id, | ||
bookmarkedCount = target.bookmarkedSurveys.size, | ||
updateOrder = TimeUtil.toInstant(), | ||
) | ||
} | ||
|
||
} |
19 changes: 14 additions & 5 deletions
19
gallery/src/main/kotlin/me/nalab/gallery/controller/GalleryController.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 |
---|---|---|
@@ -1,23 +1,32 @@ | ||
package me.nalab.gallery.controller | ||
|
||
import me.nalab.gallery.app.GalleryPreviewApp | ||
import me.nalab.gallery.app.GalleryRegisterApp | ||
import me.nalab.gallery.controller.request.GalleryRegisterRequest | ||
import me.nalab.gallery.domain.response.GalleryDto | ||
import me.nalab.gallery.domain.response.GalleryPreviewDto | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.RequestAttribute | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.ResponseStatus | ||
import org.springframework.web.bind.annotation.RestController | ||
import org.springframework.web.bind.annotation.* | ||
|
||
@RestController | ||
@RequestMapping("/v1/gallerys") | ||
class GalleryController( | ||
private val galleryPreviewApp: GalleryPreviewApp, | ||
private val galleryRegisterApp: GalleryRegisterApp, | ||
) { | ||
|
||
@GetMapping("/previews") | ||
@ResponseStatus(HttpStatus.OK) | ||
fun getGalleryPreview(@RequestAttribute("logined") targetId: Long): GalleryPreviewDto = | ||
galleryPreviewApp.findGalleryPreview(targetId) | ||
|
||
@PostMapping | ||
@ResponseStatus(HttpStatus.OK) | ||
fun registerGallery( | ||
@RequestAttribute("logined") targetId: Long, | ||
@RequestBody request: GalleryRegisterRequest, | ||
): GalleryDto { | ||
return galleryRegisterApp.registerGalleryByTargetId(targetId, request.job) | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
gallery/src/main/kotlin/me/nalab/gallery/controller/request/GalleryRegisterRequest.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,9 @@ | ||
package me.nalab.gallery.controller.request | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator | ||
import com.fasterxml.jackson.annotation.JsonProperty | ||
|
||
data class GalleryRegisterRequest @JsonCreator constructor( | ||
@JsonProperty("job") | ||
val job: String, | ||
) |
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
14 changes: 14 additions & 0 deletions
14
gallery/src/main/kotlin/me/nalab/gallery/domain/GalleryRepository.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,14 @@ | ||
package me.nalab.gallery.domain | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.data.jpa.repository.Lock | ||
import org.springframework.data.jpa.repository.Query | ||
import org.springframework.data.repository.query.Param | ||
import javax.persistence.LockModeType | ||
|
||
interface GalleryRepository : JpaRepository<Gallery, Long> { | ||
|
||
@Lock(LockModeType.OPTIMISTIC) | ||
@Query("select g from Gallery as g where g.target.targetId = :targetId") | ||
fun findByTargetIdOrNull(@Param("targetId") targetId: Long): Gallery? | ||
} |
24 changes: 24 additions & 0 deletions
24
gallery/src/main/kotlin/me/nalab/gallery/domain/GalleryService.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,24 @@ | ||
package me.nalab.gallery.domain | ||
|
||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Service | ||
class GalleryService( | ||
private val galleryRepository: GalleryRepository, | ||
) { | ||
|
||
@Transactional | ||
fun registerGallery(gallery: Gallery): Gallery { | ||
require(galleryRepository.findByTargetIdOrNull(gallery.getTargetId()) == null) { | ||
"target \"${gallery.getTargetId()}\" 이 이미 Gallery에 등록되어있습니다." | ||
} | ||
|
||
return galleryRepository.save(gallery) | ||
} | ||
|
||
@Transactional | ||
fun increaseBookmarkCount(targetId: Long) { | ||
galleryRepository.findByTargetIdOrNull(targetId)?.increaseBookmarkedCount() | ||
} | ||
} |
Oops, something went wrong.