-
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.
feat: Add pretotype and basic testing
- Loading branch information
1 parent
fa987fa
commit 38dde39
Showing
10 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
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
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/com/waffletoy/team1server/DomainException.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,17 @@ | ||
package com.waffletoy.team1server | ||
|
||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.HttpStatusCode | ||
|
||
open class DomainException( | ||
// client 와 약속된 Application Error 에 대한 코드 필요 시 Enum 으로 관리하자. | ||
val errorCode: Int, | ||
// HTTP Status Code, 비어있다면 500 이다. | ||
val httpErrorCode: HttpStatusCode = HttpStatus.INTERNAL_SERVER_ERROR, | ||
val msg: String, | ||
cause: Throwable? = null, | ||
) : RuntimeException(msg, cause) { | ||
override fun toString(): String { | ||
return "com.waffletoy.team1server.DomainException(msg='$msg', errorCode=$errorCode, httpErrorCode=$httpErrorCode)" | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/com/waffletoy/team1server/GlobalControllerExceptionHandler.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,15 @@ | ||
package com.waffletoy.team1server | ||
|
||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.ControllerAdvice | ||
import org.springframework.web.bind.annotation.ExceptionHandler | ||
|
||
@ControllerAdvice | ||
class GlobalControllerExceptionHandler { | ||
@ExceptionHandler(DomainException::class) | ||
fun handle(exception: DomainException): ResponseEntity<Map<String, Any>> { | ||
return ResponseEntity | ||
.status(exception.httpErrorCode) | ||
.body(mapOf("error" to exception.msg, "errorCode" to exception.errorCode)) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/com/waffletoy/team1server/pretotype/PretotypeEmailConflictException.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,6 @@ | ||
package com.waffletoy.team1server.pretotype | ||
|
||
import com.waffletoy.team1server.DomainException | ||
import org.springframework.http.HttpStatus | ||
|
||
class PretotypeEmailConflictException: DomainException(0, HttpStatus.CONFLICT, "Email already exists") |
20 changes: 20 additions & 0 deletions
20
src/main/kotlin/com/waffletoy/team1server/pretotype/controller/Pretotype.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,20 @@ | ||
package com.waffletoy.team1server.pretotype.controller | ||
|
||
import com.waffletoy.team1server.pretotype.persistence.PretotypeEntity | ||
import java.time.Instant | ||
|
||
class Pretotype ( | ||
val email: String, | ||
val isSubscribed: Boolean, | ||
val createdAt: Instant, | ||
) { | ||
companion object { | ||
fun fromEntity(entity: PretotypeEntity): Pretotype { | ||
return Pretotype( | ||
email = entity.email, | ||
isSubscribed = entity.isSubscribed, | ||
createdAt = entity.createdAt | ||
) | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/com/waffletoy/team1server/pretotype/controller/PretotypeController.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 com.waffletoy.team1server.pretotype.controller | ||
|
||
import com.waffletoy.team1server.pretotype.service.PretotypeService | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
class TestResponseController ( | ||
private val pretotypeService: PretotypeService, | ||
){ | ||
@PostMapping("/api/pretotype") | ||
fun createPretotype( | ||
@RequestBody request: PretotypeRequest | ||
): ResponseEntity<Pretotype> { | ||
val pretotype = pretotypeService.createPretotype(request.email, request.isSubscribed) | ||
return ResponseEntity.ok(pretotype) | ||
} | ||
|
||
@GetMapping("/api/pretotype/list") | ||
fun listPretotypes(): ResponseEntity<List<Pretotype>> { | ||
val pretotypes = pretotypeService.listPretotypes() | ||
return ResponseEntity.ok(pretotypes) | ||
} | ||
} | ||
|
||
data class PretotypeRequest( | ||
val email: String, | ||
val isSubscribed: Boolean | ||
) |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/com/waffletoy/team1server/pretotype/persistence/PretotypeEntity.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,21 @@ | ||
package com.waffletoy.team1server.pretotype.persistence | ||
|
||
import jakarta.persistence.* | ||
import java.time.Instant | ||
|
||
@Entity | ||
@Table(name = "pretotypes") | ||
class PretotypeEntity( | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
var id: Long? = null, | ||
@Column(unique = true) | ||
var email: String = "", | ||
@Column(name = "is_subscribed") | ||
var isSubscribed: Boolean = false, | ||
@Column(name = "created_at") | ||
var createdAt: Instant = Instant.now() | ||
) { | ||
// No-args constructor for Hibernate | ||
constructor() : this(null, "", false, Instant.now()) | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/com/waffletoy/team1server/pretotype/persistence/PretotypeRepository.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,7 @@ | ||
package com.waffletoy.team1server.pretotype.persistence | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface PretotypeRepository: JpaRepository<PretotypeEntity, String> { | ||
fun findByEmail(email: String): PretotypeEntity? | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/kotlin/com/waffletoy/team1server/pretotype/service/PretotypeService.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,38 @@ | ||
package com.waffletoy.team1server.pretotype.service | ||
|
||
import com.waffletoy.team1server.pretotype.PretotypeEmailConflictException | ||
import com.waffletoy.team1server.pretotype.controller.Pretotype | ||
import com.waffletoy.team1server.pretotype.persistence.PretotypeEntity | ||
import com.waffletoy.team1server.pretotype.persistence.PretotypeRepository | ||
import jakarta.transaction.Transactional | ||
import org.springframework.stereotype.Service | ||
import java.time.Instant | ||
|
||
@Service | ||
class PretotypeService ( | ||
private val pretotypeRepository: PretotypeRepository | ||
){ | ||
@Transactional | ||
fun createPretotype( | ||
email: String, | ||
isSubscribed: Boolean, | ||
): Pretotype { | ||
pretotypeRepository.findByEmail(email) ?.let { | ||
throw PretotypeEmailConflictException() | ||
} ?: run { | ||
val pretotypeEntity = PretotypeEntity( | ||
email = email, | ||
isSubscribed = isSubscribed, | ||
createdAt = Instant.now() | ||
) | ||
pretotypeRepository.save(pretotypeEntity) | ||
return Pretotype.fromEntity(pretotypeEntity) | ||
} | ||
} | ||
|
||
fun listPretotypes(): List<Pretotype> { | ||
return pretotypeRepository.findAll().map { | ||
Pretotype.fromEntity(it) | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/kotlin/com/waffletoy/team1server/ApplicationTests.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 |
---|---|---|
@@ -1,11 +1,24 @@ | ||
package com.waffletoy.team1server | ||
|
||
import com.waffletoy.team1server.pretotype.service.PretotypeService | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.hasSize | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.context.SpringBootTest | ||
|
||
@SpringBootTest | ||
class ApplicationTests { | ||
@Autowired | ||
private val pretotypeService: PretotypeService? = null | ||
|
||
@Test | ||
fun contextLoads() { | ||
} | ||
|
||
@Test | ||
fun whenPretotypeAdded_thenOneItemInList() { | ||
pretotypeService!!.createPretotype("[email protected]", isSubscribed = false) | ||
assertThat(pretotypeService.listPretotypes(), hasSize(1)) | ||
} | ||
} |