feat: APIの返答にuserも含めるように

This commit is contained in:
usbharu 2023-06-16 17:15:40 +09:00
parent 3c55a36fed
commit c48ae3b698
5 changed files with 82 additions and 33 deletions

View File

@ -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
)
}
}
}

View File

@ -88,7 +88,9 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe
limit: Int?, limit: Int?,
userId: Long? userId: Long?
): List<Post> { ): List<Post> {
TODO("Not yet implemented") return query {
Posts.select { Posts.visibility eq Visibility.PUBLIC.ordinal }.map { it.toPost() }
}
} }
override suspend fun findByUserNameAndDomain( override suspend fun findByUserNameAndDomain(

View File

@ -1,12 +1,12 @@
package dev.usbharu.hideout.service.api 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 import java.time.Instant
@Suppress("LongParameterList") @Suppress("LongParameterList")
interface IPostApiService { interface IPostApiService {
suspend fun createPost(postForm: dev.usbharu.hideout.domain.model.hideout.form.Post, 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?): Post suspend fun getById(id: Long, userId: Long?): PostResponse
suspend fun getAll( suspend fun getAll(
since: Instant? = null, since: Instant? = null,
until: Instant? = null, until: Instant? = null,
@ -14,7 +14,7 @@ interface IPostApiService {
maxId: Long? = null, maxId: Long? = null,
limit: Int? = null, limit: Int? = null,
userId: Long? = null userId: Long? = null
): List<Post> ): List<PostResponse>
suspend fun getByUser( suspend fun getByUser(
nameOrId: String, nameOrId: String,
@ -24,5 +24,5 @@ interface IPostApiService {
maxId: Long? = null, maxId: Long? = null,
limit: Int? = null, limit: Int? = null,
userId: Long? = null userId: Long? = null
): List<Post> ): List<PostResponse>
} }

View File

@ -2,11 +2,16 @@ package dev.usbharu.hideout.service.api
import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.config.Config
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.dto.PostResponse
import dev.usbharu.hideout.exception.PostNotFoundException import dev.usbharu.hideout.repository.*
import dev.usbharu.hideout.repository.IPostRepository
import dev.usbharu.hideout.service.post.IPostService import dev.usbharu.hideout.service.post.IPostService
import dev.usbharu.hideout.util.AcctUtil 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 org.koin.core.annotation.Single
import java.time.Instant import java.time.Instant
import dev.usbharu.hideout.domain.model.hideout.form.Post as FormPost 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 @Single
class PostApiServiceImpl( class PostApiServiceImpl(
private val postService: IPostService, private val postService: IPostService,
private val postRepository: IPostRepository private val postRepository: IPostRepository,
private val userRepository: IUserRepository
) : IPostApiService { ) : IPostApiService {
override suspend fun createPost(postForm: FormPost, userId: Long): Post { override suspend fun createPost(postForm: FormPost, userId: Long): PostResponse {
return postService.createLocal( val createdPost = postService.createLocal(
PostCreateDto( PostCreateDto(
text = postForm.text, text = postForm.text,
overview = postForm.overview, overview = postForm.overview,
@ -27,11 +33,20 @@ class PostApiServiceImpl(
userId = userId userId = userId
) )
) )
val creator = userRepository.findById(userId)
return PostResponse.from(createdPost, creator!!)
} }
override suspend fun getById(id: Long, userId: Long?): Post { @Suppress("InjectDispatcher")
return postRepository.findOneById(id, userId) suspend fun <T> query(block: suspend () -> T): T =
?: throw PostNotFoundException("$id was not found or is not authorized.") 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( override suspend fun getAll(
@ -41,7 +56,12 @@ class PostApiServiceImpl(
maxId: Long?, maxId: Long?,
limit: Int?, limit: Int?,
userId: Long? userId: Long?
): List<Post> = postRepository.findAll(since, until, minId, maxId, limit, userId) ): List<PostResponse> {
return query {
Posts.innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id }).selectAll()
.map { PostResponse.from(it.toPost(), it.toUser()) }
}
}
override suspend fun getByUser( override suspend fun getByUser(
nameOrId: String, nameOrId: String,
@ -51,23 +71,22 @@ class PostApiServiceImpl(
maxId: Long?, maxId: Long?,
limit: Int?, limit: Int?,
userId: Long? userId: Long?
): List<Post> { ): List<PostResponse> {
val idOrNull = nameOrId.toLongOrNull() val idOrNull = nameOrId.toLongOrNull()
return if (idOrNull == null) { return if (idOrNull == null) {
val acct = AcctUtil.parse(nameOrId) val acct = AcctUtil.parse(nameOrId)
postRepository.findByUserNameAndDomain( query {
username = acct.username, Posts.innerJoin(Users, onColumn = { Posts.userId }, otherColumn = { id }).select {
s = acct.domain Users.name.eq(acct.username)
?: Config.configData.domain, .and(Users.domain eq (acct.domain ?: Config.configData.domain))
since = since, }.map { PostResponse.from(it.toPost(), it.toUser()) }
until = until, }
minId = minId,
maxId = maxId,
limit = limit,
userId = userId
)
} else { } 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()) }
}
} }
} }
} }

View File

@ -1,4 +1,4 @@
import { defineConfig } from 'vite'; import {defineConfig} from 'vite';
import solidPlugin from 'vite-plugin-solid'; import solidPlugin from 'vite-plugin-solid';
import suidPlugin from "@suid/vite-plugin"; import suidPlugin from "@suid/vite-plugin";
@ -8,9 +8,6 @@ export default defineConfig({
port: 3000, port: 3000,
proxy: { proxy: {
'/api': 'http://localhost:8080', '/api': 'http://localhost:8080',
'/login': 'http://localhost:8080',
'/auth-check': 'http://localhost:8080',
'/refresh-token': 'http://localhost:8080',
} }
}, },
root: './src/main/web', root: './src/main/web',