-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Unifying tags ### What's done: * Added tag table * Added lnk_vulnerability_tag * Added general Tag entity - this entity should be linked with another taggable entity * Added TagRepository and TagService * Supported Tag entity in a field of Vulnerability entity - created LnkVulnerabilityTag * Dropped tags column in vulnerability table ### TODO: * Implement filtering by tag and double-test it
- Loading branch information
1 parent
87ff34e
commit bba4b66
Showing
15 changed files
with
326 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<databaseChangeLog | ||
xmlns="http://www.liquibase.org/xml/ns/dbchangelog" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog | ||
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd"> | ||
|
||
<changeSet id="lnk-vulnerability-tag-1" author="sanyavertolet" context="dev or prod"> | ||
<createTable tableName="lnk_vulnerability_tag"> | ||
<column name="id" type="bigint" autoIncrement="true"> | ||
<constraints primaryKey="true" nullable="false"/> | ||
</column> | ||
<column name="vulnerability_id" type="bigint"> | ||
<constraints foreignKeyName="fk_vulnerability_tag_vulnerability" references="vulnerability(id)" nullable="false" deleteCascade="true"/> | ||
</column> | ||
<column name="tag_id" type="bigint"> | ||
<constraints foreignKeyName="fk_lnk_vulnerability_tag_tag" references="tag(id)" nullable="false" deleteCascade="true"/> | ||
</column> | ||
</createTable> | ||
|
||
<addUniqueConstraint tableName="lnk_vulnerability_tag" columnNames="tag_id, vulnerability_id"/> | ||
</changeSet> | ||
|
||
</databaseChangeLog> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<databaseChangeLog | ||
xmlns="http://www.liquibase.org/xml/ns/dbchangelog" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog | ||
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd"> | ||
|
||
<changeSet id="tag-1" author="sanyavertolet" context="dev or prod"> | ||
<createTable tableName="tag"> | ||
<column name="id" type="bigint" autoIncrement="true"> | ||
<constraints primaryKey="true" nullable="false"/> | ||
</column> | ||
<column name="name" type="varchar(16)"> | ||
<constraints nullable="false"/> | ||
</column> | ||
</createTable> | ||
</changeSet> | ||
|
||
</databaseChangeLog> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
save-backend/src/main/kotlin/com/saveourtool/save/backend/repository/TagRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.saveourtool.save.backend.repository | ||
|
||
import com.saveourtool.save.entities.Tag | ||
import com.saveourtool.save.spring.repository.BaseEntityRepository | ||
import org.springframework.stereotype.Repository | ||
|
||
/** | ||
* [BaseEntityRepository] for [Tag]s. | ||
*/ | ||
@Repository | ||
interface TagRepository : BaseEntityRepository<Tag> { | ||
/** | ||
* Find [Tag] by its [Tag.name] | ||
* | ||
* @param name tag name | ||
* @return [Tag] if found, null otherwise | ||
*/ | ||
fun findByName(name: String): Tag? | ||
|
||
/** | ||
* Find [Tag]s by their [Tag.name] | ||
* | ||
* @param tagNames [Set] of [Tag.name] | ||
* @return [Set] of [Tag] | ||
*/ | ||
fun findByNameIn(tagNames: Set<String>): Set<Tag> | ||
} |
36 changes: 36 additions & 0 deletions
36
...in/com/saveourtool/save/backend/repository/vulnerability/LnkVulnerabilityTagRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.saveourtool.save.backend.repository.vulnerability | ||
|
||
import com.saveourtool.save.entities.vulnerabilities.LnkVulnerabilityTag | ||
import com.saveourtool.save.spring.repository.BaseEntityRepository | ||
import org.springframework.stereotype.Repository | ||
|
||
/** | ||
* [BaseEntityRepository] for [LnkVulnerabilityTag] | ||
*/ | ||
@Repository | ||
interface LnkVulnerabilityTagRepository : BaseEntityRepository<LnkVulnerabilityTag> { | ||
/** | ||
* @param tagName tag | ||
* @return list of [LnkVulnerabilityTag] links to vulnerability | ||
*/ | ||
fun findByTagName(tagName: String): List<LnkVulnerabilityTag> | ||
|
||
/** | ||
* @param tagNames [Set] of tags | ||
* @return list of [LnkVulnerabilityTag] links to vulnerability | ||
*/ | ||
fun findByTagNameIn(tagNames: Set<String>): List<LnkVulnerabilityTag> | ||
|
||
/** | ||
* @param vulnerabilityId id of vulnerability | ||
* @return list of [LnkVulnerabilityTag] links to vulnerability | ||
*/ | ||
fun findByVulnerabilityId(vulnerabilityId: Long): List<LnkVulnerabilityTag> | ||
|
||
/** | ||
* @param vulnerabilityId id of vulnerability | ||
* @param tagName tag | ||
* @return [LnkVulnerabilityTag] | ||
*/ | ||
fun findByVulnerabilityIdAndTagName(vulnerabilityId: Long, tagName: String): LnkVulnerabilityTag? | ||
} |
59 changes: 59 additions & 0 deletions
59
save-backend/src/main/kotlin/com/saveourtool/save/backend/service/TagService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.saveourtool.save.backend.service | ||
|
||
import com.saveourtool.save.backend.repository.TagRepository | ||
import com.saveourtool.save.backend.repository.vulnerability.LnkVulnerabilityTagRepository | ||
import com.saveourtool.save.backend.repository.vulnerability.VulnerabilityRepository | ||
import com.saveourtool.save.entities.Tag | ||
import com.saveourtool.save.entities.vulnerabilities.LnkVulnerabilityTag | ||
import com.saveourtool.save.entities.vulnerabilities.Vulnerability | ||
import com.saveourtool.save.utils.orNotFound | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
/** | ||
* [Service] for [Tag] entity | ||
* | ||
* @property tagRepository | ||
*/ | ||
@Service | ||
class TagService( | ||
private val tagRepository: TagRepository, | ||
private val vulnerabilityRepository: VulnerabilityRepository, | ||
private val lnkVulnerabilityTagRepository: LnkVulnerabilityTagRepository, | ||
) { | ||
/** | ||
* @param vulnerabilityName [Vulnerability.name] | ||
* @param tagName tag to add | ||
* @return new [LnkVulnerabilityTag] | ||
*/ | ||
@Transactional | ||
fun addVulnerabilityTag(vulnerabilityName: String, tagName: String): LnkVulnerabilityTag { | ||
val vulnerability = vulnerabilityRepository.findByName(vulnerabilityName).orNotFound { | ||
"Could not find vulnerability $vulnerabilityName" | ||
} | ||
val tag = tagRepository.findByName(tagName) ?: tagRepository.save(Tag(tagName)) | ||
|
||
return lnkVulnerabilityTagRepository.save( | ||
LnkVulnerabilityTag(vulnerability, tag) | ||
) | ||
} | ||
|
||
/** | ||
* @param vulnerabilityName [Vulnerability.name] | ||
* @param tagName tag to delete | ||
* @return updated [Vulnerability] | ||
*/ | ||
@Transactional | ||
fun deleteVulnerabilityTag(vulnerabilityName: String, tagName: String) { | ||
val vulnerability = vulnerabilityRepository.findByName(vulnerabilityName).orNotFound { | ||
"Could not find vulnerability $vulnerabilityName" | ||
} | ||
|
||
val link = lnkVulnerabilityTagRepository.findByVulnerabilityIdAndTagName( | ||
vulnerability.requiredId(), | ||
tagName | ||
).orNotFound { "Tag '$tagName' is not linked with vulnerability $vulnerabilityName." } | ||
|
||
lnkVulnerabilityTagRepository.delete(link) | ||
} | ||
} |
Oops, something went wrong.