Skip to content

Commit

Permalink
Replace Exposed with Beak
Browse files Browse the repository at this point in the history
  • Loading branch information
lnhrdt committed Apr 3, 2018
1 parent c014da2 commit a483366
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 21 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ allprojects {
repositories {
mavenCentral()
maven { url "https://jitpack.io" }
maven { url "https://dl.bintray.com/codebandits/beak" }
maven { url "https://dl.bintray.com/kotlin/exposed" }
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ kotlinVersion=1.2.31
springBootVersion=2.0.0.RELEASE
mockitoKotlinVersion=2.0.0-alpha03
arrowVersion=0.6.1
exposedVersion=0.10.1
beakVersion=0.0.19
2 changes: 1 addition & 1 deletion gradle/scripts/persistence.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dependencies {
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile("org.jetbrains.exposed:exposed:$exposedVersion")
compile("io.github.codebandits:beak:$beakVersion")
compile('org.flywaydb:flyway-core')
runtime('mysql:mysql-connector-java')
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.leonhardt.coffee.latte.coffee

import arrow.core.Either
import arrow.core.flatMap
import io.github.codebandits.beak.findByIdOrError
import io.github.codebandits.beak.newOrError
import io.leonhardt.coffee.latte.Errors
import io.leonhardt.coffee.latte.friends.FriendEntity
import org.jetbrains.exposed.sql.transactions.transaction
Expand All @@ -13,17 +16,14 @@ class CoffeeCreateService {

fun create(coffeeNew: CoffeeNew): Either<Errors, Coffee> = transaction {
val friendId = coffeeNew.friendId.toString()
val foundFriend = FriendEntity.findById(friendId)
when (foundFriend) {
null -> Either.left(mapOf("friendId" to "friendId $friendId not found"))
else -> Either.right(foundFriend)
}
.map { friendEntity ->
CoffeeEntity.new {
FriendEntity.findByIdOrError(friendId)
.flatMap { friendEntity ->
CoffeeEntity.newOrError {
friend = friendEntity
dateTime = DateTime(Instant.now().toEpochMilli())
}
}
.map { it.toCoffee() }
.mapLeft { mapOf("friendId" to "friendId $friendId not found") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package io.leonhardt.coffee.latte.friends

import arrow.core.Either
import arrow.core.flatMap
import io.github.codebandits.beak.findByIdOrError
import io.github.codebandits.beak.newOrError
import io.leonhardt.coffee.latte.Errors
import io.leonhardt.coffee.latte.groups.GroupEntity
import org.jetbrains.exposed.sql.transactions.transaction
Expand All @@ -12,19 +14,17 @@ class FriendCreateService(val friendCreateRequestValidator: FriendCreateRequestV

fun create(friendNew: FriendNew): Either<Errors, Friend> = transaction {
val groupId = friendNew.groupId.toString()
val foundGroup = GroupEntity.findById(groupId)
when (foundGroup) {
null -> Either.left(mapOf("groupId" to "groupId $groupId not found"))
else -> Either.right(foundGroup)
}
.flatMap { groupEntity ->
friendCreateRequestValidator.validate(friendNew)
.map { validFriendNew ->
FriendEntity.new {

friendCreateRequestValidator.validate(friendNew)
.flatMap { validFriendNew ->
GroupEntity.findByIdOrError(groupId)
.flatMap { groupEntity ->
FriendEntity.newOrError {
name = validFriendNew.name
group = groupEntity
}
}
.mapLeft { mapOf("groupId" to "groupId $groupId not found") }
}
.map { it.toFriend() }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.leonhardt.coffee.latte.friends

import arrow.core.Either
import io.github.codebandits.beak.findWhereOrError
import io.leonhardt.coffee.latte.Errors
import org.jetbrains.exposed.sql.transactions.transaction
import org.springframework.stereotype.Service
Expand All @@ -10,6 +11,8 @@ import java.util.*
class FriendFindAllByGroupService {

fun findAllByGroup(groupId: UUID): Either<Errors, List<Friend>> = transaction {
FriendEntity.find { FriendTable.group eq groupId.toString() }.map { it.toFriend() }.let { Either.right(it) }
FriendEntity.findWhereOrError { FriendTable.group eq groupId.toString() }
.map { it.map { it.toFriend() } }
.mapLeft { mapOf<String, String>() }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.leonhardt.coffee.latte.groups

import arrow.core.Either
import io.github.codebandits.beak.newOrError
import io.leonhardt.coffee.latte.Errors
import org.jetbrains.exposed.sql.transactions.transaction
import org.springframework.stereotype.Service
Expand All @@ -9,6 +10,8 @@ import org.springframework.stereotype.Service
class GroupCreateService {

fun create(groupNew: GroupNew): Either<Errors, Group> = transaction {
GroupEntity.new { name = groupNew.name }.toGroup().let { Either.right(it) }
GroupEntity.newOrError { name = groupNew.name }
.map { it.toGroup() }
.mapLeft { mapOf<String, String>() }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.leonhardt.coffee.latte.groups

import arrow.core.Either
import io.github.codebandits.beak.allOrError
import io.leonhardt.coffee.latte.Errors
import org.jetbrains.exposed.sql.transactions.transaction
import org.springframework.stereotype.Service
Expand All @@ -9,6 +10,8 @@ import org.springframework.stereotype.Service
class GroupGetAllService {

fun getAll(): Either<Errors, List<Group>> = transaction {
GroupEntity.all().map { it.toGroup() }.let { Either.right(it) }
GroupEntity.allOrError()
.map { it.map { it.toGroup() } }
.mapLeft { mapOf<String, String>() }
}
}

0 comments on commit a483366

Please sign in to comment.