Skip to content

Commit

Permalink
chore: Database changes to allow multi unique column
Browse files Browse the repository at this point in the history
  • Loading branch information
LichtHund committed Jul 4, 2024
1 parent 37c6b78 commit dda7f23
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 34 deletions.
8 changes: 2 additions & 6 deletions backend/src/main/kotlin/Module.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,8 @@ public fun Application.module() {

routing {

staticResources("/static", "static") {
preCompressed(CompressedFileType.BROTLI, CompressedFileType.GZIP)
}
staticFiles("/assets", DATA_FOLDER.resolve("core")) {
preCompressed(CompressedFileType.BROTLI, CompressedFileType.GZIP)
}
staticResources("/static", "static")
staticFiles("/assets", DATA_FOLDER.resolve("core"))

install(CachingHeaders) {
options { _, outgoingContent ->
Expand Down
6 changes: 4 additions & 2 deletions backend/src/main/kotlin/api/ProjectSetup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public fun setupRepository(projects: File) {

project.versions.forEach { version ->

val versionEntity = DocVersionEntity.new(version.reference) {
val versionEntity = DocVersionEntity.new {
this.reference = version.reference
this.project = projectEntity
this.navigation = version.navigation
this.stable = version.stable
Expand All @@ -81,7 +82,8 @@ public fun setupRepository(projects: File) {
output = pageDir.resolve("banner.png"),
)

PageEntity.new(page.id) {
PageEntity.new {
this.pageId = page.id
this.project = projectEntity
this.version = versionEntity
this.content = page.content
Expand Down
31 changes: 20 additions & 11 deletions backend/src/main/kotlin/api/database/Entities.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import dev.triumphteam.website.project.Navigation
import dev.triumphteam.website.project.PageSummary
import org.jetbrains.exposed.dao.Entity
import org.jetbrains.exposed.dao.EntityClass
import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IdTable
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.ReferenceOption

Expand All @@ -18,29 +21,33 @@ public object Projects : IdTable<String>("projects") {
override val primaryKey: PrimaryKey = PrimaryKey(id)
}

public object DocVersions : IdTable<String>("docs_version") {
public override val id: Column<EntityID<String>> = varchar("version_id", 255).entityId()
public object DocVersions : IntIdTable("docs_version") {
public val reference: Column<String> = varchar("version_reference", 255)
public val project: Column<EntityID<String>> =
reference("project_id", Projects, ReferenceOption.CASCADE, ReferenceOption.CASCADE)
public val navigation: Column<Navigation> = serializable<Navigation>("navigation")
public val stable: Column<Boolean> = bool("stable")
public val recommended: Column<Boolean> = bool("recommended")

override val primaryKey: PrimaryKey = PrimaryKey(id)
init {
uniqueIndex("ref_project_uq", reference, project)
}
}

public object Pages : IdTable<String>("pages") {
public override val id: Column<EntityID<String>> = varchar("page_id", 255).entityId()
public object Pages : IntIdTable("pages") {
public val pageId: Column<String> = varchar("page_id", 255)
public val project: Column<EntityID<String>> =
reference("project_id", Projects, ReferenceOption.CASCADE, ReferenceOption.CASCADE)
public val version: Column<EntityID<String>> =
public val version: Column<EntityID<Int>> =
reference("version_id", DocVersions, ReferenceOption.CASCADE, ReferenceOption.CASCADE)
public val content: Column<String> = text("content")
public val title: Column<String> = text("title")
public val subTitle: Column<String> = text("sub_title")
public val summary: Column<PageSummary> = serializable<PageSummary>("summary")

override val primaryKey: PrimaryKey = PrimaryKey(id)
init {
uniqueIndex("page_project_version_uq", pageId, project, version)
}
}

public class ProjectEntity(id: EntityID<String>) : Entity<String>(id) {
Expand All @@ -51,18 +58,20 @@ public class ProjectEntity(id: EntityID<String>) : Entity<String>(id) {
public var github: String by Projects.github
}

public class DocVersionEntity(id: EntityID<String>) : Entity<String>(id) {
public companion object : EntityClass<String, DocVersionEntity>(DocVersions)
public class DocVersionEntity(id: EntityID<Int>) : IntEntity(id) {
public companion object : IntEntityClass<DocVersionEntity>(DocVersions)

public var reference: String by DocVersions.reference
public var project: ProjectEntity by ProjectEntity referencedOn DocVersions.project
public var navigation: Navigation by DocVersions.navigation
public var stable: Boolean by DocVersions.stable
public var recommended: Boolean by DocVersions.recommended
}

public class PageEntity(id: EntityID<String>) : Entity<String>(id) {
public companion object : EntityClass<String, PageEntity>(Pages)
public class PageEntity(id: EntityID<Int>) : IntEntity(id) {
public companion object : IntEntityClass<PageEntity>(Pages)

public var pageId: String by Pages.pageId
public var project: ProjectEntity by ProjectEntity referencedOn Pages.project
public var version: DocVersionEntity by DocVersionEntity referencedOn Pages.version
public var content: String by Pages.content
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/kotlin/banner/BannerMaker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class BannerMaker {
}
}

public fun BufferedImage.deepCopy(): BufferedImage {
private fun BufferedImage.deepCopy(): BufferedImage {
val cm = colorModel
val isAlphaPremultiplied = cm.isAlphaPremultiplied
val raster = copyData(null)
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/kotlin/website/pages/Common.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public fun FlowContent.backgroundBlob(properties: List<String>) {
div {
classes = setOf(
"absolute -z-10",
"bg-blur-effect",
"blob-background",
"bg-cover",
"pointer-events-none",
).plus(properties)
Expand Down
14 changes: 2 additions & 12 deletions backend/src/main/kotlin/website/pages/docs/DocsPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ import dev.triumphteam.backend.website.respondHtmlCached
import dev.triumphteam.website.project.Navigation
import dev.triumphteam.website.project.PageSummary
import dev.triumphteam.website.project.SummaryEntry
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.http.content.TextContent
import io.ktor.http.withCharset
import io.ktor.server.application.call
import io.ktor.server.html.respondHtml
import io.ktor.server.request.uri
import io.ktor.server.response.respond
import io.ktor.server.response.respondRedirect
Expand All @@ -42,13 +38,11 @@ import kotlinx.html.classes
import kotlinx.html.div
import kotlinx.html.footer
import kotlinx.html.h1
import kotlinx.html.html
import kotlinx.html.id
import kotlinx.html.img
import kotlinx.html.li
import kotlinx.html.meta
import kotlinx.html.script
import kotlinx.html.stream.appendHTML
import kotlinx.html.style
import kotlinx.html.styleLink
import kotlinx.html.title
Expand All @@ -60,10 +54,6 @@ import java.time.LocalDate
import kotlin.time.Duration.Companion.minutes
import kotlin.time.toJavaDuration

private val pageCache: Cache<String, TextContent> = Caffeine.newBuilder()
.expireAfterWrite(10.minutes.toJavaDuration())
.build()

private val projectCache: Cache<String, ProjectData> = Caffeine.newBuilder()
.expireAfterWrite(5.minutes.toJavaDuration())
.build()
Expand Down Expand Up @@ -424,12 +414,12 @@ private fun getProject(project: String): ProjectData? {

val versions = DocVersionEntity.find { DocVersions.project eq projectEntity.id }.map { entity ->
Version(
reference = entity.id.value,
reference = entity.reference,
navigation = entity.navigation,
pages = PageEntity.find { (Pages.project eq projectEntity.id) and (Pages.version eq entity.id) }
.map { pageEntity ->
Page(
id = pageEntity.id.value,
id = pageEntity.pageId,
content = pageEntity.content,
summary = pageEntity.summary,
title = pageEntity.title,
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/kotlin/website/pages/home/HomePage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public fun Routing.homeRoutes(developmentMode: Boolean) {
name = project.name,
icon = createIconPath(project.id.value),
color = project.color,
version = version.id.value,
version = version.reference,
)
}
}
Expand Down
5 changes: 5 additions & 0 deletions backend/src/main/resources/static/css/home_style.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@
background-color: #3C3E41;
border-radius: 25px;
}

.blob-background {
background: rgb(155, 85, 186);
background: radial-gradient(circle, rgba(155, 85, 186, 0.2) 0%, rgba(181, 50, 160, 0) 50%);
}
Binary file not shown.

0 comments on commit dda7f23

Please sign in to comment.