mirror of https://github.com/usbharu/Hideout.git
				
				
				
			feat: PostApiServiceを実装
This commit is contained in:
		
							parent
							
								
									de44bd7fc6
								
							
						
					
					
						commit
						d14e99b1a1
					
				|  | @ -1,11 +1,23 @@ | |||
| package dev.usbharu.hideout.repository | ||||
| 
 | ||||
| import dev.usbharu.hideout.domain.model.hideout.entity.Post | ||||
| import java.time.Instant | ||||
| 
 | ||||
| interface IPostRepository { | ||||
|     suspend fun generateId(): Long | ||||
|     suspend fun save(post: Post): Post | ||||
|     suspend fun findOneById(id: Long): Post? | ||||
|     suspend fun findOneById(id: Long, userId: Long? = null): Post? | ||||
|     suspend fun findByUrl(url: String): Post? | ||||
|     suspend fun delete(id: Long) | ||||
|     suspend fun findAll(since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List<Post> | ||||
|     suspend fun findByUserNameAndDomain(username: String, | ||||
|                                         s: String, | ||||
|                                         since: Instant?, | ||||
|                                         until: Instant?, | ||||
|                                         minId: Long?, | ||||
|                                         maxId: Long?, | ||||
|                                         limit: Int?, | ||||
|                                         userId: Long?): List<Post> | ||||
| 
 | ||||
|     suspend fun findByUserId(idOrNull: Long, since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List<Post> | ||||
| } | ||||
|  |  | |||
|  | @ -1,9 +1,7 @@ | |||
| package dev.usbharu.hideout.routing.api.internal.v1 | ||||
| 
 | ||||
| import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto | ||||
| import dev.usbharu.hideout.domain.model.hideout.form.Post | ||||
| import dev.usbharu.hideout.exception.ParameterNotExistException | ||||
| import dev.usbharu.hideout.exception.PostNotFoundException | ||||
| import dev.usbharu.hideout.plugins.TOKEN_AUTH | ||||
| import dev.usbharu.hideout.service.api.IPostApiService | ||||
| import dev.usbharu.hideout.util.InstantParseUtil | ||||
|  | @ -24,15 +22,7 @@ fun Route.posts(postApiService: IPostApiService) { | |||
|                 val userId = principal.payload.getClaim("uid").asLong() | ||||
| 
 | ||||
|                 val receive = call.receive<Post>() | ||||
|                 val postCreateDto = PostCreateDto( | ||||
|                         text = receive.text, | ||||
|                         overview = receive.overview, | ||||
|                         visibility = receive.visibility, | ||||
|                         repostId = receive.repostId, | ||||
|                         repolyId = receive.replyId, | ||||
|                         userId = userId | ||||
|                 ) | ||||
|                 val create = postApiService.createPost(postCreateDto) | ||||
|                 val create = postApiService.createPost(receive, userId) | ||||
|                 call.response.header("Location", create.url) | ||||
|                 call.respond(HttpStatusCode.OK) | ||||
|             } | ||||
|  |  | |||
|  | @ -1,11 +1,10 @@ | |||
| package dev.usbharu.hideout.service.api | ||||
| 
 | ||||
| import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto | ||||
| import dev.usbharu.hideout.domain.model.hideout.entity.Post | ||||
| import java.time.Instant | ||||
| 
 | ||||
| interface IPostApiService { | ||||
|     suspend fun createPost(postCreateDto: PostCreateDto): Post | ||||
|     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 getAll(since: Instant? = null, | ||||
|                        until: Instant? = null, | ||||
|  |  | |||
|  | @ -0,0 +1,45 @@ | |||
| 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.service.post.IPostService | ||||
| import dev.usbharu.hideout.util.AcctUtil | ||||
| import org.koin.core.annotation.Single | ||||
| import java.time.Instant | ||||
| 
 | ||||
| @Single | ||||
| class PostApiServiceImpl(private val postService: IPostService, private val postRepository: IPostRepository) : IPostApiService { | ||||
|     override suspend fun createPost(postForm: dev.usbharu.hideout.domain.model.hideout.form.Post, userId: Long): Post { | ||||
|         return postService.createLocal(PostCreateDto( | ||||
|                 text = postForm.text, | ||||
|                 overview = postForm.overview, | ||||
|                 visibility = postForm.visibility, | ||||
|                 repostId = postForm.repostId, | ||||
|                 repolyId = postForm.replyId, | ||||
|                 userId = userId | ||||
|         )) | ||||
|     } | ||||
| 
 | ||||
|     override suspend fun getById(id: Long, userId: Long?): Post { | ||||
|         return postRepository.findOneById(id, userId) | ||||
|                 ?: throw PostNotFoundException("$id was not found or is not authorized.") | ||||
|     } | ||||
| 
 | ||||
|     override suspend fun getAll(since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List<Post> { | ||||
|         return postRepository.findAll(since, until, minId, maxId, limit, userId) | ||||
|     } | ||||
| 
 | ||||
|     override suspend fun getByUser(nameOrId: String, since: Instant?, until: Instant?, minId: Long?, maxId: Long?, limit: Int?, userId: Long?): List<Post> { | ||||
|         val idOrNull = nameOrId.toLongOrNull() | ||||
|         return if (idOrNull == null) { | ||||
|             val acct = AcctUtil.parse(nameOrId) | ||||
|             postRepository.findByUserNameAndDomain(acct.username, acct.domain | ||||
|                     ?: Config.configData.domain, since, until, minId, maxId, limit, userId) | ||||
|         } else { | ||||
|             postRepository.findByUserId(idOrNull, since, until, minId, maxId, limit, userId) | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | @ -1,9 +1,10 @@ | |||
| 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.entity.Post | ||||
| 
 | ||||
| interface IPostService { | ||||
|     suspend fun createLocal(post: Post): Post | ||||
|     suspend fun createLocal(post: PostCreateDto): Post | ||||
|     suspend fun createRemote(note: Note): Post | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue