Skip to content

Commit

Permalink
GH-735 Merge some init methods and use UserId alias instead of String
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed Apr 12, 2021
1 parent bfa7c63 commit 5ece463
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fun Application.mainModuleWithDeps(json: Json, httpClient: HttpClient) {

val userFacade = usersModule(httpClient, database)
val packageFacade = packagesModule(httpClient, userFacade, database)
val authFacade = authModule(userFacade)
val authFacade = authModule(httpClient, userFacade)

install(Routing) {
installUserRouting(this, userFacade)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ package org.panda_lang.hub.auth
import com.auth0.jwt.JWT
import com.auth0.jwt.algorithms.Algorithm
import io.ktor.application.Application
import io.ktor.application.ApplicationStopping
import io.ktor.application.install
import io.ktor.auth.Authentication
import io.ktor.auth.OAuthServerSettings
import io.ktor.auth.jwt.JWTPrincipal
import io.ktor.auth.jwt.jwt
import io.ktor.auth.oauth
import io.ktor.client.HttpClient
import io.ktor.client.engine.apache.Apache
import io.ktor.locations.locations
import io.ktor.locations.url
import io.ktor.routing.Routing
Expand All @@ -39,18 +37,7 @@ import org.panda_lang.hub.user.UserFacade
import java.security.SecureRandom
import java.util.concurrent.TimeUnit

fun Application.authModule(userFacade: UserFacade): AuthFacade {
return authModuleWithDeps(userFacade)
}

fun Application.authModuleWithDeps(
userFacade: UserFacade,
oauthClient: HttpClient = HttpClient(Apache).apply {
environment.monitor.subscribe(ApplicationStopping) {
close()
}
},
): AuthFacade {
fun Application.authModule(httpClient: HttpClient, userFacade: UserFacade): AuthFacade {
val config = environment.config

val secureRandom = SecureRandom()
Expand Down Expand Up @@ -81,7 +68,7 @@ fun Application.authModuleWithDeps(

install(Authentication) {
oauth("oauth") {
client = oauthClient
client = httpClient
providerLookup = {
loginProviders[application.locations.resolve<AuthorizeLocation>(AuthorizeLocation::class, this).type]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package org.panda_lang.hub.auth.jwt
import com.auth0.jwt.JWT
import io.ktor.auth.jwt.JWTCredential
import io.ktor.auth.jwt.JWTPrincipal
import org.panda_lang.hub.user.UserId

const val JWT_SUBJECT = "Authentication"

Expand All @@ -28,7 +29,7 @@ const val TOKEN_CLAIM = "oauth2"

class JwtProvider(private val configuration: JwtConfiguration, private val expirationDateProvider: ExpirationDateProvider) {

fun generateToken(secret: String, userId: String, login: String): String {
fun generateToken(secret: String, userId: UserId, login: String): String {
return JWT.create()
.withIssuer(configuration.issuer)
.withAudience(configuration.audience)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,41 @@
package org.panda_lang.hub.packages

import org.panda_lang.hub.github.RepositoryId
import org.panda_lang.hub.user.UserId
import java.util.concurrent.ConcurrentHashMap

internal class InMemoryPackageRepository : PackageRepository {

private val packages = ConcurrentHashMap<String, Package>()

override suspend fun updateDailyStats(packageId: String, date: Date, dailyBulk: Map<Country, Int>) {
TODO("Not yet implemented")
}
private val packages = ConcurrentHashMap<PackageId, Package>()

override suspend fun updateDailyStats(packageId: PackageId, date: Date, dailyBulk: Map<Country, Int>) =
packages[packageId]!!.let {
packages[packageId] = Package(
_id = it._id,
registered = it.registered,
repository = it.repository,
dailyStats = HashMap(it.dailyStats).also { updatedStats ->
updatedStats[date.toString()] = ((updatedStats[date.toString()] ?: DailyStats()).countries.asSequence() + dailyBulk.asSequence())
.groupBy({ entry -> entry.key }, { entry -> entry.value })
.mapValues { entry -> entry.value.sum() }
.let { map -> DailyStats(map) }
}
)
}

override suspend fun savePackage(pkg: Package): Package =
pkg.also { packages[it._id] = it }

override suspend fun deletePackage(pkg: Package): Boolean =
packages.remove(pkg._id).let { true }

override suspend fun findPackageById(id: String): Package? =
override suspend fun findPackageById(id: PackageId): Package? =
packages[id]

override suspend fun findPackageByRepositoryId(id: RepositoryId): Package? =
packages.values.firstOrNull { it.repository.fullName == id.fullName() }

override suspend fun findPackagesByUserId(id: String): Collection<Package> =
override suspend fun findPackagesByUserId(id: UserId): Collection<Package> =
packages.values.filter { it.repository.owner.id == id.toLong() }

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import org.litote.kmongo.upsert
import org.panda_lang.hub.github.GitHubRepositoryInfo
import org.panda_lang.hub.github.GitHubUserInfo
import org.panda_lang.hub.github.RepositoryId
import org.panda_lang.hub.user.UserId

internal class MongoPackageRepository(private val collection: CoroutineCollection<Package>) : PackageRepository {

override suspend fun updateDailyStats(packageId: String, date: Date, dailyBulk: Map<Country, Int>) =
override suspend fun updateDailyStats(packageId: PackageId, date: Date, dailyBulk: Map<Country, Int>) =
dailyBulk.forEach {
collection.updateOneById(
packageId,
Expand All @@ -43,13 +44,13 @@ internal class MongoPackageRepository(private val collection: CoroutineCollectio
override suspend fun deletePackage(pkg: Package): Boolean =
collection.deleteOneById(pkg._id).wasAcknowledged()

override suspend fun findPackageById(id: String): Package? =
override suspend fun findPackageById(id: PackageId): Package? =
collection.findOne(Package::_id eq id)

override suspend fun findPackageByRepositoryId(id: RepositoryId): Package? =
collection.findOne(Package::repository / GitHubRepositoryInfo::fullName eq id.fullName())

override suspend fun findPackagesByUserId(id: String): Collection<Package> =
override suspend fun findPackagesByUserId(id: UserId): Collection<Package> =
collection.find(Package::repository / GitHubRepositoryInfo::owner / GitHubUserInfo::id eq id.toLong()).toList()

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import io.ktor.client.HttpClient
import io.ktor.routing.Routing
import kotlinx.coroutines.runBlocking
import org.litote.kmongo.coroutine.CoroutineDatabase
import org.panda_lang.hub.github.GitHubClient
import org.panda_lang.hub.github.RemoteGitHubClient
import org.panda_lang.hub.user.UserFacade
import java.util.concurrent.Executors
Expand All @@ -36,15 +35,6 @@ fun Application.packagesModule(httpClient: HttpClient, userFacade: UserFacade, d

val packageCollection = database.getCollection<Package>()
val packageRepository = MongoPackageRepository(packageCollection)

return packagesModuleWithDeps(gitHubClient, userFacade, packageRepository)
}

internal fun Application.packagesModuleWithDeps(
gitHubClient: GitHubClient,
userFacade: UserFacade,
packageRepository: PackageRepository,
): PackageFacade {
val packageService = PackageService(gitHubClient, userFacade, packageRepository)
val statsService = StatsService(packageService)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PackageFacade internal constructor(
private val statsService: StatsService
) {

fun incrementRequestsCount(packageId: String, ip: String) =
fun incrementRequestsCount(packageId: PackageId, ip: String) =
statsService.incrementRequestsCount(packageId, ip)

suspend fun deletePackage(login: String, name: String): Boolean =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@
package org.panda_lang.hub.packages

import org.panda_lang.hub.github.RepositoryId
import org.panda_lang.hub.user.UserId

internal interface PackageRepository {

suspend fun updateDailyStats(packageId: String, date: Date, dailyBulk: Map<Country, Int>)
suspend fun updateDailyStats(packageId: PackageId, date: Date, dailyBulk: Map<Country, Int>)

suspend fun savePackage(pkg: Package): Package

suspend fun deletePackage(pkg: Package): Boolean

suspend fun findPackageById(id: String): Package?
suspend fun findPackageById(id: PackageId): Package?

suspend fun findPackageByRepositoryId(id: RepositoryId): Package?

suspend fun findPackagesByUserId(id: String): Collection<Package>
suspend fun findPackagesByUserId(id: UserId): Collection<Package>

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal class PackageService(
private val packageRepository: PackageRepository
) {

internal suspend fun updateDailyStats(packageId: String, date: Date, dailyBulk: Map<Country, Int>) =
internal suspend fun updateDailyStats(packageId: PackageId, date: Date, dailyBulk: Map<Country, Int>) =
packageRepository.updateDailyStats(packageId, date, dailyBulk)

suspend fun delete(repositoryId: RepositoryId): Boolean =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import java.util.concurrent.ConcurrentHashMap

internal class InMemoryUserRepository : UserRepository {

private val users = ConcurrentHashMap<String, User>()
private val users = ConcurrentHashMap<UserId, User>()

override suspend fun findUserById(id: String): User? =
override suspend fun findUserById(id: UserId): User? =
users[id]

override suspend fun findUserByLogin(login: String): User? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal class MongoUserRepository(private val collection: CoroutineCollection<U
override suspend fun saveUser(user: User): User =
user.also { collection.insertOne(it) }

override suspend fun findUserById(id: String): User? =
override suspend fun findUserById(id: UserId): User? =
collection.findOne(User::_id eq id)

override suspend fun findUserByLogin(login: String): User? =
Expand Down
4 changes: 3 additions & 1 deletion hub-backend/src/main/kotlin/org/panda_lang/hub/user/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package org.panda_lang.hub.user
import kotlinx.serialization.Serializable
import org.panda_lang.hub.github.GitHubProfile

typealias UserId = String

@Serializable
data class User(
val _id: String,
val _id: UserId,
val registered: Boolean = false,
val profile: GitHubProfile
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,15 @@ import io.ktor.application.Application
import io.ktor.client.HttpClient
import io.ktor.routing.Routing
import org.litote.kmongo.coroutine.CoroutineDatabase
import org.panda_lang.hub.github.GitHubClient
import org.panda_lang.hub.github.RemoteGitHubClient

fun Application.usersModule(httpClient: HttpClient, database: CoroutineDatabase): UserFacade {
val gitHubClient = RemoteGitHubClient(httpClient)
val collection = database.getCollection<User>()
val repository = MongoUserRepository(collection)

return usersModuleWithDeps(gitHubClient, repository)
}
val userCollection = database.getCollection<User>()
val userRepository = MongoUserRepository(userCollection)
val userService = UserService(gitHubClient, userRepository)

internal fun Application.usersModuleWithDeps(gitHubClient: GitHubClient, repository: UserRepository): UserFacade {
val userService = UserService(gitHubClient, repository)
return UserFacade(userService)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ class UserFacade internal constructor(private val userService: UserService) {

suspend fun getUserByLogin(login: String): User? = userService.getUserByLogin(login)

suspend fun getUser(id: String): User? = userService.getUser(id)
suspend fun getUser(id: UserId): User? = userService.getUser(id)

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package org.panda_lang.hub.user

internal interface UserRepository {

suspend fun findUserById(id: String): User?
suspend fun findUserById(id: UserId): User?

suspend fun findUserByLogin(login: String): User?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal class UserService(
suspend fun getUserByLogin(login: String): User? =
userRepository.findUserByLogin(login)

suspend fun getUser(id: String): User? =
suspend fun getUser(id: UserId): User? =
userRepository.findUserById(id)

}

0 comments on commit 5ece463

Please sign in to comment.