diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/IPostRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/IPostRepository.kt index 3b77248e..7321fbf7 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/IPostRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/IPostRepository.kt @@ -1,11 +1,23 @@ package dev.usbharu.hideout.repository import dev.usbharu.hideout.domain.model.hideout.entity.Post +import java.time.Instant interface IPostRepository { suspend fun generateId(): Long suspend fun save(post: Post): Post - suspend fun findOneById(id: Long): Post? + suspend fun findOneById(id: Long, userId: Long? = null): Post? suspend fun findByUrl(url: String): Post? suspend fun delete(id: Long) + suspend fun findAll(since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List + suspend fun findByUserNameAndDomain(username: String, + s: String, + since: Instant?, + until: Instant?, + minId: Long?, + maxId: Long?, + limit: Int?, + userId: Long?): List + + suspend fun findByUserId(idOrNull: Long, since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List } diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Posts.kt b/src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Posts.kt index ee533159..2312d575 100644 --- a/src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Posts.kt +++ b/src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Posts.kt @@ -1,9 +1,7 @@ package dev.usbharu.hideout.routing.api.internal.v1 -import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto import dev.usbharu.hideout.domain.model.hideout.form.Post import dev.usbharu.hideout.exception.ParameterNotExistException -import dev.usbharu.hideout.exception.PostNotFoundException import dev.usbharu.hideout.plugins.TOKEN_AUTH import dev.usbharu.hideout.service.api.IPostApiService import dev.usbharu.hideout.util.InstantParseUtil @@ -24,15 +22,7 @@ fun Route.posts(postApiService: IPostApiService) { val userId = principal.payload.getClaim("uid").asLong() val receive = call.receive() - val postCreateDto = PostCreateDto( - text = receive.text, - overview = receive.overview, - visibility = receive.visibility, - repostId = receive.repostId, - repolyId = receive.replyId, - userId = userId - ) - val create = postApiService.createPost(postCreateDto) + val create = postApiService.createPost(receive, userId) call.response.header("Location", create.url) call.respond(HttpStatusCode.OK) } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/api/IPostApiService.kt b/src/main/kotlin/dev/usbharu/hideout/service/api/IPostApiService.kt index d54d48cb..00b6ff03 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/api/IPostApiService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/api/IPostApiService.kt @@ -1,11 +1,10 @@ package dev.usbharu.hideout.service.api -import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto import dev.usbharu.hideout.domain.model.hideout.entity.Post import java.time.Instant interface IPostApiService { - suspend fun createPost(postCreateDto: PostCreateDto): Post + suspend fun createPost(postForm: dev.usbharu.hideout.domain.model.hideout.form.Post, userId: Long): Post suspend fun getById(id: Long, userId: Long?): Post suspend fun getAll(since: Instant? = null, until: Instant? = null, diff --git a/src/main/kotlin/dev/usbharu/hideout/service/api/PostApiServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/api/PostApiServiceImpl.kt new file mode 100644 index 00000000..9061224e --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/service/api/PostApiServiceImpl.kt @@ -0,0 +1,45 @@ +package dev.usbharu.hideout.service.api + +import dev.usbharu.hideout.config.Config +import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto +import dev.usbharu.hideout.domain.model.hideout.entity.Post +import dev.usbharu.hideout.exception.PostNotFoundException +import dev.usbharu.hideout.repository.IPostRepository +import dev.usbharu.hideout.service.post.IPostService +import dev.usbharu.hideout.util.AcctUtil +import org.koin.core.annotation.Single +import java.time.Instant + +@Single +class PostApiServiceImpl(private val postService: IPostService, private val postRepository: IPostRepository) : IPostApiService { + override suspend fun createPost(postForm: dev.usbharu.hideout.domain.model.hideout.form.Post, userId: Long): Post { + return postService.createLocal(PostCreateDto( + text = postForm.text, + overview = postForm.overview, + visibility = postForm.visibility, + repostId = postForm.repostId, + repolyId = postForm.replyId, + userId = userId + )) + } + + override suspend fun getById(id: Long, userId: Long?): Post { + return postRepository.findOneById(id, userId) + ?: throw PostNotFoundException("$id was not found or is not authorized.") + } + + override suspend fun getAll(since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List { + return postRepository.findAll(since, until, minId, maxId, limit, userId) + } + + override suspend fun getByUser(nameOrId: String, since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List { + val idOrNull = nameOrId.toLongOrNull() + return if (idOrNull == null) { + val acct = AcctUtil.parse(nameOrId) + postRepository.findByUserNameAndDomain(acct.username, acct.domain + ?: Config.configData.domain, since, until, minId, maxId, limit, userId) + } else { + postRepository.findByUserId(idOrNull, since, until, minId, maxId, limit, userId) + } + } +} diff --git a/src/main/kotlin/dev/usbharu/hideout/service/post/IPostService.kt b/src/main/kotlin/dev/usbharu/hideout/service/post/IPostService.kt index 970eaa26..298dae0a 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/post/IPostService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/post/IPostService.kt @@ -1,9 +1,10 @@ package dev.usbharu.hideout.service.post import dev.usbharu.hideout.domain.model.ap.Note +import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto import dev.usbharu.hideout.domain.model.hideout.entity.Post interface IPostService { - suspend fun createLocal(post: Post): Post + suspend fun createLocal(post: PostCreateDto): Post suspend fun createRemote(note: Note): Post }