diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/dto/PostResponse.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/dto/PostResponse.kt new file mode 100644 index 00000000..6be3ba4d --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/dto/PostResponse.kt @@ -0,0 +1,31 @@ +package dev.usbharu.hideout.domain.model.hideout.dto + +import dev.usbharu.hideout.domain.model.hideout.entity.Post +import dev.usbharu.hideout.domain.model.hideout.entity.User +import dev.usbharu.hideout.domain.model.hideout.entity.Visibility + +data class PostResponse( + val id: Long, + val user: UserResponse, + val overview: String? = null, + val text: String? = null, + val createdAt: Long, + val visibility: Visibility, + val url: String, + val sensitive: Boolean = false, +) { + companion object { + fun from(post: Post, user: User): PostResponse { + return PostResponse( + post.id, + UserResponse.from(user), + post.overview, + post.text, + post.createdAt, + post.visibility, + post.url, + post.sensitive + ) + } + } +} diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt index 6360e609..12156818 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt @@ -88,7 +88,9 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe limit: Int?, userId: Long? ): List { - TODO("Not yet implemented") + return query { + Posts.select { Posts.visibility eq Visibility.PUBLIC.ordinal }.map { it.toPost() } + } } override suspend fun findByUserNameAndDomain( 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 eb7eb97a..6aa54403 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/api/IPostApiService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/api/IPostApiService.kt @@ -1,12 +1,12 @@ package dev.usbharu.hideout.service.api -import dev.usbharu.hideout.domain.model.hideout.entity.Post +import dev.usbharu.hideout.domain.model.hideout.dto.PostResponse import java.time.Instant @Suppress("LongParameterList") interface IPostApiService { - 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 createPost(postForm: dev.usbharu.hideout.domain.model.hideout.form.Post, userId: Long): PostResponse + suspend fun getById(id: Long, userId: Long?): PostResponse suspend fun getAll( since: Instant? = null, until: Instant? = null, @@ -14,7 +14,7 @@ interface IPostApiService { maxId: Long? = null, limit: Int? = null, userId: Long? = null - ): List + ): List suspend fun getByUser( nameOrId: String, @@ -24,5 +24,5 @@ interface IPostApiService { maxId: Long? = null, limit: Int? = null, userId: Long? = null - ): List + ): List } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/api/PostApiServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/api/PostApiServiceImpl.kt index 35e05585..b8957f87 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/api/PostApiServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/api/PostApiServiceImpl.kt @@ -2,11 +2,16 @@ 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.domain.model.hideout.dto.PostResponse +import dev.usbharu.hideout.repository.* import dev.usbharu.hideout.service.post.IPostService import dev.usbharu.hideout.util.AcctUtil +import kotlinx.coroutines.Dispatchers +import org.jetbrains.exposed.sql.and +import org.jetbrains.exposed.sql.innerJoin +import org.jetbrains.exposed.sql.select +import org.jetbrains.exposed.sql.selectAll +import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import org.koin.core.annotation.Single import java.time.Instant import dev.usbharu.hideout.domain.model.hideout.form.Post as FormPost @@ -14,10 +19,11 @@ import dev.usbharu.hideout.domain.model.hideout.form.Post as FormPost @Single class PostApiServiceImpl( private val postService: IPostService, - private val postRepository: IPostRepository + private val postRepository: IPostRepository, + private val userRepository: IUserRepository ) : IPostApiService { - override suspend fun createPost(postForm: FormPost, userId: Long): Post { - return postService.createLocal( + override suspend fun createPost(postForm: FormPost, userId: Long): PostResponse { + val createdPost = postService.createLocal( PostCreateDto( text = postForm.text, overview = postForm.overview, @@ -27,11 +33,20 @@ class PostApiServiceImpl( userId = userId ) ) + val creator = userRepository.findById(userId) + return PostResponse.from(createdPost, creator!!) } - override suspend fun getById(id: Long, userId: Long?): Post { - return postRepository.findOneById(id, userId) - ?: throw PostNotFoundException("$id was not found or is not authorized.") + @Suppress("InjectDispatcher") + suspend fun query(block: suspend () -> T): T = + newSuspendedTransaction(Dispatchers.IO) { block() } + + override suspend fun getById(id: Long, userId: Long?): PostResponse { + val query = query { + Posts.innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { Users.id }).select { Posts.id eq id } + .single() + } + return PostResponse.from(query.toPost(), query.toUser()) } override suspend fun getAll( @@ -41,7 +56,12 @@ class PostApiServiceImpl( maxId: Long?, limit: Int?, userId: Long? - ): List = postRepository.findAll(since, until, minId, maxId, limit, userId) + ): List { + return query { + Posts.innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id }).selectAll() + .map { PostResponse.from(it.toPost(), it.toUser()) } + } + } override suspend fun getByUser( nameOrId: String, @@ -51,23 +71,22 @@ class PostApiServiceImpl( maxId: Long?, limit: Int?, userId: Long? - ): List { + ): List { val idOrNull = nameOrId.toLongOrNull() return if (idOrNull == null) { val acct = AcctUtil.parse(nameOrId) - postRepository.findByUserNameAndDomain( - username = acct.username, - s = acct.domain - ?: Config.configData.domain, - since = since, - until = until, - minId = minId, - maxId = maxId, - limit = limit, - userId = userId - ) + query { + Posts.innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id }).select { + Users.name.eq(acct.username) + .and(Users.domain eq (acct.domain ?: Config.configData.domain)) + }.map { PostResponse.from(it.toPost(), it.toUser()) } + } } else { - postRepository.findByUserId(idOrNull, since, until, minId, maxId, limit, userId) + query { + Posts.innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id }).select { + Posts.userId eq idOrNull + }.map { PostResponse.from(it.toPost(), it.toUser()) } + } } } } diff --git a/vite.config.ts b/vite.config.ts index 3f3c0c58..e0e384eb 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,4 +1,4 @@ -import { defineConfig } from 'vite'; +import {defineConfig} from 'vite'; import solidPlugin from 'vite-plugin-solid'; import suidPlugin from "@suid/vite-plugin"; @@ -8,9 +8,6 @@ export default defineConfig({ port: 3000, proxy: { '/api': 'http://localhost:8080', - '/login': 'http://localhost:8080', - '/auth-check': 'http://localhost:8080', - '/refresh-token': 'http://localhost:8080', } }, root: './src/main/web',