From c77ec6373b18700484ed6c6449a7a7e52f8b4a54 Mon Sep 17 00:00:00 2001 From: wwan13 Date: Sun, 28 Jul 2024 19:07:10 +0900 Subject: [PATCH 1/5] =?UTF-8?q?refactor=20:=20=EC=9A=94=EC=B2=AD=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=20=EC=B6=9C=EB=A0=A5=20=ED=98=95=EC=8B=9D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../grida/support/requestlogger/LogFilter.kt | 7 ++- .../requestlogger/LogFilterRegistrar.kt | 7 ++- .../requestlogger/RequestLogContext.kt | 48 +++++++++---------- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/LogFilter.kt b/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/LogFilter.kt index 09328717..1dd9fcf7 100644 --- a/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/LogFilter.kt +++ b/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/LogFilter.kt @@ -1,5 +1,6 @@ package org.grida.support.requestlogger +import com.fasterxml.jackson.databind.ObjectMapper import mu.KotlinLogging import org.springframework.web.filter.OncePerRequestFilter import org.springframework.web.util.ContentCachingRequestWrapper @@ -10,7 +11,9 @@ import javax.servlet.http.HttpServletResponse private val log = KotlinLogging.logger {} -class LogFilter : OncePerRequestFilter() { +class LogFilter( + private val objectMapper: ObjectMapper +) : OncePerRequestFilter() { override fun doFilterInternal( request: HttpServletRequest, @@ -26,7 +29,7 @@ class LogFilter : OncePerRequestFilter() { val requestElapsed = (requestCompleted - requestOccurred) / 1000.0 val logContext = RequestLogContext.of(requestWrapper, responseWrapper, requestElapsed) - log.info(logContext.toLogMessage()) + log.info(logContext.toLogMessage(objectMapper)) responseWrapper.copyBodyToResponse() } diff --git a/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/LogFilterRegistrar.kt b/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/LogFilterRegistrar.kt index 11c0bc33..049f27b0 100644 --- a/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/LogFilterRegistrar.kt +++ b/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/LogFilterRegistrar.kt @@ -1,14 +1,17 @@ package org.grida.support.requestlogger +import com.fasterxml.jackson.databind.ObjectMapper import org.springframework.boot.web.servlet.FilterRegistrationBean import org.springframework.context.annotation.Bean class LogFilterRegistrar { @Bean - fun logFilter(): FilterRegistrationBean { + fun logFilter( + objectMapper: ObjectMapper + ): FilterRegistrationBean { val filterRegistration = FilterRegistrationBean() - filterRegistration.filter = LogFilter() + filterRegistration.filter = LogFilter(objectMapper) filterRegistration.order = 0 return filterRegistration } diff --git a/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/RequestLogContext.kt b/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/RequestLogContext.kt index 61370594..a0a683ab 100644 --- a/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/RequestLogContext.kt +++ b/grida-core/core-api/src/main/kotlin/org/grida/support/requestlogger/RequestLogContext.kt @@ -1,49 +1,34 @@ package org.grida.support.requestlogger +import com.fasterxml.jackson.databind.ObjectMapper import org.springframework.http.HttpStatus import org.springframework.web.util.ContentCachingRequestWrapper import org.springframework.web.util.ContentCachingResponseWrapper - -fun ContentCachingRequestWrapper.getRequestHeaders(): String { - val headers = this.headerNames.toList().map { "\"$it\":\"${this.getHeader(it)}\"" } - val joinedHeaders = headers.joinToString(", ") - return "[$joinedHeaders]" -} - -fun ContentCachingRequestWrapper.getRequestParams(): String { - val params = this.parameterNames.toList().map { "\"$it\":\"${this.getParameter(it)}\"" } - val joinedHeaders = params.joinToString(", ") - return "[$joinedHeaders]" -} - -fun String.trimSpaceAndNewLine(): String { - return this.replace("\\n".toRegex(), "").replace(" ", "") -} +import java.util.Enumeration data class RequestLogContext( val method: String, val uri: String, val status: HttpStatus, val elapsed: Double, - val requestHeaders: String, - val requestParams: String, + val requestHeaders: Map, + val requestParams: Map, val requestBody: String, val responseBody: String ) { - fun toLogMessage(): String { + fun toLogMessage(objectMapper: ObjectMapper): String { return """ | |$method $uri - $status ($elapsed s) |>> REQUEST HEADERS : $requestHeaders |>> REQUEST PARAMS : $requestParams - |>> REQUEST BODY : $requestBody - |>> RESPONSE BODY : $responseBody + |>> REQUEST BODY : ${objectMapper.readTree(requestBody.ifBlank { "{}" })} + |>> RESPONSE BODY : ${objectMapper.readTree(responseBody)} """.trimMargin() } companion object { - fun of( request: ContentCachingRequestWrapper, response: ContentCachingResponseWrapper, @@ -54,11 +39,22 @@ data class RequestLogContext( uri = request.requestURI, status = HttpStatus.valueOf(response.status), elapsed = elapsed, - requestHeaders = request.getRequestHeaders(), - requestParams = request.getRequestParams(), - requestBody = String(request.contentAsByteArray).trimSpaceAndNewLine(), - responseBody = String(response.contentAsByteArray).trimSpaceAndNewLine(), + requestHeaders = extractAsMap(request, request.headerNames), + requestParams = extractAsMap(request, request.parameterNames), + requestBody = String(request.contentAsByteArray), + responseBody = String(response.contentAsByteArray), ) } + + private fun extractAsMap( + request: ContentCachingRequestWrapper, + names: Enumeration + ): Map { + val result = mutableMapOf() + names.asIterator().forEach { + result.put(it, request.getHeader(it)) + } + return result + } } } From e683165b72c4b043c83e151532e5f15d79894948 Mon Sep 17 00:00:00 2001 From: wwan13 Date: Mon, 29 Jul 2024 13:36:14 +0900 Subject: [PATCH 2/5] =?UTF-8?q?refactor=20:=20=ED=94=84=EB=A1=9C=ED=95=84?= =?UTF-8?q?=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EC=83=81=ED=83=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/grida/domain/profileimage/ProfileImage.kt | 8 -------- .../grida/domain/profileimage/ProfileImageModifier.kt | 4 ++-- .../domain/profileimage/ProfileImageRepository.kt | 2 +- .../persistence/profileimage/ProfileImageEntity.kt | 10 ++-------- .../profileimage/ProfileImageEntityRepository.kt | 9 ++++++--- 5 files changed, 11 insertions(+), 22 deletions(-) diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImage.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImage.kt index 9c454b80..ace4889a 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImage.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImage.kt @@ -11,12 +11,4 @@ data class ProfileImage( fun isOwner(accessorId: Long): Boolean { return userId == accessorId } - - fun activate(): ProfileImage { - return ProfileImage(id, userId, image.activate(), appearance) - } - - fun deactivate(): ProfileImage { - return ProfileImage(id, userId, image.deactivate(), appearance) - } } diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageModifier.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageModifier.kt index d3cbadda..ae450493 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageModifier.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageModifier.kt @@ -19,13 +19,13 @@ class ProfileImageModifier( val profileImage = profileImageReader.read(profileImageId) profileImageValidator.validateIsOwner(profileImage, userId) - profileImageRepository.update(profileImage.activate()) + profileImageRepository.updateStatue(profileImageId, ImageStatus.ACTIVATE) } @Transactional fun modifyOriginalProfileImageAsDeactivate(userId: Long) { val originalProfileImage = profileImageReader.readActivateProfileImage(userId) - profileImageRepository.update(originalProfileImage.deactivate()) + profileImageRepository.updateStatue(originalProfileImage.id, ImageStatus.DEACTIVATE) } } diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageRepository.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageRepository.kt index 7b9eeffe..f8af6940 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageRepository.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageRepository.kt @@ -15,5 +15,5 @@ interface ProfileImageRepository { fun existsByUserIdAndStatus(userId: Long, status: ImageStatus): Boolean - fun update(profileImage: ProfileImage): Long + fun updateStatue(profileImageId: Long, status: ImageStatus): Long } diff --git a/grida-database/database-rds/src/main/kotlin/org/grida/persistence/profileimage/ProfileImageEntity.kt b/grida-database/database-rds/src/main/kotlin/org/grida/persistence/profileimage/ProfileImageEntity.kt index bbacfc14..dc392bcc 100644 --- a/grida-database/database-rds/src/main/kotlin/org/grida/persistence/profileimage/ProfileImageEntity.kt +++ b/grida-database/database-rds/src/main/kotlin/org/grida/persistence/profileimage/ProfileImageEntity.kt @@ -69,14 +69,8 @@ class ProfileImageEntity( ) } - fun update(profileImage: ProfileImage) { - this.imageUrl = profileImage.image.url - this.status = profileImage.image.status - this.gender = profileImage.appearance.gender - this.age = profileImage.appearance.age - this.hairStyle = profileImage.appearance.hairStyle - this.glasses = profileImage.appearance.glasses - this.bodyShape = profileImage.appearance.bodyShape + fun updateStatue(status: ImageStatus) { + this.status = status } companion object { diff --git a/grida-database/database-rds/src/main/kotlin/org/grida/persistence/profileimage/ProfileImageEntityRepository.kt b/grida-database/database-rds/src/main/kotlin/org/grida/persistence/profileimage/ProfileImageEntityRepository.kt index 35f2a9f4..96ea7537 100644 --- a/grida-database/database-rds/src/main/kotlin/org/grida/persistence/profileimage/ProfileImageEntityRepository.kt +++ b/grida-database/database-rds/src/main/kotlin/org/grida/persistence/profileimage/ProfileImageEntityRepository.kt @@ -44,9 +44,12 @@ class ProfileImageEntityRepository( } @Transactional - override fun update(profileImage: ProfileImage): Long { - val profileImageEntity = profileImageJpaEntityRepository.findByIdOrException(profileImage.id) - profileImageEntity.update(profileImage) + override fun updateStatue( + profileImageId: Long, + status: ImageStatus + ): Long { + val profileImageEntity = profileImageJpaEntityRepository.findByIdOrException(profileImageId) + profileImageEntity.updateStatue(status) return profileImageEntity.id } } From af0d0a2ffd3521c2fd588d94f64e12fe7a8a1e07 Mon Sep 17 00:00:00 2001 From: wwan13 Date: Mon, 29 Jul 2024 13:42:12 +0900 Subject: [PATCH 3/5] =?UTF-8?q?feat=20:=20=EC=9D=BC=EA=B8=B0=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../v1/diary/dto/DiaryModifyRequest.kt | 6 +++ .../org/grida/domain/diary/DiaryAppender.kt | 2 + .../org/grida/domain/diary/DiaryModifier.kt | 38 +++++++++++++++++++ .../org/grida/domain/diary/DiaryReader.kt | 2 +- .../org/grida/domain/diary/DiaryRepository.kt | 4 ++ .../org/grida/domain/diary/DiaryService.kt | 27 ++++++++++++- .../org/grida/domain/diary/DiaryValidator.kt | 2 +- .../grida/persistence/diary/DiaryEntity.kt | 13 ++++++- .../diary/DiaryEntityRepository.kt | 15 ++++++++ 9 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 grida-core/core-api/src/main/kotlin/org/grida/presentation/v1/diary/dto/DiaryModifyRequest.kt create mode 100644 grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryModifier.kt diff --git a/grida-core/core-api/src/main/kotlin/org/grida/presentation/v1/diary/dto/DiaryModifyRequest.kt b/grida-core/core-api/src/main/kotlin/org/grida/presentation/v1/diary/dto/DiaryModifyRequest.kt new file mode 100644 index 00000000..d8ef4758 --- /dev/null +++ b/grida-core/core-api/src/main/kotlin/org/grida/presentation/v1/diary/dto/DiaryModifyRequest.kt @@ -0,0 +1,6 @@ +package org.grida.presentation.v1.diary.dto + +data class DiaryModifyRequest( + val content: String, + val scope: String +) diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryAppender.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryAppender.kt index 3ec7ef99..7ac62447 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryAppender.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryAppender.kt @@ -2,6 +2,7 @@ package org.grida.domain.diary import org.grida.domain.user.UserReader import org.springframework.stereotype.Component +import org.springframework.transaction.annotation.Transactional @Component class DiaryAppender( @@ -9,6 +10,7 @@ class DiaryAppender( private val userReader: UserReader ) { + @Transactional fun append( diary: Diary ): Long { diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryModifier.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryModifier.kt new file mode 100644 index 00000000..aebe7078 --- /dev/null +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryModifier.kt @@ -0,0 +1,38 @@ +package org.grida.domain.diary + +import org.springframework.stereotype.Component +import org.springframework.transaction.annotation.Transactional + +@Component +class DiaryModifier( + private val diaryRepository: DiaryRepository, + private val diaryReader: DiaryReader, + private val diaryValidator: DiaryValidator +) { + + @Transactional + fun modify( + diaryId: Long, + userId: Long, + content: String, + scope: DiaryScope + ) { + val diary = diaryReader.read(diaryId) + diaryValidator.validateIsOwner(diary, userId) + + diaryRepository.updateContent(diaryId, content) + diaryRepository.updateScope(diaryId, scope) + } + + @Transactional + fun modifyScope( + diaryId: Long, + userId: Long, + scope: DiaryScope + ) { + val diary = diaryReader.read(diaryId) + diaryValidator.validateIsOwner(diary, userId) + + diaryRepository.updateScope(diaryId, scope) + } +} \ No newline at end of file diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryReader.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryReader.kt index a56950ae..fe4c54b2 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryReader.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryReader.kt @@ -9,7 +9,7 @@ class DiaryReader( ) { @Transactional(readOnly = true) - fun read(diaryId: Long, userId: Long): Diary { + fun read(diaryId: Long): Diary { return diaryRepository.findById(diaryId) } } \ No newline at end of file diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryRepository.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryRepository.kt index ae974734..a92efc7c 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryRepository.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryRepository.kt @@ -10,4 +10,8 @@ interface DiaryRepository { fun findById(id: Long): Diary fun existsByUserIdAndTargetDate(userId: Long, targetDate: LocalDate): Boolean + + fun updateContent(diaryId: Long, content: String): Long + + fun updateScope(diaryId: Long, scope: DiaryScope): Long } \ No newline at end of file diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryService.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryService.kt index be327dea..4a3fc74b 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryService.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryService.kt @@ -6,6 +6,7 @@ import org.springframework.stereotype.Service class DiaryService( private val diaryAppender: DiaryAppender, private val diaryReader: DiaryReader, + private val diaryModifier: DiaryModifier, private val diaryValidator: DiaryValidator ) { @@ -16,10 +17,32 @@ class DiaryService( return diaryAppender.append(diary) } - fun readDiary(diaryId: Long, userId: Long): Diary { - val diary = diaryReader.read(diaryId, userId) + fun readDiary( + diaryId: Long, + userId: Long + ): Diary { + val diary = diaryReader.read(diaryId) diaryValidator.validateCanAccess(diary, userId) return diary } + + fun modify( + diaryId: Long, + userId: Long, + content: String, + scope: DiaryScope + ): Long { + diaryModifier.modify(diaryId, userId, content, scope) + return diaryId + } + + fun modifyScope( + diaryId: Long, + userId: Long, + scope: DiaryScope + ): Long { + diaryModifier.modifyScope(diaryId, userId, scope) + return diaryId + } } \ No newline at end of file diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryValidator.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryValidator.kt index 3d74221a..99a69ef6 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryValidator.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryValidator.kt @@ -43,7 +43,7 @@ class DiaryValidator( throw GridaException(AccessFailed) } - private fun validateIsOwner(diary: Diary, userId: Long) { + fun validateIsOwner(diary: Diary, userId: Long) { if (!diary.isOwner(userId)) { throw GridaException(AccessFailed) } diff --git a/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diary/DiaryEntity.kt b/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diary/DiaryEntity.kt index efd42c1b..068f4c46 100644 --- a/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diary/DiaryEntity.kt +++ b/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diary/DiaryEntity.kt @@ -8,6 +8,8 @@ import org.grida.persistence.user.UserEntity import java.time.LocalDate import javax.persistence.Column import javax.persistence.Entity +import javax.persistence.EnumType +import javax.persistence.Enumerated import javax.persistence.FetchType import javax.persistence.GeneratedValue import javax.persistence.GenerationType @@ -26,9 +28,10 @@ class DiaryEntity( var targetDate: LocalDate, + @Column(length = 4095) var content: String, - @Column(length = 4095) + @Enumerated(EnumType.STRING) var scope: DiaryScope, @ManyToOne(fetch = FetchType.LAZY) @@ -47,6 +50,14 @@ class DiaryEntity( ) } + fun updateContent(content: String) { + this.content = content + } + + fun updateScope(scope: DiaryScope) { + this.scope = scope + } + companion object { fun from( diary: Diary, diff --git a/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diary/DiaryEntityRepository.kt b/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diary/DiaryEntityRepository.kt index da91d8c6..7fda38ec 100644 --- a/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diary/DiaryEntityRepository.kt +++ b/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diary/DiaryEntityRepository.kt @@ -2,6 +2,7 @@ package org.grida.persistence.diary import org.grida.domain.diary.Diary import org.grida.domain.diary.DiaryRepository +import org.grida.domain.diary.DiaryScope import org.grida.domain.user.User import org.springframework.stereotype.Repository import org.springframework.transaction.annotation.Transactional @@ -33,4 +34,18 @@ class DiaryEntityRepository( ): Boolean { return diaryJpaEntityRepository.existsByUserIdAndTargetDate(userId, targetDate) } + + @Transactional + override fun updateContent(diaryId: Long, content: String): Long { + val diaryEntity = diaryJpaEntityRepository.findByIdOrException(diaryId) + diaryEntity.updateContent(content) + return diaryEntity.id + } + + @Transactional + override fun updateScope(diaryId: Long, scope: DiaryScope): Long { + val diaryEntity = diaryJpaEntityRepository.findByIdOrException(diaryId) + diaryEntity.updateScope(scope) + return diaryEntity.id + } } \ No newline at end of file From fa083cf97d434d5af43f7cbbe93bce967c1014ea Mon Sep 17 00:00:00 2001 From: wwan13 Date: Mon, 29 Jul 2024 13:42:22 +0900 Subject: [PATCH 4/5] =?UTF-8?q?feat=20:=20=EC=9D=BC=EA=B8=B0=20=EA=B3=B5?= =?UTF-8?q?=EA=B0=9C=20=EB=B2=94=EC=9C=84=20=EC=88=98=EC=A0=95=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/v1/diary/DiaryController.kt | 35 +++++++++++++++++++ .../v1/diary/dto/DiaryScopeRequest.kt | 5 +++ 2 files changed, 40 insertions(+) create mode 100644 grida-core/core-api/src/main/kotlin/org/grida/presentation/v1/diary/dto/DiaryScopeRequest.kt diff --git a/grida-core/core-api/src/main/kotlin/org/grida/presentation/v1/diary/DiaryController.kt b/grida-core/core-api/src/main/kotlin/org/grida/presentation/v1/diary/DiaryController.kt index 333d952e..a43b66d1 100644 --- a/grida-core/core-api/src/main/kotlin/org/grida/presentation/v1/diary/DiaryController.kt +++ b/grida-core/core-api/src/main/kotlin/org/grida/presentation/v1/diary/DiaryController.kt @@ -3,10 +3,14 @@ package org.grida.presentation.v1.diary import io.wwan13.wintersecurity.resolve.RequestUserId import org.grida.api.ApiResponse import org.grida.api.dto.IdResponse +import org.grida.domain.diary.DiaryScope import org.grida.domain.diary.DiaryService +import org.grida.presentation.v1.diary.dto.DiaryModifyRequest import org.grida.presentation.v1.diary.dto.DiaryRequest import org.grida.presentation.v1.diary.dto.DiaryResponse +import org.grida.presentation.v1.diary.dto.DiaryScopeRequest import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PatchMapping import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody @@ -38,4 +42,35 @@ class DiaryController( val response = DiaryResponse.from(diary) return ApiResponse.success(response) } + + @PatchMapping("/{diaryId}") + fun modifyDiary( + @RequestUserId userId: Long, + @PathVariable diaryId: Long, + @RequestBody request: DiaryModifyRequest + ): ApiResponse { + val modifiedDiaryId = diaryService.modify( + diaryId, + userId, + request.content, + DiaryScope.valueOf(request.scope) + ) + val response = IdResponse(modifiedDiaryId) + return ApiResponse.success(response) + } + + @PatchMapping("/{diaryId}/scope") + fun modifyDiaryScope( + @RequestUserId userId: Long, + @PathVariable diaryId: Long, + @RequestBody request: DiaryScopeRequest + ): ApiResponse { + val modifiedDiaryId = diaryService.modifyScope( + diaryId, + userId, + DiaryScope.valueOf(request.scope) + ) + val response = IdResponse(modifiedDiaryId) + return ApiResponse.success(response) + } } diff --git a/grida-core/core-api/src/main/kotlin/org/grida/presentation/v1/diary/dto/DiaryScopeRequest.kt b/grida-core/core-api/src/main/kotlin/org/grida/presentation/v1/diary/dto/DiaryScopeRequest.kt new file mode 100644 index 00000000..1476993b --- /dev/null +++ b/grida-core/core-api/src/main/kotlin/org/grida/presentation/v1/diary/dto/DiaryScopeRequest.kt @@ -0,0 +1,5 @@ +package org.grida.presentation.v1.diary.dto + +data class DiaryScopeRequest( + val scope: String +) From ed0b596cf004b930928712e44651b73577c1b535 Mon Sep 17 00:00:00 2001 From: wwan13 Date: Mon, 29 Jul 2024 13:42:33 +0900 Subject: [PATCH 5/5] =?UTF-8?q?feat=20:=20=EC=9D=BC=EA=B8=B0=20=EC=88=98?= =?UTF-8?q?=EC=A0=95,=20=EA=B3=B5=EA=B0=9C=20=EB=B2=94=EC=9C=84=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20API=20=EB=AC=B8=EC=84=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/grida/docs/diary/DiaryApiDocsTest.kt | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/grida-core/core-api/src/test/kotlin/org/grida/docs/diary/DiaryApiDocsTest.kt b/grida-core/core-api/src/test/kotlin/org/grida/docs/diary/DiaryApiDocsTest.kt index b01f6a3e..d48cb8ef 100644 --- a/grida-core/core-api/src/test/kotlin/org/grida/docs/diary/DiaryApiDocsTest.kt +++ b/grida-core/core-api/src/test/kotlin/org/grida/docs/diary/DiaryApiDocsTest.kt @@ -14,7 +14,9 @@ import org.grida.domain.diary.Diary import org.grida.domain.diary.DiaryScope import org.grida.domain.diary.DiaryService import org.grida.presentation.v1.diary.DiaryController +import org.grida.presentation.v1.diary.dto.DiaryModifyRequest import org.grida.presentation.v1.diary.dto.DiaryRequest +import org.grida.presentation.v1.diary.dto.DiaryScopeRequest import org.junit.jupiter.api.Test import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest import java.time.LocalDate @@ -90,4 +92,60 @@ class DiaryApiDocsTest( ) } } + + @Test + fun `일기 수정 API`() { + every { diaryService.modify(any(), any(), any(), any()) } returns 1L + + val api = api.patch("/api/v1/diary/{diaryId}", 1L) { + withBearerToken() + requestBody( + DiaryModifyRequest( + content = "수정할 일기 콘텐츠", + scope = "PUBLIC" + ) + ) + } + + documentFor(api, "modify-diary") { + summary("일기 생성 API") + requestHeaders( + "Authorization" whichMeans "인증 토큰" + ) + requestFields( + "content" isTypeOf STRING whichMeans "수정할 일기 콘텐츠", + "scope" isTypeOf ENUM(DiaryScope::class) whichMeans "일기 공개 범위" + ) + responseFields( + "data.id" isTypeOf NUMBER whichMeans "수정돤 일기 ID" + ) + } + } + + @Test + fun `일기 공개 범위 수정 API`() { + every { diaryService.modifyScope(any(), any(), any()) } returns 1L + + val api = api.patch("/api/v1/diary/{diaryId}/scope", 1L) { + withBearerToken() + requestBody( + DiaryScopeRequest( + scope = "PUBLIC" + ) + ) + } + + documentFor(api, "modify-diary") { + summary("일기 생성 API") + requestHeaders( + "Authorization" whichMeans "인증 토큰" + ) + requestFields( + "scope" isTypeOf ENUM(DiaryScope::class) whichMeans "일기 공개 범위" + ) + responseFields( + "data.id" isTypeOf NUMBER whichMeans "수정돤 일기 ID" + ) + } + } }