Created
August 1, 2024 22:00
-
-
Save elyphas/1673574ed1f5c4e485e9faea7dee680c to your computer and use it in GitHub Desktop.
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
trait Repository2 { | |
type Input | |
type Output | |
val instance: Output | |
private val scheme: String = dom.window.location.protocol | |
private val host = dom.window.location.host | |
protected val rootPath: Uri = Uri.unsafeFromString(s"$scheme//$host") | |
protected val client: Client[IO] = FetchClientBuilder[IO].create | |
protected val endPoint: String | |
val event: PublishSubject[(Input, EventsDBWillDox)] = Subject.publish[(Input, EventsDBWillDox)] | |
val onEvent: Observable[Either[Throwable, (Output, EventsDBResult)]] | |
val onSuccess: Observable[Output] = onEvent.filter(_.fold(l => false, r => true)) | |
.map { | |
case Left(value) => instance | |
case Right((lst, event)) => lst | |
} | |
} | |
trait Repository3 extends Repository2 { | |
type Input = Recipe | |
type Output = List[ItemRecipe] | |
val instance: Output = List.empty[ItemRecipe] | |
protected val endPoint = "getLstItemsRecipe" | |
implicit val bodyPostEncode: EntityEncoder[IO, Input] = jsonEncoderOf | |
implicit val responseDecode: EntityDecoder[IO, (Output, EventsDBResult)] = jsonOf | |
override val onEvent: Observable[Either[Throwable, (Output, EventsDBResult)]] = { | |
event.switchMap { bodyPost => | |
val (recipe, event) = bodyPost | |
val bodyPostRefine = recipe | |
Observable.fromEffect { | |
client.fetchAs[(Output, EventsDBResult)](POST(bodyPostRefine, rootPath / endPoint)) | |
.redeem ( | |
error => (new Exception(error)).asLeft[(Output, EventsDBResult)], | |
something => something.asRight[Throwable] | |
) | |
} | |
} | |
} | |
} | |
implicit val repoLstItemsRecipe3: Repository3 = new Repository3{} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment