Skip to content

Commit

Permalink
Merge pull request #85 from grida-diary/feature/diary
Browse files Browse the repository at this point in the history
Feature/diary
  • Loading branch information
wwan13 authored Jul 30, 2024
2 parents 157de9a + 0fd63f6 commit 7e14432
Show file tree
Hide file tree
Showing 21 changed files with 333 additions and 116 deletions.
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class UserController(
fun signIn(
@RequestBody request: SignInRequest
): ApiResponse<IdResponse> {
val ROLEUser = User(
val user = User(
username = request.username,
password = passwordEncoder.encode(request.password),
nickname = request.username,
role = Role.ROLE_USER
)
val userId = userService.appendNormalUser(ROLEUser, request.passwordConfirm)
val userId = userService.appendNormalUser(user, request.passwordConfirm)
val response = IdResponse(userId)
return ApiResponse.success(response)
}
Expand Down
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
)
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)
}
}
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"
}
}
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
}
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,4 @@ data class Image(
val url: String,
val status: ImageStatus = ImageStatus.ACTIVATE,
val timestamp: Timestamp = Timestamp()
) {
fun activate(): Image {
return Image(this.url, ImageStatus.ACTIVATE)
}

fun deactivate(): Image {
return Image(this.url, ImageStatus.DEACTIVATE)
}
}
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.grida.persistence.diary

import org.grida.domain.diary.Diary
import org.grida.domain.diary.DiaryScope
import org.grida.domain.user.User
import org.grida.persistence.base.BaseEntity
import org.grida.persistence.user.UserEntity
import java.time.LocalDate
Expand Down Expand Up @@ -39,37 +37,11 @@ class DiaryEntity(
var user: UserEntity
) : BaseEntity() {

fun toDiary(): Diary {
return Diary(
id = id,
timestamp = this.toTimeStamp(),
targetDate = targetDate,
content = content,
scope = scope,
userId = user.id
)
}

fun updateContent(content: String) {
this.content = content
}

fun updateScope(scope: DiaryScope) {
this.scope = scope
}

companion object {
fun from(
diary: Diary,
user: User
): DiaryEntity {
return DiaryEntity(
id = diary.id,
targetDate = diary.targetDate,
content = diary.content,
scope = diary.scope,
user = UserEntity.from(user)
)
}
}
}
}
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
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ class DiaryEntityRepository(
diary: Diary,
user: User
): Long {
val diaryEntity = diaryJpaEntityRepository.save(DiaryEntity.from(diary, user))
val diaryEntity = diary.toEntity(user)
diaryJpaEntityRepository.save(diaryEntity)
return diaryEntity.id
}

override fun findById(id: Long): Diary {
val diaryEntity = diaryJpaEntityRepository.findByIdOrException(id)
return diaryEntity.toDiary()
return diaryEntity.toDomain()
}

override fun existsByUserIdAndTargetDate(
Expand Down
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()
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()
)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.grida.persistence.diaryimage

import org.grida.domain.diary.Diary
import org.grida.domain.diaryimage.DiaryImage
import org.grida.domain.diaryimage.DiaryImageRepository
import org.grida.domain.user.User
import org.grida.persistence.diary.DiaryEntity
import org.grida.persistence.user.UserEntity
import org.springframework.stereotype.Repository
import org.springframework.transaction.annotation.Transactional

@Repository
@Transactional(readOnly = true)
class DiaryImageEntityRepository(
private val diaryImageJpaEntityRepository: DiaryImageJpaEntityRepository
) : DiaryImageRepository {

@Transactional
override fun save(
diaryImage: DiaryImage,
diary: Diary,
user: User
): Long {
val diaryEntity = diaryImage.toEntity(
UserEntity.from(user),
DiaryEntity.from(diary, user)
)
diaryImageJpaEntityRepository.save(diaryEntity)
return diaryEntity.id
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.grida.persistence.diaryimage

import org.springframework.data.jpa.repository.JpaRepository

interface DiaryImageJpaEntityRepository : JpaRepository<DiaryImageEntity, Long> {
}
Loading

0 comments on commit 7e14432

Please sign in to comment.