feat: UsersのAPIの返り値を修正

This commit is contained in:
usbharu 2023-05-20 15:05:27 +09:00
parent 9c8aef6c44
commit ec5dca4ed2
7 changed files with 167 additions and 16 deletions

View File

@ -1,5 +1,7 @@
package dev.usbharu.hideout.domain.model.hideout.dto
import dev.usbharu.hideout.domain.model.hideout.entity.User
data class UserResponse(
val id: Long,
val name: String,
@ -8,4 +10,18 @@ data class UserResponse(
val description: String = "",
val url: String,
val createdAt: Long
)
) {
companion object {
fun from(user: User): UserResponse {
return UserResponse(
user.id,
user.name,
user.domain,
user.screenName,
user.description,
user.url,
user.createdAt.toEpochMilli()
)
}
}
}

View File

@ -5,6 +5,7 @@ import dev.usbharu.hideout.domain.model.hideout.dto.UserCreateDto
import dev.usbharu.hideout.domain.model.hideout.form.UserCreate
import dev.usbharu.hideout.exception.ParameterNotExistException
import dev.usbharu.hideout.plugins.TOKEN_AUTH
import dev.usbharu.hideout.service.IUserApiService
import dev.usbharu.hideout.service.impl.IUserService
import dev.usbharu.hideout.util.AcctUtil
import io.ktor.http.*
@ -15,10 +16,10 @@ import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
fun Route.users(userService: IUserService) {
fun Route.users(userService: IUserService, userApiService: IUserApiService) {
route("/users") {
get {
call.respond(userService.findAllForUser())
call.respond(userApiService.findAll())
}
post {
val userCreate = call.receive<UserCreate>()
@ -43,10 +44,10 @@ fun Route.users(userService: IUserService) {
val userParameter = (call.parameters["name"]
?: throw ParameterNotExistException("Parameter(name='userName@domain') does not exist."))
if (userParameter.toLongOrNull() != null) {
return@get call.respond(userService.findById(userParameter.toLong()))
return@get call.respond(userApiService.findById(userParameter.toLong()))
} else {
val acct = AcctUtil.parse(userParameter)
return@get call.respond(userService.findByNameAndDomain(acct.username, acct.domain))
return@get call.respond(userApiService.findByAcct(acct))
}
}
}
@ -56,10 +57,10 @@ fun Route.users(userService: IUserService) {
val userParameter = call.parameters["name"]
?: throw ParameterNotExistException("Parameter(name='userName@domain') does not exist.")
if (userParameter.toLongOrNull() != null) {
return@get call.respond(userService.findFollowersById(userParameter.toLong()))
return@get call.respond(userApiService.findFollowers(userParameter.toLong()))
}
val acct = AcctUtil.parse(userParameter)
return@get call.respond(userService.findFollowersByNameAndDomain(acct.username, acct.domain))
return@get call.respond(userApiService.findFollowersByAcct(acct))
}
authenticate(TOKEN_AUTH) {
@ -76,7 +77,7 @@ fun Route.users(userService: IUserService) {
}
}
val acct = AcctUtil.parse(userParameter)
val targetUser = userService.findByNameAndDomain(acct.username, acct.domain)
val targetUser = userApiService.findByAcct(acct)
if (userService.addFollowers(targetUser.id, userId)) {
return@post call.respond(HttpStatusCode.OK)
} else {
@ -90,10 +91,10 @@ fun Route.users(userService: IUserService) {
val userParameter = (call.parameters["name"]
?: throw ParameterNotExistException("Parameter(name='userName@domain') does not exist."))
if (userParameter.toLongOrNull() != null) {
return@get call.respond(userService.findFollowingById(userParameter.toLong()))
return@get call.respond(userApiService.findFollowings(userParameter.toLong()))
}
val acct = AcctUtil.parse(userParameter)
return@get call.respond(userService.findFollowingByNameAndDomain(acct.username, acct.domain))
return@get call.respond(userApiService.findFollowingsByAcct(acct))
}
}
}

View File

@ -0,0 +1,24 @@
package dev.usbharu.hideout.service
import dev.usbharu.hideout.domain.model.Acct
import dev.usbharu.hideout.domain.model.hideout.dto.UserResponse
interface IUserApiService {
suspend fun findAll(limit: Int? = 100, offset: Long = 0): List<UserResponse>
suspend fun findById(id: Long): UserResponse
suspend fun findByIds(ids: List<Long>): List<UserResponse>
suspend fun findByAcct(acct: Acct): UserResponse
suspend fun findByAccts(accts: List<Acct>): List<UserResponse>
suspend fun findFollowers(userId: Long): List<UserResponse>
suspend fun findFollowings(userId: Long): List<UserResponse>
suspend fun findFollowersByAcct(acct: Acct): List<UserResponse>
suspend fun findFollowingsByAcct(acct: Acct): List<UserResponse>
}

View File

@ -0,0 +1,38 @@
package dev.usbharu.hideout.service
import dev.usbharu.hideout.config.Config
import dev.usbharu.hideout.domain.model.Acct
import dev.usbharu.hideout.domain.model.hideout.dto.UserResponse
import dev.usbharu.hideout.service.impl.IUserService
import org.koin.core.annotation.Single
@Single
class UserApiServiceImpl(private val userService: IUserService) : IUserApiService {
override suspend fun findAll(limit: Int?, offset: Long): List<UserResponse> =
userService.findAll(limit, offset).map { UserResponse.from(it) }
override suspend fun findById(id: Long): UserResponse = UserResponse.from(userService.findById(id))
override suspend fun findByIds(ids: List<Long>): List<UserResponse> =
userService.findByIds(ids).map { UserResponse.from(it) }
override suspend fun findByAcct(acct: Acct): UserResponse =
UserResponse.from(userService.findByNameAndDomain(acct.username, acct.domain))
override suspend fun findByAccts(accts: List<Acct>): List<UserResponse> {
return userService.findByNameAndDomains(accts.map { it.username to (it.domain ?: Config.configData.domain) })
.map { UserResponse.from(it) }
}
override suspend fun findFollowers(userId: Long): List<UserResponse> =
userService.findFollowersById(userId).map { UserResponse.from(it) }
override suspend fun findFollowings(userId: Long): List<UserResponse> =
userService.findFollowingById(userId).map { UserResponse.from(it) }
override suspend fun findFollowersByAcct(acct: Acct): List<UserResponse> =
userService.findFollowersByNameAndDomain(acct.username, acct.domain).map { UserResponse.from(it) }
override suspend fun findFollowingsByAcct(acct: Acct): List<UserResponse> =
userService.findFollowingByNameAndDomain(acct.username, acct.domain).map { UserResponse.from(it) }
}

View File

@ -2,15 +2,12 @@ package dev.usbharu.hideout.service.impl
import dev.usbharu.hideout.domain.model.hideout.dto.RemoteUserCreateDto
import dev.usbharu.hideout.domain.model.hideout.dto.UserCreateDto
import dev.usbharu.hideout.domain.model.hideout.dto.UserResponse
import dev.usbharu.hideout.domain.model.hideout.entity.User
@Suppress("TooManyFunctions")
interface IUserService {
suspend fun findAll(limit: Int? = 100, offset: Long? = 0): List<User>
suspend fun findAllForUser(limit: Int? = 100, offset: Long? = 0): List<UserResponse>
suspend fun findById(id: Long): User
suspend fun findByIds(ids: List<Long>): List<User>

View File

@ -24,7 +24,7 @@ class UserService(private val userRepository: IUserRepository, private val userA
)
}
override suspend fun findAllForUser(limit: Int?, offset: Long?): List<UserResponse> {
suspend fun findAllForUser(limit: Int?, offset: Long?): List<UserResponse> {
TODO("Not yet implemented")
}

View File

@ -112,6 +112,15 @@ paths:
required: false
schema:
type: "integer"
responses:
"200":
description: "OK"
content:
'*/*':
schema:
type: "array"
items:
$ref: "#/components/schemas/Post"
post:
description: ""
requestBody:
@ -127,6 +136,61 @@ paths:
'*/*':
schema:
type: "object"
/api/internal/v1/posts/{id}:
get:
description: ""
parameters:
- name: "id"
in: "path"
required: true
schema:
type: "number"
responses:
"200":
description: "OK"
content:
'*/*':
schema:
$ref: "#/components/schemas/Post"
/api/internal/v1/users/{name}/posts:
get:
description: ""
parameters:
- name: "name"
in: "path"
required: true
schema:
type: "string"
responses:
"200":
description: "OK"
content:
'*/*':
schema:
type: "array"
items:
$ref: "#/components/schemas/Post"
/api/internal/v1/users/{name}/posts/{id}:
get:
description: ""
parameters:
- name: "id"
in: "path"
required: true
schema:
type: "number"
- name: "name"
in: "path"
required: true
schema:
type: "string"
responses:
"200":
description: "OK"
content:
'*/*':
schema:
$ref: "#/components/schemas/Post"
/inbox:
get:
description: ""
@ -346,10 +410,19 @@ components:
Post:
type: "object"
properties:
text:
type: "string"
id:
type: "integer"
format: "int64"
userId:
type: "integer"
format: "int64"
overview:
type: "string"
text:
type: "string"
createdAt:
type: "integer"
format: "int64"
visibility:
type: "string"
enum:
@ -357,6 +430,8 @@ components:
- "UNLISTED"
- "FOLLOWERS"
- "DIRECT"
url:
type: "string"
repostId:
type: "integer"
format: "int64"