From eb12046fa5c71d9d3232ae1a72185d726da36ce0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=9C=EC=8B=9C=EC=98=A8=28HAN=2C=20SION=29?= Date: Mon, 5 Aug 2024 18:13:56 +0900 Subject: [PATCH] Implement journal modification (#29) Resolved: #28 --- api-docs.yml | 45 +++++++++++++++++++ .../likelionhgu/stepper/journal/Journal.kt | 10 ++++- .../stepper/journal/JournalController.kt | 17 +++++++ .../stepper/journal/JournalService.kt | 10 +++++ .../stepper/journal/request/JournalRequest.kt | 9 +++- .../stepper/journal/JournalServiceTest.kt | 13 ++++++ 6 files changed, 101 insertions(+), 3 deletions(-) diff --git a/api-docs.yml b/api-docs.yml index 64720d7..b95c368 100644 --- a/api-docs.yml +++ b/api-docs.yml @@ -298,6 +298,51 @@ paths: content: | 미팅 시간에 돌아가면서 진행상황을 공유했다. 각자의 역할은 존중하면서 하나의 프로덕트를 다같이 만들어간다는 느낌이 들어 기분이 좋았다. createdDate: 24.07.17 + put: + tags: + - journals + summary: 일지 수정 + description: " " + operationId: updateJournal + parameters: + - name: journalId + in: path + required: true + schema: + type: string + description: 일지 ID + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/JournalRequest' + examples: + JournalUpdateRequestExample: + value: + title: 중앙해커톤 대상(수정) + content: 수정할 내용 + thumbnail: https://3-namsong-st.s3.ap-northeast-2.amazonaws.com/goals/b46f73d9-fa83-4331-b37d-996897280aa6.jpeg... + responses: + "200": + description: OK + delete: + tags: + - journals + summary: 일지 삭제 + description: " " + operationId: deleteJournal + parameters: + - name: journalId + in: path + required: true + schema: + type: string + description: 일지 ID + responses: + "200": + description: OK + # chat /v1/goals/{goalId}/chats: diff --git a/src/main/kotlin/com/likelionhgu/stepper/journal/Journal.kt b/src/main/kotlin/com/likelionhgu/stepper/journal/Journal.kt index 33f4650..b7fa208 100644 --- a/src/main/kotlin/com/likelionhgu/stepper/journal/Journal.kt +++ b/src/main/kotlin/com/likelionhgu/stepper/journal/Journal.kt @@ -26,13 +26,19 @@ class Journal( @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "member_id") - val member: Member, + val member: Member? = null, @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "goal_id") - val goal: Goal + val goal: Goal? = null ) : BaseTime() { + fun update(targetJournal: Journal) { + title = targetJournal.title + content = targetJournal.content + thumbnail = targetJournal.thumbnail + } + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) val journalId = 0L diff --git a/src/main/kotlin/com/likelionhgu/stepper/journal/JournalController.kt b/src/main/kotlin/com/likelionhgu/stepper/journal/JournalController.kt index d79008d..37adaf7 100644 --- a/src/main/kotlin/com/likelionhgu/stepper/journal/JournalController.kt +++ b/src/main/kotlin/com/likelionhgu/stepper/journal/JournalController.kt @@ -10,9 +10,11 @@ import jakarta.validation.Valid import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import org.springframework.security.core.annotation.AuthenticationPrincipal +import org.springframework.web.bind.annotation.DeleteMapping import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.PutMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.RestController @@ -62,4 +64,19 @@ class JournalController( val responseBody = JournalResponseWrapper.JournalResponse.of(journal) return ResponseEntity.ok(responseBody) } + + @PutMapping("/v1/journals/{journalId}") + fun updateJournal( + @PathVariable journalId: Long, + @Valid @RequestBody journalRequest: JournalRequest + ) { + journalService.updateJournal(journalId, journalRequest) + } + + @DeleteMapping("/v1/journals/{journalId}") + fun deleteJournal( + @PathVariable journalId: Long + ) { + journalService.deleteJournal(journalId) + } } \ No newline at end of file diff --git a/src/main/kotlin/com/likelionhgu/stepper/journal/JournalService.kt b/src/main/kotlin/com/likelionhgu/stepper/journal/JournalService.kt index 2df3972..b239612 100644 --- a/src/main/kotlin/com/likelionhgu/stepper/journal/JournalService.kt +++ b/src/main/kotlin/com/likelionhgu/stepper/journal/JournalService.kt @@ -53,4 +53,14 @@ class JournalService( return journalRepository.findById(journalId).getOrNull() ?: throw JournalNotFoundException("Journal not found with ID: $journalId") } + + fun updateJournal(journalId: Long, journalRequest: JournalRequest) { + val sourceJournal = journalInfo(journalId) + val targetJournal = journalRequest.toEntity() + sourceJournal.update(targetJournal) + } + + fun deleteJournal(journalId: Long) { + journalRepository.deleteById(journalId) + } } diff --git a/src/main/kotlin/com/likelionhgu/stepper/journal/request/JournalRequest.kt b/src/main/kotlin/com/likelionhgu/stepper/journal/request/JournalRequest.kt index bc3b22e..cb73bf5 100644 --- a/src/main/kotlin/com/likelionhgu/stepper/journal/request/JournalRequest.kt +++ b/src/main/kotlin/com/likelionhgu/stepper/journal/request/JournalRequest.kt @@ -1,5 +1,6 @@ package com.likelionhgu.stepper.journal.request +import com.likelionhgu.stepper.journal.Journal import jakarta.validation.constraints.NotBlank import jakarta.validation.constraints.NotNull @@ -13,4 +14,10 @@ data class JournalRequest( val content: String?, val thumbnail: String? = null -) +) { + fun toEntity() = Journal( + title = title!!, + content = content.orEmpty(), + thumbnail = thumbnail + ) +} diff --git a/src/test/kotlin/com/likelionhgu/stepper/journal/JournalServiceTest.kt b/src/test/kotlin/com/likelionhgu/stepper/journal/JournalServiceTest.kt index b66510b..3dadbbd 100644 --- a/src/test/kotlin/com/likelionhgu/stepper/journal/JournalServiceTest.kt +++ b/src/test/kotlin/com/likelionhgu/stepper/journal/JournalServiceTest.kt @@ -5,6 +5,7 @@ import com.likelionhgu.stepper.journal.request.JournalRequest import io.kotest.core.spec.style.BehaviorSpec import io.kotest.extensions.spring.SpringExtension import io.kotest.matchers.collections.shouldHaveSize +import io.kotest.matchers.shouldBe import io.kotest.matchers.shouldNotBe import io.mockk.every import io.mockk.mockk @@ -72,6 +73,18 @@ class JournalServiceTest : BehaviorSpec({ journal shouldNotBe null } } + + `when`("a member tries to update a journal entry") { + then("the journal entry should be updated") { + val target = JournalRequest("Updated Title", "Updated Content") + journalService.updateJournal(0L, target) + + val result = journalService.journalInfo(0L) + + result.title shouldBe "Updated Title" + result.content shouldBe "Updated Content" + } + } } }) { override fun extensions() = listOf(SpringExtension)