Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Proof of concept implementation of generic repository #62

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
package com.softwaremill.realworld.profiles

import io.getquill.*
import io.getquill.context.sql.idiom.SqlIdiom
import io.getquill.jdbczio.*
import zio.ZLayer
import zio.{Tag, ZIO, ZLayer}

import java.sql.SQLException
import javax.sql.DataSource

class ProfilesRepository(quill: Quill.Sqlite[SnakeCase]) {
import quill.*
trait ProfilesRepository {
def follow(followedId: Int, followerId: Int): ZIO[Any, SQLException, Long]
def unfollow(followedId: Int, followerId: Int): ZIO[Any, SQLException, Long]
def isFollowing(followedId: Int, followerId: Int): ZIO[Any, SQLException, Boolean]
}

def follow(followedId: Int, followerId: Int) = run {
query[Followers].insert(_.userId -> lift(followedId), _.followerId -> lift(followerId)).onConflictIgnore
}
object ProfilesRepository {
def live[I <: SqlIdiom: Tag, N <: NamingStrategy: Tag]: ZLayer[Quill[I, N], Nothing, ProfilesRepository] =
ZLayer.fromFunction(ProfilesRepositoryLive[I, N](_))

def unfollow(followedId: Int, followerId: Int) = run {
query[Followers].filter(f => (f.userId == lift(followedId)) && (f.followerId == lift(followerId))).delete
}
private class ProfilesRepositoryLive[I <: SqlIdiom, N <: NamingStrategy](quill: Quill[I, N]) extends ProfilesRepository {

def isFollowing(followedId: Int, followerId: Int) = run {
query[Followers].filter(_.userId == lift(followedId)).filter(_.followerId == lift(followerId)).map(_ => 1).nonEmpty
}
import quill.*

}
def follow(followedId: Int, followerId: Int): ZIO[Any, SQLException, Long] = run {
query[Followers].insert(_.userId -> lift(followedId), _.followerId -> lift(followerId)).onConflictIgnore
}

def unfollow(followedId: Int, followerId: Int): ZIO[Any, SQLException, Long] = run {
query[Followers].filter(f => (f.userId == lift(followedId)) && (f.followerId == lift(followerId))).delete
}

object ProfilesRepository:
val live: ZLayer[Quill.Sqlite[SnakeCase], Nothing, ProfilesRepository] =
ZLayer.fromFunction(new ProfilesRepository(_))
def isFollowing(followedId: Int, followerId: Int): ZIO[Any, SQLException, Boolean] = run {
query[Followers].filter(_.userId == lift(followedId)).filter(_.followerId == lift(followerId)).map(_ => 1).nonEmpty
}
}
}