Skip to content

Commit

Permalink
More refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarcpozas committed Mar 27, 2024
1 parent 7d840e1 commit 67a5acf
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,17 @@ import io.swagger.v3.oas.annotations.media.Content
import io.swagger.v3.oas.annotations.media.Schema
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.responses.ApiResponses
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.http.HttpStatus
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.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
import oscar.c.pozas.playgroud.context.pokemon.app.controller.inputmodel.PokemonInputModel
import org.springframework.web.bind.annotation.*
import oscar.c.pozas.playgroud.context.pokemon.app.controller.viewmodel.PokemonViewModel
import oscar.c.pozas.playgroud.context.pokemon.app.controller.viewmodel.toViewModel
import oscar.c.pozas.playgroud.context.pokemon.domain.Pokemon
import oscar.c.pozas.playgroud.context.pokemon.domain.service.PokemonCreator
import oscar.c.pozas.playgroud.context.pokemon.domain.service.PokemonFinder
import oscar.c.pozas.playgroud.context.pokemon.app.usecase.GetPokemonByIdCommand
import oscar.c.pozas.playgroud.context.pokemon.app.usecase.GetPokemonByIdCommandHandler

@RestController
@RequestMapping("public/v1/pokemon")
@Tag(name = "Pokemon public API")
class PokemonController(
private val pokemonFinder: PokemonFinder,
private val pokemonCreator: PokemonCreator,
class GetPokemonController(
private val getPokemonById: GetPokemonByIdCommandHandler
) {

@GetMapping("/{id}")
Expand All @@ -50,11 +39,5 @@ class PokemonController(
]
)
@ResponseStatus(HttpStatus.OK)
fun findById(@PathVariable("id") id: Int): PokemonViewModel =
pokemonFinder(Pokemon.Id(id))!!.toViewModel()

@PostMapping("/")
@Operation(summary = "Create a new custom Pokemon")
@ResponseStatus(HttpStatus.CREATED)
fun create(@RequestBody input: PokemonInputModel) = pokemonCreator(input.toDomain())
}
fun findById(@PathVariable("id") id: Int): PokemonViewModel = getPokemonById(GetPokemonByIdCommand(id)).toViewModel()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package oscar.c.pozas.playgroud.context.pokemon.app.usecase

import oscar.c.pozas.playgroud.context.pokemon.domain.Pokemon
import oscar.c.pozas.playgroud.context.pokemon.domain.service.PokemonFinder

class GetPokemonByIdCommandHandler(private val pokemonFinder: PokemonFinder) {

operator fun invoke(command: GetPokemonByIdCommand): Pokemon {
val id = Pokemon.Id(command.id)
return pokemonFinder(id)!!
}
}

data class GetPokemonByIdCommand(val id: Int)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ data class Pokemon(
data class Id(val value: Int)

data class Name(val value: String)


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@ import oscar.c.pozas.playgroud.context.pokemon.domain.Pokemon

interface PokemonRepository {

fun create(pokemon: Pokemon): Pokemon

fun findById(id: Pokemon.Id): Pokemon?
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class PokemonDataRepository(
private val pokemonStoreDataSource: PokemonStoreDataSource
) : PokemonRepository {

override fun create(pokemon: Pokemon) = pokemonStoreDataSource.save(pokemon)

@Cacheable(value = ["pokemon"])
override fun findById(id: Pokemon.Id): Pokemon? =
pokemonStoreDataSource.getById(id.value) ?: pokemonApiClientDataSource.get(id.value)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package oscar.c.pozas.playground.app.controller
package oscar.c.pozas.playground.context.pokemon.app.controller

import com.fasterxml.jackson.databind.ObjectMapper
import com.github.tomakehurst.wiremock.client.WireMock.get
Expand All @@ -22,7 +22,7 @@ import org.springframework.util.ResourceUtils
@WireMockTest(httpPort = 8089)
@AutoConfigureEmbeddedDatabase(type = POSTGRES, refresh = AFTER_EACH_TEST_METHOD)
@ActiveProfiles("test")
class PokemonControllerAcceptanceTest {
class CreatePokemonControllerAcceptanceTest {

@LocalServerPort
protected var springBootPort = 0 // Port used during the test is injected
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package oscar.c.pozas.playground.domain.usecase
package oscar.c.pozas.playground.context.pokemon.domain.service

import io.mockk.MockKAnnotations
import io.mockk.every
Expand All @@ -14,10 +14,9 @@ import org.junit.jupiter.api.assertThrows
import oscar.c.pozas.playgroud.context.pokemon.domain.Pokemon
import oscar.c.pozas.playgroud.context.pokemon.domain.repository.PokemonRepository
import oscar.c.pozas.playgroud.context.pokemon.domain.service.PokemonFinder
import java.util.Optional

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class GetPokemonUseCaseTest {
class PokemonFinderTest {

@MockK
private lateinit var repository: PokemonRepository
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package oscar.c.pozas.playground.infrastructure.datasource
package oscar.c.pozas.playground.context.pokemon.infrastructure

import io.mockk.MockKAnnotations
import io.mockk.every
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package oscar.c.pozas.playground.kernel

abstract class DomainEvent {

abstract fun getName(): String
}

This file was deleted.

0 comments on commit 67a5acf

Please sign in to comment.