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

This commit is contained in:
usbharu 2023-06-16 17:15:40 +09:00
parent fdc6e1bae4
commit 0dd7facbad
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
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?,
userId: Long?
): List<Post> {
TODO("Not yet implemented")
return query {
Posts.select { Posts.visibility eq Visibility.PUBLIC.ordinal }.map { it.toPost() }
}
}
override suspend fun findByUserNameAndDomain(

View File

@ -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<Post>
): List<PostResponse>
suspend fun getByUser(
nameOrId: String,
@ -24,5 +24,5 @@ interface IPostApiService {
maxId: Long? = null,
limit: Int? = 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.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 <T> 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<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(
nameOrId: String,
@ -51,23 +71,22 @@ class PostApiServiceImpl(
maxId: Long?,
limit: Int?,
userId: Long?
): List<Post> {
): List<PostResponse> {
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()) }
}
}
}
}

View File

@ -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',