Skip to content

Instantly share code, notes, and snippets.

View oharaandrew314's full-sized avatar

Andrew O'Hara oharaandrew314

View GitHub Profile
@oharaandrew314
oharaandrew314 / CatDtoV2.kt
Created March 15, 2022 19:57
hexagonal-client-cat-v2
data class CatDtoV2(
val id: UUID,
val name: String,
val owner: OwnerDtoV2,
val colours: Set<ColourV2>,
val breed: BreedV2,
val favouriteFood: String
)
data class OwnerDtoV2(
@oharaandrew314
oharaandrew314 / Cat.kt
Last active March 16, 2022 02:02
hexagonal-clients-original
data class Cat(
val id: Int,
val name: String,
val ownerId: Int,
val ownerName: String,
val brown: Boolean,
val grey: Boolean,
val breed: Breed?,
val appointments: List<Instant>
)
fun main() {
val imagesHost = System.getenv("IMAGES_HOST")
val imagesApiKey = System.getenv("IMAGES_API_KEY")
val dbHost = System.getenv("DB_HOST")
val dbUser = System.getenv("DB_USER")
val dbPass = System.getenv("DB_PASS")
val dbName = System.getenv("DB_NAME")
val dataSource = MysqlDataSource().apply {
class RestApiTest {
private val imageBackend = FakeThirdPartyImageBackend()
private val petService = PetService(
pets = PetsDao.mock(),
images = ThirdPartyImageClient(imageBackend)
)
private val testObj = RestApi(petService).toHttpHandler()
class RestApi(private val pets: PetService) {
companion object {
val petIdLens = Path.long().of("petId")
val petLens = Body.auto<Pet>().toLens()
val nameLens = Query.nonEmptyString().required("name")
const val petsPath = "/pet"
val petPath = "/pet/${petIdLens}"
val uploadImagePath = "/pet/${petIdLens}/uploadImage"
class PetService(private val pets: PetsDao, private val images: ThirdPartyImageClient) {
fun get(id: Long): Pet? {
return pets[id]
}
fun create(name: String): Pet {
val id = pets.create(name)
return pets[id] ?: throw IllegalStateException("Pet was not created")
}
class FakeThirdPartyImageBackend: HttpHandler {
private var nextId = 0
val images = mutableListOf<ThirdPartyImageDto>()
private fun upload(request: Request): Response {
val id = "image${nextId++}"
val image = ThirdPartyImageDto(
id = id,
class PetsDaoTest {
private val testObj = PetsDao.mock()
@Test
fun `get missing`() {
testObj[123] shouldBe null
}
@Test
data class Pet(
val id: Long,
val name: String,
val photoUrls: List<String>
)
class PetsDao(private val dataSource: DataSource) {
private object Sql {
const val get = """
SELECT pets.*, GROUP_CONCAT(photos.url) photo_urls
FROM pets
LEFT JOIN photos ON photos.pet_id = pets.id
WHERE pets.id = ?
GROUP BY pets.id
"""