Skip to content

Commit

Permalink
Updated API dependencies (#191)
Browse files Browse the repository at this point in the history
* Updated to latest deps

* Fixed lint check

* Updated README.md
  • Loading branch information
kasem-sm authored Jul 31, 2022
1 parent 0647544 commit 5fcf194
Show file tree
Hide file tree
Showing 26 changed files with 64 additions and 57 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ for more.

## Tech stack / Miscellaneous Features 🚀

- <b>Kotlin, Jetpack Compose, Work Manager, Glance API, Material You, Coroutines, Flow, Accompanist,
Navigation Component, Ktor, Room Database, Coil, Dagger Hilt, Mockk etc.</b>
- <b>Kotlin, Jetpack Compose, Work Manager, Glance API, Material You, Coroutines, Flow, Accompanist, Ktor, Room Database, Coil, Dagger Hilt, Mockk etc.</b>
- **[Compose Destinations:](https://github.com/raamcosta/compose-destinations)** Annotation processing library for type-safe Jetpack Compose navigation with no boilerplate.
- **Observer Pattern:** Data from the cache would be shown while new data is fetched from the
server. Newly retrieved data gets updated immediately on the screen.
- **WorkManager:** App uses WorkManager API to update user's subscriptions and to fetch Daily Read
Expand Down Expand Up @@ -96,8 +96,7 @@ when you create a pull request. Running `spotlessApply` before creating a pull r
_cherry on the cake_.

* Special thanks to our contributors:
- The first one gets featured here 😎
- [All contributors](https://github.com/kasem-sm/SlimeKT/graphs/contributors)
- [List of Contributors](https://github.com/kasem-sm/SlimeKT/graphs/contributors)

## Medium Articles related to this project 🖋

Expand Down
8 changes: 4 additions & 4 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ val kmongoVersion: String by project

plugins {
application
kotlin("jvm") version "1.7.0"
id("org.jetbrains.kotlin.plugin.serialization") version "1.7.0"
kotlin("jvm") version "1.7.10"
id("org.jetbrains.kotlin.plugin.serialization") version "1.7.10"
id("com.github.johnrengelman.shadow") version "7.1.2"
id("com.diffplug.spotless") version "6.7.2"
id("com.diffplug.spotless") version "6.9.0"
}

group = "slime.com"
version = "0.0.1"
version = "0.0.2"
application {
mainClass.set("io.ktor.server.netty.EngineMain")
}
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/kotlin/slime/com/data/models/Article.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ data class Article(
val topic: String,
val timestamp: Long = System.currentTimeMillis(),
@BsonId
val id: Int = ObjectId().timestamp,
val id: Int = ObjectId().timestamp
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ data class RecommendedTopic(
val topicId: String,
val topicName: String,
@BsonId
val id: String = ObjectId().toString(),
val id: String = ObjectId().toString()
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ data class SubscribedTopic(
val topicId: String,
val isInExplore: Boolean = false,
@BsonId
val id: String = ObjectId().toString(),
val id: String = ObjectId().toString()
)
2 changes: 1 addition & 1 deletion api/src/main/kotlin/slime/com/data/models/Topic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ data class Topic(
val timestamp: Long = System.currentTimeMillis(),
val isInRecommendation: Boolean = false,
@BsonId
val id: String = ObjectId().toString(),
val id: String = ObjectId().toString()
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ interface ArticleRepository {
suspend fun getArticleById(articleId: Int): Article?
suspend fun getAllArticles(
topic: String = "",
query: String = "",
query: String = ""
): List<Article>
suspend fun getRandomArticleFromSubscription(userId: String? = null): Article?
suspend fun getRecommendedArticles(
userId: String? = null,
userId: String? = null
): List<Article>
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ class ArticleRepositoryImpl(

override suspend fun getAllArticles(
topic: String,
query: String,
query: String
): List<Article> {
val articles = when {
topic.isEmpty() && query.isNotEmpty() -> {
articleDb.find().filter(
or(
Article::title regex Regex("(?i).*$query.*"),
Article::author regex Regex("(?i).*$query.*"),
Article::author regex Regex("(?i).*$query.*")
)
).skipAndMap()
}
Expand All @@ -81,7 +81,7 @@ class ArticleRepositoryImpl(
).filter(
or(
Article::title regex Regex("(?i).*$query.*"),
Article::author regex Regex("(?i).*$query.*"),
Article::author regex Regex("(?i).*$query.*")
)
).skipAndMap()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (C) 2022, Kasem S.M
* All rights reserved.
*/
package slime.com.data.repository.recommended_topic
package slime.com.data.repository.recommendedtopic

import slime.com.data.models.RecommendedTopic

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
* Copyright (C) 2022, Kasem S.M
* All rights reserved.
*/
package slime.com.data.repository.recommended_topic
package slime.com.data.repository.recommendedtopic

import org.litote.kmongo.coroutine.CoroutineDatabase
import org.litote.kmongo.eq
import slime.com.data.models.RecommendedTopic

class RecommendedTopicRepositoryImpl(
db: CoroutineDatabase,
db: CoroutineDatabase
) : RecommendedTopicRepository {

private val recommendedDb = db.getCollection<RecommendedTopic>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (C) 2022, Kasem S.M
* All rights reserved.
*/
package slime.com.data.repository.subscribed_topic
package slime.com.data.repository.subscribedtopic

import slime.com.data.models.Topic

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (C) 2022, Kasem S.M
* All rights reserved.
*/
package slime.com.data.repository.subscribed_topic
package slime.com.data.repository.subscribedtopic

import org.litote.kmongo.and
import org.litote.kmongo.coroutine.CoroutineDatabase
Expand All @@ -11,7 +11,7 @@ import slime.com.data.models.SubscribedTopic
import slime.com.data.models.Topic

class SubscribeTopicsRepositoryImpl(
db: CoroutineDatabase,
db: CoroutineDatabase
) : SubscribeTopicsRepository {

private val subscribedTopicDb = db.getCollection<SubscribedTopic>()
Expand All @@ -26,23 +26,26 @@ class SubscribeTopicsRepositoryImpl(
override suspend fun subscribe(userId: String, topicId: String): Boolean {
return subscribedTopicDb.insertOne(
SubscribedTopic(
userId = userId, topicId = topicId
userId = userId,
topicId = topicId
)
).wasAcknowledged()
}

override suspend fun unSubscribe(userId: String, topicId: String): Boolean {
return subscribedTopicDb.deleteOne(
and(
SubscribedTopic::userId eq userId, SubscribedTopic::topicId eq topicId
SubscribedTopic::userId eq userId,
SubscribedTopic::topicId eq topicId
)
).wasAcknowledged()
}

override suspend fun checkAlreadySubscribed(userId: String, topicId: String): Boolean {
return subscribedTopicDb.findOne(
and(
SubscribedTopic::userId eq userId, SubscribedTopic::topicId eq topicId
SubscribedTopic::userId eq userId,
SubscribedTopic::topicId eq topicId
)
) != null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ data class AuthResponse(
@SerialName("username")
val username: String,
@SerialName("token")
val token: String,
val token: String
)
10 changes: 5 additions & 5 deletions api/src/main/kotlin/slime/com/di/MainModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import slime.com.data.repository.article.ArticleRepository
import slime.com.data.repository.article.ArticleRepositoryImpl
import slime.com.data.repository.auth.AuthRepository
import slime.com.data.repository.auth.AuthRepositoryImpl
import slime.com.data.repository.recommended_topic.RecommendedTopicRepository
import slime.com.data.repository.recommended_topic.RecommendedTopicRepositoryImpl
import slime.com.data.repository.subscribed_topic.SubscribeTopicsRepository
import slime.com.data.repository.subscribed_topic.SubscribeTopicsRepositoryImpl
import slime.com.data.repository.recommendedtopic.RecommendedTopicRepository
import slime.com.data.repository.recommendedtopic.RecommendedTopicRepositoryImpl
import slime.com.data.repository.subscribedtopic.SubscribeTopicsRepository
import slime.com.data.repository.subscribedtopic.SubscribeTopicsRepositoryImpl
import slime.com.data.repository.topic.TopicRepository
import slime.com.data.repository.topic.TopicRepositoryImpl
import slime.com.isDebugMode
Expand All @@ -40,7 +40,7 @@ val mainModule = module(createdAtStart = true) {
TopicRepositoryImpl(get())
}
single<SubscribeTopicsRepository> {
SubscribeTopicsRepositoryImpl(get(),)
SubscribeTopicsRepositoryImpl(get())
}
single<RecommendedTopicRepository> {
RecommendedTopicRepositoryImpl(get())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import io.ktor.routing.routing
import org.koin.ktor.ext.inject
import slime.com.data.repository.article.ArticleRepository
import slime.com.data.repository.auth.AuthRepository
import slime.com.data.repository.recommended_topic.RecommendedTopicRepository
import slime.com.data.repository.recommendedtopic.RecommendedTopicRepository
import slime.com.data.repository.topic.TopicRepository
import slime.com.isDebugMode
import slime.com.routes.registerArticleRoutes
import slime.com.routes.registerAuthenticationRoutes
import slime.com.routes.registerSubscribeTopicsRoute
import slime.com.routes.registerTopicRoutes
import slime.com.routes.articleRoutes
import slime.com.routes.authenticationRoutes
import slime.com.routes.subscribeTopicsRoute
import slime.com.routes.topicRoutes
import slime.com.service.ArticleService
import slime.com.service.SubscriptionService
import slime.com.service.UserService
Expand All @@ -43,10 +43,10 @@ fun Application.configureRouting() {
val subscriptionService by inject<SubscriptionService>()

routing {
registerAuthenticationRoutes(userService)
registerArticleRoutes(articleService)
registerTopicRoutes(topicRepository, subscriptionService)
registerSubscribeTopicsRoute(subscriptionService, userService, topicRepository)
authenticationRoutes(userService)
articleRoutes(articleService)
topicRoutes(topicRepository, subscriptionService)
subscribeTopicsRoute(subscriptionService, userService, topicRepository)

static {
resources("static")
Expand Down
8 changes: 5 additions & 3 deletions api/src/main/kotlin/slime/com/routes/ArticleRoutes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import slime.com.utils.get
import slime.com.utils.getUserId
import slime.com.utils.respondWith

fun Route.registerArticleRoutes(
fun Route.articleRoutes(
service: ArticleService
) {
get("/api/article/get/random") {
Expand All @@ -39,7 +39,8 @@ fun Route.registerArticleRoutes(
val query = get("query") ?: ""

val articles = service.getAllArticles(
topic = topic, query = query
topic = topic,
query = query
)

val articlesResponse = ArticlesResponse(
Expand Down Expand Up @@ -70,7 +71,8 @@ fun Route.registerArticleRoutes(
true -> respondWith<Unit>(SlimeResponse(true, "Article deleted successfully"))
false -> respondWith<Unit>(
SlimeResponse(
false, "Couldn't complete your request, Please try again later"
false,
"Couldn't complete your request, Please try again later"
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import slime.com.utils.loginUser
import slime.com.utils.respondWith
import slime.com.utils.respondWithBadRequest

fun Route.registerAuthenticationRoutes(
fun Route.authenticationRoutes(
service: UserService
) {
post("api/auth/register") {
Expand Down Expand Up @@ -49,7 +49,7 @@ fun Route.registerAuthenticationRoutes(
validateLogin = service::validateCredentialsForLogin,
username = username,
password = password,
userId = it.id,
userId = it.id
)
} ?: kotlin.run { return@post }
}
Expand All @@ -72,7 +72,7 @@ fun Route.registerAuthenticationRoutes(
validateLogin = service::validateCredentialsForLogin,
username = username,
password = password,
userId = it.id,
userId = it.id
)
} ?: kotlin.run {
respondWith(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import slime.com.utils.getUserId
import slime.com.utils.respondWith
import slime.com.utils.respondWithResult

fun Route.registerSubscribeTopicsRoute(
fun Route.subscribeTopicsRoute(
service: SubscriptionService,
userService: UserService,
topicRepository: TopicRepository
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/kotlin/slime/com/routes/TopicRoutes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import slime.com.utils.get
import slime.com.utils.getUserId
import slime.com.utils.respondWith

fun Route.registerTopicRoutes(
fun Route.topicRoutes(
repository: TopicRepository,
subscriptionService: SubscriptionService
) {
Expand Down
3 changes: 2 additions & 1 deletion api/src/main/kotlin/slime/com/service/ArticleService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package slime.com.service

import slime.com.data.models.Article
import slime.com.data.repository.article.ArticleRepository
import slime.com.data.repository.recommended_topic.RecommendedTopicRepository
import slime.com.data.repository.recommendedtopic.RecommendedTopicRepository
import slime.com.data.request.CreateArticleRequest
import slime.com.utils.ServiceResult

Expand Down Expand Up @@ -38,6 +38,7 @@ class ArticleService(

suspend fun deleteArticleById(articleId: Int) = articleRepository.deleteArticle(articleId)

@Suppress("Unused")
suspend fun validateAndCreateArticle(article: CreateArticleRequest): ServiceResult {
article.apply {
return when {
Expand Down
11 changes: 6 additions & 5 deletions api/src/main/kotlin/slime/com/service/SubscriptionService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
package slime.com.service

import slime.com.data.models.Topic
import slime.com.data.repository.recommended_topic.RecommendedTopicRepository
import slime.com.data.repository.subscribed_topic.SubscribeTopicsRepository
import slime.com.data.repository.recommendedtopic.RecommendedTopicRepository
import slime.com.data.repository.subscribedtopic.SubscribeTopicsRepository
import slime.com.data.repository.topic.TopicRepository
import slime.com.utils.ServiceResult

class SubscriptionService(
private val subscribeRepository: SubscribeTopicsRepository,
private val topicRepository: TopicRepository,
private val recommendedTopicRepository: RecommendedTopicRepository,
private val recommendedTopicRepository: RecommendedTopicRepository
) {
suspend fun getNumber(topicId: String) = subscribeRepository.getNumberOfSubscribers(topicId)

Expand Down Expand Up @@ -72,7 +72,8 @@ class SubscriptionService(
suspend fun checkIfUserSubscribes(userId: String, topicId: String): Boolean {
return when {
subscribeRepository.checkAlreadySubscribed(
userId = userId, topicId = topicId
userId = userId,
topicId = topicId
) -> true
else -> false
}
Expand All @@ -93,7 +94,7 @@ class SubscriptionService(
}

private suspend fun refreshRecommendedTopic(
currentUserId: String,
currentUserId: String
) {
val isNotEmpty = getUserSubscribedTopics(currentUserId).isNotEmpty()
if (isNotEmpty) {
Expand Down
3 changes: 2 additions & 1 deletion api/src/main/kotlin/slime/com/service/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ class UserService(
(userName.isBlank() || password.isBlank()) -> ServiceResult.Error("Required fields cannot be blank")
authRepository.isUsernameAvailable(userName) -> ServiceResult.Error("No such user exists.")
!authRepository.verifyPasswordForUsername(
userName, encryptorService.encryptPassword(password)
userName,
encryptorService.encryptPassword(password)
) -> ServiceResult.Error("Invalid Credentials")
else -> loginUserAndGenerateToken(userName)
}
Expand Down
Loading

0 comments on commit 5fcf194

Please sign in to comment.