diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt index b547f446..e50a0f09 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt @@ -9,6 +9,7 @@ import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import org.jetbrains.exposed.sql.transactions.transaction import org.koin.core.annotation.Single +import java.time.Instant @Single class PostRepositoryImpl(database: Database, private val idGenerateService: IdGenerateService) : IPostRepository { @@ -45,10 +46,8 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe } } - override suspend fun findOneById(id: Long): Post? { - return query { - Posts.select { Posts.id eq id }.singleOrNull()?.toPost() - } + override suspend fun findOneById(id: Long, userId: Long?): Post? { + TODO("Not yet implemented") } override suspend fun findByUrl(url: String): Post? { @@ -62,6 +61,18 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe Posts.deleteWhere { Posts.id eq id } } } + + override suspend fun findAll(since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List { + TODO("Not yet implemented") + } + + override suspend fun findByUserNameAndDomain(username: String, s: String, since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List { + TODO("Not yet implemented") + } + + override suspend fun findByUserId(idOrNull: Long, since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List { + TODO("Not yet implemented") + } } object Posts : Table() { 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 298dae0a..4459b8d2 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/post/IPostService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/post/IPostService.kt @@ -1,10 +1,8 @@ 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: PostCreateDto): Post - suspend fun createRemote(note: Note): Post } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt new file mode 100644 index 00000000..fa9e20f1 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/service/post/PostServiceImpl.kt @@ -0,0 +1,35 @@ +package dev.usbharu.hideout.service.post + +import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto +import dev.usbharu.hideout.domain.model.hideout.entity.Post +import dev.usbharu.hideout.exception.UserNotFoundException +import dev.usbharu.hideout.repository.IPostRepository +import dev.usbharu.hideout.repository.IUserRepository +import dev.usbharu.hideout.service.activitypub.ActivityPubNoteService +import org.koin.core.annotation.Single +import java.time.Instant + +@Single +class PostServiceImpl(private val postRepository: IPostRepository, private val userRepository: IUserRepository, private val activityPubNoteService: ActivityPubNoteService) : IPostService { + override suspend fun createLocal(post: PostCreateDto): Post { + val user = userRepository.findById(post.userId) ?: throw UserNotFoundException("${post.userId} was not found") + val id = postRepository.generateId() + val createPost = Post( + id = id, + userId = post.userId, + overview = post.overview, + text = post.text, + createdAt = Instant.now().toEpochMilli(), + visibility = post.visibility, + url = "${user.url}/posts/$id", + repostId = null, + replyId = null + ) + activityPubNoteService.createNote(createPost) + return internalCreate(createPost) + } + + private suspend fun internalCreate(post: Post): Post { + return postRepository.save(post) + } +}