Skip to content

Commit

Permalink
Add GoalProof Create API
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Diger committed Dec 9, 2023
1 parent f1e7e19 commit 5d5a99f
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,38 @@ package com.whatever.raisedragon.applicationservice

import com.whatever.raisedragon.controller.goalproof.GoalProofCreateUpdateResponse
import com.whatever.raisedragon.controller.goalproof.GoalProofRetrieveResponse
import com.whatever.raisedragon.domain.goal.GoalService
import com.whatever.raisedragon.domain.goalproof.Document
import com.whatever.raisedragon.domain.goalproof.GoalProofService
import com.whatever.raisedragon.domain.user.UserService
import com.whatever.raisedragon.security.authentication.UserInfo
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional
class GoalProofApplicationService(
private val goalProofService: GoalProofService
private val goalProofService: GoalProofService,
private val goalService: GoalService,
private val userService: UserService,
) {

fun create(): GoalProofCreateUpdateResponse {
fun create(
userInfo: UserInfo,
goalId: Long,
document: Document
): GoalProofCreateUpdateResponse {
val goalProof = goalProofService.create(
user = userService.loadById(userInfo.id),
goal = goalService.loadById(goalId),
document = document
)
return GoalProofCreateUpdateResponse(
GoalProofRetrieveResponse(
id = 0L,
userId = 0L,
goalId = 0L,
document = Document("Fake Document")
id = goalProof.id,
userId = goalProof.userId,
goalId = goalProof.goalId,
document = document
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.whatever.raisedragon.controller.goalproof

import com.whatever.raisedragon.applicationservice.GoalProofApplicationService
import com.whatever.raisedragon.common.Response
import com.whatever.raisedragon.domain.goalproof.Document
import com.whatever.raisedragon.security.authentication.UserInfo
import com.whatever.raisedragon.security.resolver.GetAuth
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.validation.Valid
Expand All @@ -16,12 +19,19 @@ class GoalProofController(
) {

@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "GoalProof create API", description = "Create GoalProof")
@Operation(summary = "GoalProof create API", description = "다짐 인증을 생성합니다.")
@PostMapping
fun create(
@Valid @RequestBody goalProofCreateRequest: GoalProofCreateRequest
@Valid @RequestBody goalProofCreateRequest: GoalProofCreateRequest,
@GetAuth userInfo: UserInfo
): Response<GoalProofCreateUpdateResponse> {
return Response.success(goalProofApplicationService.create())
return Response.success(
goalProofApplicationService.create(
userInfo = userInfo,
goalId = goalProofCreateRequest.goalId,
document = Document(goalProofCreateRequest.document)
)
)
}

@Operation(summary = "GoalProof retrieve API", description = "Retrieve GoalProof")
Expand All @@ -31,20 +41,4 @@ class GoalProofController(
): Response<GoalProofRetrieveResponse> {
return Response.success(goalProofApplicationService.retrieve())
}

@Operation(summary = "GoalProof update API", description = "Update GoalProof")
@PutMapping
fun update(
@RequestBody goalProofUpdateRequest: GoalProofUpdateRequest
): Response<GoalProofCreateUpdateResponse> {
return Response.success(goalProofApplicationService.create())
}

@Operation(summary = "GoalProof delete API", description = "Delete GoalProof")
@DeleteMapping("{goalProofId}")
fun delete(
@PathVariable goalProofId: Long
): Response<Unit> {
return Response.success()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import io.swagger.v3.oas.annotations.media.Schema
data class GoalProofCreateRequest(
@Schema(description = "Goal Id")
val goalId: Long,

@Schema(description = "다짐 인증에 대한 PresignedURL")
val document: String
)

@Schema(description = "[Request] 인증내역 수정")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.whatever.raisedragon.domain.goal

import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDateTime
Expand Down Expand Up @@ -29,4 +30,9 @@ class GoalService(
)
return goal.toDto()
}

@Transactional(readOnly = true)
fun loadById(id: Long): Goal {
return goalRepository.findByIdOrNull(id)?.toDto() ?: throw Exception()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ class GoalProofEntity(
) : BaseEntity()

@Embeddable
data class Document(val document: String)
data class Document(
@Column(name = "document")
val value: String
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
package com.whatever.raisedragon.domain.goalproof

import com.whatever.raisedragon.domain.goal.Goal
import com.whatever.raisedragon.domain.goal.fromDto
import com.whatever.raisedragon.domain.user.User
import com.whatever.raisedragon.domain.user.fromDto
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional
class GoalProofService(
private val goalProofRepository: GoalProofRepository
)
) {

fun create(
user: User,
goal: Goal,
document: Document
): GoalProof {
val goalProof = goalProofRepository.save(
GoalProofEntity(
userEntity = user.fromDto(),
goalEntity = goal.fromDto(),
document = document
)
)
return goalProof.toDto()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.whatever.raisedragon.domain.user

import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

Expand All @@ -14,6 +15,11 @@ class UserService(
return userRepository.findByOauthTokenPayload(payload)?.toDto()
}

@Transactional(readOnly = true)
fun loadById(id: Long): User {
return userRepository.findByIdOrNull(id)?.toDto() ?: throw Exception()
}

fun create(user: User): User {
return userRepository.save(user.fromDto()).toDto()
}
Expand Down

0 comments on commit 5d5a99f

Please sign in to comment.