diff --git a/src/main/kotlin/wanijo/wanijo2/domain/Document.kt b/src/main/kotlin/wanijo/wanijo2/domain/Document.kt deleted file mode 100644 index 690b603..0000000 --- a/src/main/kotlin/wanijo/wanijo2/domain/Document.kt +++ /dev/null @@ -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 = emptySet(), - val dateFields: Set = emptySet(), - val tags: Set = emptySet() -) diff --git a/src/main/kotlin/wanijo/wanijo2/domain/DocumentDao.kt b/src/main/kotlin/wanijo/wanijo2/domain/DocumentDao.kt deleted file mode 100644 index 55fdd58..0000000 --- a/src/main/kotlin/wanijo/wanijo2/domain/DocumentDao.kt +++ /dev/null @@ -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 { - - fun findAll(): List - - fun findById(id: Long): Document? - - fun findByName(name: String): List - - 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 - -} diff --git a/src/main/kotlin/wanijo/wanijo2/domain/Tag.kt b/src/main/kotlin/wanijo/wanijo2/domain/Tag.kt deleted file mode 100644 index dab34c3..0000000 --- a/src/main/kotlin/wanijo/wanijo2/domain/Tag.kt +++ /dev/null @@ -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() -) diff --git a/src/main/kotlin/wanijo/wanijo2/domain/document.kt b/src/main/kotlin/wanijo/wanijo2/domain/document.kt new file mode 100644 index 0000000..bf5d251 --- /dev/null +++ b/src/main/kotlin/wanijo/wanijo2/domain/document.kt @@ -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 = emptySet(), + val dateFields: Set = emptySet(), +) { + + @Transient + val tags: Set = emptySet() + +} + +interface DocumentDao: Repository { + fun findAll(): List + fun findById(id: Long): Document? + fun findByName(name: String): List + 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 +} diff --git a/src/main/kotlin/wanijo/wanijo2/domain/fields.kt b/src/main/kotlin/wanijo/wanijo2/domain/fields.kt index 8c286fe..e31e593 100644 --- a/src/main/kotlin/wanijo/wanijo2/domain/fields.kt +++ b/src/main/kotlin/wanijo/wanijo2/domain/fields.kt @@ -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, diff --git a/src/main/kotlin/wanijo/wanijo2/domain/tag.kt b/src/main/kotlin/wanijo/wanijo2/domain/tag.kt new file mode 100644 index 0000000..bbc3f12 --- /dev/null +++ b/src/main/kotlin/wanijo/wanijo2/domain/tag.kt @@ -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, + val documentId: AggregateReference +) + +interface DocumentTaggingDao : Repository { + fun save(tagging: DocumentTagging) +} + +interface TagDao: Repository { + fun save(tag: Tag): Tag + fun findByName(name: String): Tag? +} diff --git a/src/main/resources/db/migration/V202507181738__label_date_fields.sql b/src/main/resources/db/migration/V202507181738__label_date_fields.sql index 2be3d20..300dc09 100644 --- a/src/main/resources/db/migration/V202507181738__label_date_fields.sql +++ b/src/main/resources/db/migration/V202507181738__label_date_fields.sql @@ -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 ); diff --git a/src/test/kotlin/wanijo/wanijo2/domain/DocumentDaoTest.kt b/src/test/kotlin/wanijo/wanijo2/domain/DocumentDaoTest.kt index 7221202..31e4ada 100644 --- a/src/test/kotlin/wanijo/wanijo2/domain/DocumentDaoTest.kt +++ b/src/test/kotlin/wanijo/wanijo2/domain/DocumentDaoTest.kt @@ -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) } }