mirror of https://github.com/usbharu/Hideout.git
feat: PostServiceを実装
This commit is contained in:
parent
e8eccaa39b
commit
48d44bdd0f
|
@ -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.experimental.newSuspendedTransaction
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
import org.koin.core.annotation.Single
|
import org.koin.core.annotation.Single
|
||||||
|
import java.time.Instant
|
||||||
|
|
||||||
@Single
|
@Single
|
||||||
class PostRepositoryImpl(database: Database, private val idGenerateService: IdGenerateService) : IPostRepository {
|
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? {
|
override suspend fun findOneById(id: Long, userId: Long?): Post? {
|
||||||
return query {
|
TODO("Not yet implemented")
|
||||||
Posts.select { Posts.id eq id }.singleOrNull()?.toPost()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun findByUrl(url: String): Post? {
|
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 }
|
Posts.deleteWhere { Posts.id eq id }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun findAll(since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List<Post> {
|
||||||
|
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<Post> {
|
||||||
|
TODO("Not yet implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun findByUserId(idOrNull: Long, since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List<Post> {
|
||||||
|
TODO("Not yet implemented")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
object Posts : Table() {
|
object Posts : Table() {
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
package dev.usbharu.hideout.service.post
|
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.dto.PostCreateDto
|
||||||
import dev.usbharu.hideout.domain.model.hideout.entity.Post
|
import dev.usbharu.hideout.domain.model.hideout.entity.Post
|
||||||
|
|
||||||
interface IPostService {
|
interface IPostService {
|
||||||
suspend fun createLocal(post: PostCreateDto): Post
|
suspend fun createLocal(post: PostCreateDto): Post
|
||||||
suspend fun createRemote(note: Note): Post
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue