mirror of https://github.com/usbharu/Hideout.git
feat: 投稿を追加
This commit is contained in:
parent
7f11c1b605
commit
6234315086
|
@ -0,0 +1,51 @@
|
||||||
|
package dev.usbharu.hideout.domain.model
|
||||||
|
|
||||||
|
import org.jetbrains.exposed.sql.ResultRow
|
||||||
|
import org.jetbrains.exposed.sql.Table
|
||||||
|
|
||||||
|
object Posts : Table() {
|
||||||
|
val id = long("id")
|
||||||
|
val userId = long("userId").references(Users.id)
|
||||||
|
val overview = varchar("overview", 100).nullable()
|
||||||
|
val text = varchar("text", 3000)
|
||||||
|
val createdAt = long("createdAt")
|
||||||
|
val visibility = integer("visibility").default(0)
|
||||||
|
val url = varchar("url", 500)
|
||||||
|
val repostId = long("repostId").references(id).nullable()
|
||||||
|
val replyId = long("replyId").references(id).nullable()
|
||||||
|
}
|
||||||
|
|
||||||
|
data class Post(
|
||||||
|
val userId: Long,
|
||||||
|
val overview: String? = null,
|
||||||
|
val text: String,
|
||||||
|
val createdAt: Long,
|
||||||
|
val visibility: Int,
|
||||||
|
val url: String,
|
||||||
|
val repostId: Long? = null,
|
||||||
|
val replyId: Long? = null
|
||||||
|
)
|
||||||
|
|
||||||
|
data class PostEntity(
|
||||||
|
val id: Long,
|
||||||
|
val overview: String? = null,
|
||||||
|
val text: String,
|
||||||
|
val createdAt: Long,
|
||||||
|
val visibility: Int,
|
||||||
|
val url: String,
|
||||||
|
val repostId: Long? = null,
|
||||||
|
val replyId: Long? = null
|
||||||
|
)
|
||||||
|
|
||||||
|
fun ResultRow.toPost():PostEntity{
|
||||||
|
return PostEntity(
|
||||||
|
id = this[Posts.id],
|
||||||
|
overview = this[Posts.overview],
|
||||||
|
text = this[Posts.text],
|
||||||
|
createdAt = this[Posts.createdAt],
|
||||||
|
visibility = this[Posts.visibility],
|
||||||
|
url = this[Posts.url],
|
||||||
|
repostId = this[Posts.repostId],
|
||||||
|
replyId = this[Posts.replyId]
|
||||||
|
)
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package dev.usbharu.hideout.repository
|
||||||
|
|
||||||
|
import dev.usbharu.hideout.domain.model.Post
|
||||||
|
import dev.usbharu.hideout.domain.model.PostEntity
|
||||||
|
|
||||||
|
interface IPostRepository {
|
||||||
|
suspend fun insert(post:Post):PostEntity
|
||||||
|
suspend fun findOneById(id:Long):PostEntity
|
||||||
|
suspend fun delete(id:Long)
|
||||||
|
}
|
|
@ -0,0 +1,64 @@
|
||||||
|
package dev.usbharu.hideout.repository
|
||||||
|
|
||||||
|
import dev.usbharu.hideout.domain.model.Post
|
||||||
|
import dev.usbharu.hideout.domain.model.PostEntity
|
||||||
|
import dev.usbharu.hideout.domain.model.Posts
|
||||||
|
import dev.usbharu.hideout.domain.model.toPost
|
||||||
|
import dev.usbharu.hideout.service.IdGenerateService
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import org.jetbrains.exposed.sql.*
|
||||||
|
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||||
|
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
|
||||||
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
|
class PostRepositoryImpl(database: Database, private val idGenerateService: IdGenerateService) : IPostRepository {
|
||||||
|
|
||||||
|
init {
|
||||||
|
transaction(database) {
|
||||||
|
SchemaUtils.create(Posts)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun <T> query(block: suspend () -> T): T =
|
||||||
|
newSuspendedTransaction(Dispatchers.IO) { block() }
|
||||||
|
|
||||||
|
override suspend fun insert(post: Post): PostEntity {
|
||||||
|
return query {
|
||||||
|
|
||||||
|
val generateId = idGenerateService.generateId()
|
||||||
|
Posts.insert {
|
||||||
|
it[id] = generateId
|
||||||
|
it[userId] = post.userId
|
||||||
|
it[overview] = post.overview
|
||||||
|
it[text] = post.text
|
||||||
|
it[createdAt] = post.createdAt
|
||||||
|
it[visibility] = post.visibility
|
||||||
|
it[url] = post.url
|
||||||
|
it[repostId] = post.repostId
|
||||||
|
it[replyId] = post.replyId
|
||||||
|
}
|
||||||
|
return@query PostEntity(
|
||||||
|
generateId,
|
||||||
|
post.overview,
|
||||||
|
post.text,
|
||||||
|
post.createdAt,
|
||||||
|
post.visibility,
|
||||||
|
post.url,
|
||||||
|
post.repostId,
|
||||||
|
post.replyId
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun findOneById(id: Long): PostEntity {
|
||||||
|
return query {
|
||||||
|
Posts.select { Posts.id eq id }.single().toPost()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun delete(id: Long) {
|
||||||
|
return query {
|
||||||
|
Posts.deleteWhere { Posts.id eq id }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue