-
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.
Merge pull request #87 from grida-diary/main
Docs
- Loading branch information
Showing
22 changed files
with
376 additions
and
116 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
...ore/core-api/src/main/kotlin/org/grida/presentation/v1/diaryimage/DiaryImageController.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,27 @@ | ||
package org.grida.presentation.v1.diaryimage | ||
|
||
import io.wwan13.wintersecurity.resolve.RequestUserId | ||
import org.grida.api.ApiResponse | ||
import org.grida.api.dto.IdResponse | ||
import org.grida.domain.diaryimage.DiaryImageService | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/diary") | ||
class DiaryImageController( | ||
private val diaryImageService: DiaryImageService | ||
) { | ||
|
||
@PostMapping("/{diaryId}/image") | ||
fun generateDiaryImage( | ||
@RequestUserId userId: Long, | ||
@PathVariable diaryId: Long | ||
): ApiResponse<IdResponse> { | ||
val generatedDiaryImageId = diaryImageService.generateDiaryImage(diaryId, userId) | ||
val response = IdResponse(generatedDiaryImageId) | ||
return ApiResponse.success(response) | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
grida-core/core-api/src/test/kotlin/org/grida/docs/diaryimage/DiaryImageApiDocsTest.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,46 @@ | ||
package org.grida.docs.diaryimage | ||
|
||
import com.ninjasquad.springmockk.MockkBean | ||
import io.mockk.every | ||
import io.wwan13.api.document.snippets.NUMBER | ||
import io.wwan13.api.document.snippets.isTypeOf | ||
import io.wwan13.api.document.snippets.whichMeans | ||
import org.grida.docs.ApiDocsTest | ||
import org.grida.domain.diaryimage.DiaryImageService | ||
import org.grida.presentation.v1.diaryimage.DiaryImageController | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest | ||
|
||
@WebMvcTest(controllers = [DiaryImageController::class]) | ||
class DiaryImageApiDocsTest( | ||
private val diaryImageController: DiaryImageController | ||
) : ApiDocsTest( | ||
diaryImageController, | ||
"diary-image" | ||
) { | ||
|
||
@MockkBean | ||
private lateinit var diaryImageService: DiaryImageService | ||
|
||
@Test | ||
fun `일기 이미지 생성 API`() { | ||
every { diaryImageService.generateDiaryImage(any(), any()) } returns 1L | ||
|
||
val api = api.post("/api/v1/diary/{diaryId}/image", 1L) { | ||
withBearerToken() | ||
} | ||
|
||
documentFor(api, "generate-diary-image") { | ||
summary("일기 이미지 생성 API") | ||
requestHeaders( | ||
"Authorization" whichMeans "인증 토큰" | ||
) | ||
pathParameters( | ||
"diaryId" whichMeans "생성하려는 일기의 ID" | ||
) | ||
responseFields( | ||
"data.id" isTypeOf NUMBER whichMeans "생성된 일기 이미지 ID" | ||
) | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImage.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,10 @@ | ||
package org.grida.domain.diaryimage | ||
|
||
import org.grida.domain.image.Image | ||
|
||
data class DiaryImage( | ||
val id: Long = 0, | ||
val userId: Long, | ||
val diaryId: Long, | ||
val image: Image | ||
) |
25 changes: 25 additions & 0 deletions
25
grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageAppender.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,25 @@ | ||
package org.grida.domain.diaryimage | ||
|
||
import org.grida.domain.diary.DiaryReader | ||
import org.grida.domain.user.UserReader | ||
import org.springframework.stereotype.Component | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Component | ||
class DiaryImageAppender( | ||
private val diaryImageRepository: DiaryImageRepository, | ||
private val diaryReader: DiaryReader, | ||
private val userReader: UserReader | ||
) { | ||
|
||
@Transactional | ||
fun append( | ||
diaryImage: DiaryImage, | ||
diaryId: Long, | ||
userId: Long | ||
): Long { | ||
val diary = diaryReader.read(diaryId) | ||
val user = userReader.read(userId) | ||
return diaryImageRepository.save(diaryImage, diary, user) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageGenerator.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,12 @@ | ||
package org.grida.domain.diaryimage | ||
|
||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class DiaryImageGenerator { | ||
|
||
fun generate(diaryContent: String): String { | ||
// TODO | ||
return "https://tmpImageUrl.com" | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageRepository.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,8 @@ | ||
package org.grida.domain.diaryimage | ||
|
||
import org.grida.domain.diary.Diary | ||
import org.grida.domain.user.User | ||
|
||
interface DiaryImageRepository { | ||
fun save(diaryImage: DiaryImage, diary: Diary, user: User): Long | ||
} |
32 changes: 32 additions & 0 deletions
32
grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageService.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,32 @@ | ||
package org.grida.domain.diaryimage | ||
|
||
import org.grida.domain.diary.DiaryReader | ||
import org.grida.domain.diary.DiaryValidator | ||
import org.grida.domain.image.Image | ||
import org.grida.domain.image.ImageStatus | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class DiaryImageService( | ||
private val diaryImageAppender: DiaryImageAppender, | ||
private val diaryImageGenerator: DiaryImageGenerator, | ||
private val diaryReader: DiaryReader, | ||
private val diaryValidator: DiaryValidator | ||
) { | ||
|
||
fun generateDiaryImage( | ||
diaryId: Long, | ||
userId: Long | ||
): Long { | ||
val diary = diaryReader.read(diaryId) | ||
diaryValidator.validateIsOwner(diary, userId) | ||
val generatedImageUrl = diaryImageGenerator.generate(diary.content) | ||
|
||
val diaryImage = DiaryImage( | ||
userId = userId, | ||
diaryId = diaryId, | ||
image = Image(generatedImageUrl, ImageStatus.DEACTIVATE) | ||
) | ||
return diaryImageAppender.append(diaryImage, diaryId, userId) | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
grida-database/database-rds/src/main/kotlin/org/grida/persistence/diary/DiaryEntityMapper.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,26 @@ | ||
package org.grida.persistence.diary | ||
|
||
import org.grida.domain.diary.Diary | ||
import org.grida.domain.user.User | ||
import org.grida.persistence.user.toEntity | ||
|
||
fun Diary.toEntity(user: User): DiaryEntity { | ||
return DiaryEntity( | ||
id = this.id, | ||
targetDate = this.targetDate, | ||
content = this.content, | ||
scope = this.scope, | ||
user = user.toEntity() | ||
) | ||
} | ||
|
||
fun DiaryEntity.toDomain(): Diary { | ||
return Diary( | ||
id = this.id, | ||
timestamp = this.toTimeStamp(), | ||
targetDate = this.targetDate, | ||
content = this.content, | ||
scope = this.scope, | ||
userId = this.user.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
42 changes: 42 additions & 0 deletions
42
...atabase/database-rds/src/main/kotlin/org/grida/persistence/diaryimage/DiaryImageEntity.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,42 @@ | ||
package org.grida.persistence.diaryimage | ||
|
||
import org.grida.domain.diaryimage.DiaryImage | ||
import org.grida.domain.image.ImageStatus | ||
import org.grida.persistence.base.BaseEntity | ||
import org.grida.persistence.diary.DiaryEntity | ||
import org.grida.persistence.user.UserEntity | ||
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 | ||
import javax.persistence.Id | ||
import javax.persistence.JoinColumn | ||
import javax.persistence.ManyToOne | ||
import javax.persistence.Table | ||
|
||
@Entity | ||
@Table(name = "diary_image") | ||
class DiaryImageEntity( | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "diary_image_id") | ||
var id: Long = 0, | ||
|
||
@Column(length = 511) | ||
var imageUrl: String, | ||
|
||
@Enumerated(EnumType.STRING) | ||
@Column(length = 127) | ||
var status: ImageStatus, | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id") | ||
var user: UserEntity, | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "diary_id") | ||
var diary: DiaryEntity, | ||
) : BaseEntity() |
34 changes: 34 additions & 0 deletions
34
...e/database-rds/src/main/kotlin/org/grida/persistence/diaryimage/DiaryImageEntityMapper.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,34 @@ | ||
package org.grida.persistence.diaryimage | ||
|
||
import org.grida.domain.diary.Diary | ||
import org.grida.domain.diaryimage.DiaryImage | ||
import org.grida.domain.image.Image | ||
import org.grida.domain.user.User | ||
import org.grida.persistence.diary.toEntity | ||
import org.grida.persistence.user.toEntity | ||
|
||
fun DiaryImage.toEntity( | ||
user: User, | ||
diary: Diary | ||
): DiaryImageEntity { | ||
return DiaryImageEntity( | ||
id = this.id, | ||
imageUrl = this.image.url, | ||
status = this.image.status, | ||
user = user.toEntity(), | ||
diary = diary.toEntity(user) | ||
) | ||
} | ||
|
||
fun DiaryImageEntity.toDomain(): DiaryImage { | ||
return DiaryImage( | ||
id = this.id, | ||
userId = this.user.id, | ||
diaryId = this.diary.id, | ||
image = Image( | ||
url = imageUrl, | ||
status = this.status, | ||
timestamp = this.toTimeStamp() | ||
) | ||
) | ||
} |
Oops, something went wrong.