tagging, rework locations of classes

master
Josha von Gizycki 5 days ago
parent 81f3972b55
commit 6235e5ca60

@ -1,18 +0,0 @@
package wanijo.wanijo2.domain
import org.springframework.data.annotation.Id
import org.springframework.data.relational.core.mapping.Table
import java.time.ZonedDateTime
@Table("T_DOCUMENT")
data class Document(
@Id
val id: Long = 0,
val name: String,
val description: String = "",
val updatedAt: ZonedDateTime = ZonedDateTime.now(),
val createdAt: ZonedDateTime = ZonedDateTime.now(),
val labelFields: Set<LabelField> = emptySet(),
val dateFields: Set<DateField> = emptySet(),
val tags: Set<Tag> = emptySet()
)

@ -1,27 +0,0 @@
package wanijo.wanijo2.domain
import org.springframework.data.jdbc.repository.query.Query
import org.springframework.data.repository.Repository
interface DocumentDao: Repository<Document, Int> {
fun findAll(): List<Document>
fun findById(id: Long): Document?
fun findByName(name: String): List<Document>
fun save(doc: Document)
@Query(
"""
SELECT doc.*
FROM t_document doc
JOIN t_tag tag
ON doc.id = tag.t_document
WHERE LOWER(tag.name) LIKE '%' || LOWER(:tagName) || '%'
"""
)
fun findByTagName(tagName: String): List<Document>
}

@ -1,13 +0,0 @@
package wanijo.wanijo2.domain
import org.springframework.data.annotation.Id
import org.springframework.data.relational.core.mapping.Table
import java.time.ZonedDateTime
@Table("T_TAG")
data class Tag(
@Id
val id: Long = 0,
val name: String,
val createdAt: ZonedDateTime = ZonedDateTime.now()
)

@ -0,0 +1,45 @@
package wanijo.wanijo2.domain
import org.springframework.data.annotation.Id
import org.springframework.data.annotation.Transient
import org.springframework.data.jdbc.repository.query.Query
import org.springframework.data.relational.core.mapping.Table
import org.springframework.data.repository.Repository
import java.time.ZonedDateTime
@Table("T_DOCUMENT")
data class Document(
@Id
val id: Long = 0,
val name: String,
val description: String = "",
val updatedAt: ZonedDateTime = ZonedDateTime.now(),
val createdAt: ZonedDateTime = ZonedDateTime.now(),
val labelFields: Set<LabelField> = emptySet(),
val dateFields: Set<DateField> = emptySet(),
) {
@Transient
val tags: Set<Tag> = emptySet()
}
interface DocumentDao: Repository<Document, Int> {
fun findAll(): List<Document>
fun findById(id: Long): Document?
fun findByName(name: String): List<Document>
fun save(doc: Document): Document
@Query(
"""
SELECT doc.*
FROM t_document doc
JOIN t_document_tagging tagging
ON tagging.document_id = doc.id
JOIN t_tag tag
ON tagging.tag_id = tag.id
WHERE LOWER(tag.name) LIKE '%' || LOWER(:tagName) || '%'
"""
)
fun findByTagName(tagName: String): List<Document>
}

@ -7,7 +7,7 @@ import java.time.ZonedDateTime
@Table("T_LABEL_FIELD")
data class LabelField(
@Id
val id: Long,
val id: Long = 0,
val order: Int = 0,
val name: String,
val value: String = "",
@ -18,7 +18,7 @@ data class LabelField(
@Table("T_DATE_FIELD")
data class DateField(
@Id
val id: Long,
val id: Long = 0,
val order: Int = 0,
val name: String,
val value: ZonedDateTime,

@ -0,0 +1,32 @@
package wanijo.wanijo2.domain
import org.springframework.data.annotation.Id
import org.springframework.data.jdbc.core.mapping.AggregateReference
import org.springframework.data.relational.core.mapping.Table
import org.springframework.data.repository.Repository
import java.time.ZonedDateTime
@Table("T_TAG")
data class Tag(
@Id
val id: Long = 0,
val name: String,
val createdAt: ZonedDateTime = ZonedDateTime.now()
)
@Table("T_DOCUMENT_TAGGING")
data class DocumentTagging(
@Id
val id: Long = 0,
val tagId: AggregateReference<Tag, Long>,
val documentId: AggregateReference<Document, Long>
)
interface DocumentTaggingDao : Repository<DocumentTagging, Long> {
fun save(tagging: DocumentTagging)
}
interface TagDao: Repository<Tag, Long> {
fun save(tag: Tag): Tag
fun findByName(name: String): Tag?
}

@ -23,7 +23,13 @@ CREATE TABLE t_date_field
CREATE TABLE t_tag
(
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name VARCHAR NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
t_document INT REFERENCES t_document (id) NOT NULL
name VARCHAR NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()
);
CREATE TABLE t_document_tagging
(
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
tag_id INT references t_tag,
document_id INT references t_document
);

@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertNotNull
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.data.jdbc.core.mapping.AggregateReference
import org.springframework.test.annotation.DirtiesContext
import kotlin.test.assertEquals
@ -14,6 +15,12 @@ class DocumentDaoTest {
@Autowired
lateinit var adapter: DocumentDao
@Autowired
lateinit var tagDao: TagDao
@Autowired
lateinit var documentTaggingDao: DocumentTaggingDao
@Test
fun insertWorks() {
assertEquals(0, adapter.findAll().count())
@ -60,38 +67,38 @@ class DocumentDaoTest {
@Test
fun findsDocumentsWithTag() {
adapter.save(
val tag1 = tagDao.save(Tag(name = "tAg1"))
val tag2 = tagDao.save(Tag(name = "taG2"))
val doc1 = adapter.save(
Document(
name = "Dok1",
tags = setOf(Tag(name = "tAg1"))
)
)
adapter.save(
val doc2 = adapter.save(
Document(
name = "Dok2",
tags = setOf(Tag(name = "taG2"))
)
)
val docs = adapter.findByTagName("tag1")
assertEquals(1, docs.size)
assertEquals("Dok1", docs[0].name)
}
documentTaggingDao.save(
DocumentTagging(
tagId = AggregateReference.to(tag1.id),
documentId = AggregateReference.to(doc1.id)
)
)
@Test
fun tagsAreSaved() {
adapter.save(
Document(
name = "dok1",
tags = setOf(Tag(name = "tag1"), Tag(name = "tag2"))
documentTaggingDao.save(
DocumentTagging(
tagId = AggregateReference.to(tag2.id),
documentId = AggregateReference.to(doc2.id)
)
)
val doc = adapter.findAll()[0]
assertEquals(2, doc.tags.size)
assertEquals("tag1", doc.tags.first().name)
assertEquals("tag2", doc.tags.last().name)
val docs = adapter.findByTagName("tag1")
assertEquals(1, docs.size)
assertEquals("Dok1", docs[0].name)
}
}

Loading…
Cancel
Save