-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db5c1f9
commit 45a87cb
Showing
11 changed files
with
415 additions
and
17 deletions.
There are no files selected for viewing
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
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
39 changes: 39 additions & 0 deletions
39
...-backend/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/posts/CreatePostUseCase.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package me.nathanfallet.extopy.usecases.posts | ||
|
||
import io.ktor.http.* | ||
import me.nathanfallet.extopy.models.posts.Post | ||
import me.nathanfallet.extopy.models.posts.PostPayload | ||
import me.nathanfallet.extopy.repositories.posts.IPostsRepository | ||
import me.nathanfallet.ktorx.models.exceptions.ControllerException | ||
import me.nathanfallet.usecases.context.IContext | ||
import me.nathanfallet.usecases.models.create.context.ICreateModelWithContextSuspendUseCase | ||
|
||
class CreatePostUseCase( | ||
private val repository: IPostsRepository, | ||
) : ICreateModelWithContextSuspendUseCase<Post, PostPayload> { | ||
|
||
override suspend fun invoke(input1: PostPayload, input2: IContext): Post? { | ||
if (input1.body.isBlank() && input1.repostOfId == null) throw ControllerException( | ||
HttpStatusCode.BadRequest, | ||
"posts_body_empty" | ||
) | ||
if (input1.repostOfId != null && input1.repliedToId != null) throw ControllerException( | ||
HttpStatusCode.BadRequest, | ||
"posts_can_only_one_in_reply_or_repost" | ||
Check warning on line 22 in extopy-backend/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/posts/CreatePostUseCase.kt Codecov / codecov/patchextopy-backend/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/posts/CreatePostUseCase.kt#L21-L22
|
||
) | ||
input1.repliedToId?.let { repliedToId -> | ||
if (repository.get(repliedToId) == null) throw ControllerException( | ||
HttpStatusCode.BadRequest, | ||
"posts_replied_to_not_found" | ||
) | ||
} | ||
input1.repostOfId?.let { repostOfId -> | ||
if (repository.get(repostOfId) == null) throw ControllerException( | ||
HttpStatusCode.BadRequest, | ||
"posts_repost_of_not_found" | ||
) | ||
} | ||
return repository.create(input1, input2) | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...-backend/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/posts/DeletePostUseCase.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package me.nathanfallet.extopy.usecases.posts | ||
|
||
import me.nathanfallet.extopy.models.posts.Post | ||
import me.nathanfallet.extopy.repositories.posts.IPostsRepository | ||
import me.nathanfallet.usecases.models.delete.IDeleteModelSuspendUseCase | ||
|
||
class DeletePostUseCase( | ||
private val repository: IPostsRepository, | ||
) : IDeleteModelSuspendUseCase<Post, String> { | ||
|
||
override suspend fun invoke(input: String): Boolean { | ||
return repository.delete(input) | ||
/* | ||
// TODO: Delete related data | ||
LikesInPosts.deleteWhere { | ||
Op.build { postId eq id } | ||
} | ||
Posts.deleteWhere { | ||
Op.build { Posts.id eq id } | ||
} | ||
Posts.select { | ||
repliedToId eq id or (repostOfId eq id) | ||
}.forEach { | ||
delete(it[Posts.id]) | ||
} | ||
*/ | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...-backend/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/posts/UpdatePostUseCase.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package me.nathanfallet.extopy.usecases.posts | ||
|
||
import io.ktor.http.* | ||
import me.nathanfallet.extopy.models.posts.Post | ||
import me.nathanfallet.extopy.models.posts.PostPayload | ||
import me.nathanfallet.extopy.repositories.posts.IPostsRepository | ||
import me.nathanfallet.ktorx.models.exceptions.ControllerException | ||
import me.nathanfallet.usecases.models.update.IUpdateModelSuspendUseCase | ||
|
||
class UpdatePostUseCase( | ||
private val repository: IPostsRepository, | ||
) : IUpdateModelSuspendUseCase<Post, String, PostPayload> { | ||
|
||
override suspend fun invoke(input1: String, input2: PostPayload): Post? { | ||
if (input2.body.isBlank()) throw ControllerException( | ||
HttpStatusCode.BadRequest, | ||
"posts_body_empty" | ||
) | ||
return if (repository.update(input1, input2)) repository.get(input1) | ||
else null | ||
} | ||
|
||
} |
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
132 changes: 132 additions & 0 deletions
132
...backend/src/jvmTest/kotlin/me/nathanfallet/extopy/usecases/posts/CreatePostUseCaseTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package me.nathanfallet.extopy.usecases.posts | ||
|
||
import io.ktor.http.* | ||
import io.mockk.coEvery | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.runBlocking | ||
import me.nathanfallet.extopy.models.posts.Post | ||
import me.nathanfallet.extopy.models.posts.PostPayload | ||
import me.nathanfallet.extopy.models.users.UserContext | ||
import me.nathanfallet.extopy.repositories.posts.IPostsRepository | ||
import me.nathanfallet.ktorx.models.exceptions.ControllerException | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
|
||
class CreatePostUseCaseTest { | ||
|
||
@Test | ||
fun testInvokeWithBody() = runBlocking { | ||
val repository = mockk<IPostsRepository>() | ||
val useCase = CreatePostUseCase(repository) | ||
val post = mockk<Post>() | ||
val payload = PostPayload("body") | ||
val context = mockk<UserContext>() | ||
coEvery { repository.create(payload, context) } returns post | ||
val result = useCase(payload, context) | ||
assertEquals(post, result) | ||
} | ||
|
||
@Test | ||
fun testInvokeWithEmpty() = runBlocking { | ||
val repository = mockk<IPostsRepository>() | ||
val useCase = CreatePostUseCase(repository) | ||
val post = mockk<Post>() | ||
val payload = PostPayload("") | ||
val context = mockk<UserContext>() | ||
coEvery { repository.create(payload, context) } returns post | ||
val exception = assertFailsWith<ControllerException> { | ||
useCase(payload, context) | ||
} | ||
assertEquals(HttpStatusCode.BadRequest, exception.code) | ||
assertEquals("posts_body_empty", exception.key) | ||
} | ||
|
||
@Test | ||
fun testInvokeReply() = runBlocking { | ||
val repository = mockk<IPostsRepository>() | ||
val useCase = CreatePostUseCase(repository) | ||
val post = mockk<Post>() | ||
val payload = PostPayload("body", repliedToId = "replyId") | ||
val context = mockk<UserContext>() | ||
coEvery { repository.get("replyId") } returns mockk() | ||
coEvery { repository.create(payload, context) } returns post | ||
val result = useCase(payload, context) | ||
assertEquals(post, result) | ||
} | ||
|
||
@Test | ||
fun testInvokeReplyNotExists() = runBlocking { | ||
val repository = mockk<IPostsRepository>() | ||
val useCase = CreatePostUseCase(repository) | ||
val post = mockk<Post>() | ||
val payload = PostPayload("body", repliedToId = "replyId") | ||
val context = mockk<UserContext>() | ||
coEvery { repository.get("replyId") } returns null | ||
coEvery { repository.create(payload, context) } returns post | ||
val exception = assertFailsWith<ControllerException> { | ||
useCase(payload, context) | ||
} | ||
assertEquals(HttpStatusCode.BadRequest, exception.code) | ||
assertEquals("posts_replied_to_not_found", exception.key) | ||
} | ||
|
||
@Test | ||
fun testInvokeReplyNoBody() = runBlocking { | ||
val repository = mockk<IPostsRepository>() | ||
val useCase = CreatePostUseCase(repository) | ||
val post = mockk<Post>() | ||
val payload = PostPayload("", repliedToId = "replyId") | ||
val context = mockk<UserContext>() | ||
coEvery { repository.get("replyId") } returns mockk() | ||
coEvery { repository.create(payload, context) } returns post | ||
val exception = assertFailsWith<ControllerException> { | ||
useCase(payload, context) | ||
} | ||
assertEquals(HttpStatusCode.BadRequest, exception.code) | ||
assertEquals("posts_body_empty", exception.key) | ||
} | ||
|
||
@Test | ||
fun testInvokeRepost() = runBlocking { | ||
val repository = mockk<IPostsRepository>() | ||
val useCase = CreatePostUseCase(repository) | ||
val post = mockk<Post>() | ||
val payload = PostPayload("", repostOfId = "repostId") | ||
val context = mockk<UserContext>() | ||
coEvery { repository.get("repostId") } returns mockk() | ||
coEvery { repository.create(payload, context) } returns post | ||
val result = useCase(payload, context) | ||
assertEquals(post, result) | ||
} | ||
|
||
@Test | ||
fun testInvokeRepostWithBody() = runBlocking { | ||
val repository = mockk<IPostsRepository>() | ||
val useCase = CreatePostUseCase(repository) | ||
val post = mockk<Post>() | ||
val payload = PostPayload("body", repostOfId = "repostId") | ||
val context = mockk<UserContext>() | ||
coEvery { repository.get("repostId") } returns mockk() | ||
coEvery { repository.create(payload, context) } returns post | ||
val result = useCase(payload, context) | ||
assertEquals(post, result) | ||
} | ||
|
||
@Test | ||
fun testInvokeRepostNotExists() = runBlocking { | ||
val repository = mockk<IPostsRepository>() | ||
val useCase = CreatePostUseCase(repository) | ||
val post = mockk<Post>() | ||
val payload = PostPayload("", repostOfId = "repostId") | ||
val context = mockk<UserContext>() | ||
coEvery { repository.get("repostId") } returns null | ||
coEvery { repository.create(payload, context) } returns post | ||
val exception = assertFailsWith<ControllerException> { | ||
useCase(payload, context) | ||
} | ||
assertEquals(HttpStatusCode.BadRequest, exception.code) | ||
assertEquals("posts_repost_of_not_found", exception.key) | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...backend/src/jvmTest/kotlin/me/nathanfallet/extopy/usecases/posts/DeletePostUseCaseTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package me.nathanfallet.extopy.usecases.posts | ||
|
||
import io.mockk.coEvery | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.runBlocking | ||
import me.nathanfallet.extopy.repositories.posts.IPostsRepository | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class DeletePostUseCaseTest { | ||
|
||
@Test | ||
fun testInvoke() = runBlocking { | ||
val repository = mockk<IPostsRepository>() | ||
val useCase = DeletePostUseCase(repository) | ||
coEvery { repository.delete("id") } returns true | ||
assertEquals(true, useCase("id")) | ||
} | ||
|
||
@Test | ||
fun testInvokeFails() = runBlocking { | ||
val repository = mockk<IPostsRepository>() | ||
val useCase = DeletePostUseCase(repository) | ||
coEvery { repository.delete("id") } returns false | ||
assertEquals(false, useCase("id")) | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
...backend/src/jvmTest/kotlin/me/nathanfallet/extopy/usecases/posts/UpdatePostUseCaseTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package me.nathanfallet.extopy.usecases.posts | ||
|
||
import io.ktor.http.* | ||
import io.mockk.coEvery | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.runBlocking | ||
import me.nathanfallet.extopy.models.posts.Post | ||
import me.nathanfallet.extopy.models.posts.PostPayload | ||
import me.nathanfallet.extopy.repositories.posts.IPostsRepository | ||
import me.nathanfallet.ktorx.models.exceptions.ControllerException | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
|
||
class UpdatePostUseCaseTest { | ||
|
||
@Test | ||
fun testInvoke() = runBlocking { | ||
val repository = mockk<IPostsRepository>() | ||
val useCase = UpdatePostUseCase(repository) | ||
val payload = PostPayload("body") | ||
val post = mockk<Post>() | ||
coEvery { repository.update("id", payload) } returns true | ||
coEvery { repository.get("id") } returns post | ||
assertEquals(post, useCase("id", payload)) | ||
} | ||
|
||
@Test | ||
fun testInvokeFails() = runBlocking { | ||
val repository = mockk<IPostsRepository>() | ||
val useCase = UpdatePostUseCase(repository) | ||
val payload = PostPayload("body") | ||
coEvery { repository.update("id", payload) } returns false | ||
assertEquals(null, useCase("id", payload)) | ||
} | ||
|
||
@Test | ||
fun testInvokeNoBody() = runBlocking { | ||
val useCase = UpdatePostUseCase(mockk()) | ||
val payload = PostPayload("") | ||
val exception = assertFailsWith<ControllerException> { | ||
useCase("id", payload) | ||
} | ||
assertEquals(HttpStatusCode.BadRequest, exception.code) | ||
assertEquals("posts_body_empty", exception.key) | ||
} | ||
|
||
} |
Oops, something went wrong.