-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement entity configuration metadata
- Loading branch information
Showing
12 changed files
with
159 additions
and
11 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
39 changes: 39 additions & 0 deletions
39
...in/com/sphereon/oid/fed/server/admin/controllers/EntityConfigurationMetadataController.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,39 @@ | ||
package com.sphereon.oid.fed.server.admin.controllers | ||
|
||
import com.sphereon.oid.fed.openapi.models.CreateMetadataDTO | ||
import com.sphereon.oid.fed.persistence.models.EntityConfigurationMetadata | ||
import com.sphereon.oid.fed.services.EntityConfigurationMetadataService | ||
import org.springframework.web.bind.annotation.* | ||
|
||
@RestController | ||
@RequestMapping("/accounts/{accountUsername}/metadata") | ||
class EntityConfigurationMetadataController { | ||
private val entityConfigurationMetadataService = EntityConfigurationMetadataService() | ||
|
||
@GetMapping | ||
fun get( | ||
@PathVariable accountUsername: String | ||
): Array<EntityConfigurationMetadata> { | ||
return entityConfigurationMetadataService.findByAccountUsername(accountUsername) | ||
} | ||
|
||
@PostMapping | ||
fun create( | ||
@PathVariable accountUsername: String, | ||
@RequestBody metadata: CreateMetadataDTO | ||
): EntityConfigurationMetadata { | ||
return entityConfigurationMetadataService.createEntityConfigurationMetadata( | ||
accountUsername, | ||
metadata.key, | ||
metadata.value | ||
) | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
fun delete( | ||
@PathVariable accountUsername: String, | ||
@PathVariable id: Int | ||
): EntityConfigurationMetadata { | ||
return entityConfigurationMetadataService.deleteEntityConfigurationMetadata(accountUsername, id) | ||
} | ||
} |
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
6 changes: 2 additions & 4 deletions
6
modules/persistence/src/commonMain/kotlin/com/sphereon/oid/fed/persistence/Persistence.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 |
---|---|---|
@@ -1,13 +1,11 @@ | ||
package com.sphereon.oid.fed.persistence | ||
|
||
import com.sphereon.oid.fed.persistence.models.AccountQueries | ||
import com.sphereon.oid.fed.persistence.models.EntityConfigurationStatementQueries | ||
import com.sphereon.oid.fed.persistence.models.KeyQueries | ||
import com.sphereon.oid.fed.persistence.models.SubordinateQueries | ||
import com.sphereon.oid.fed.persistence.models.* | ||
|
||
expect object Persistence { | ||
val entityConfigurationStatementQueries: EntityConfigurationStatementQueries | ||
val accountQueries: AccountQueries | ||
val keyQueries: KeyQueries | ||
val subordinateQueries: SubordinateQueries | ||
val entityConfigurationMetadataQueries: EntityConfigurationMetadataQueries | ||
} |
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
11 changes: 11 additions & 0 deletions
11
modules/persistence/src/commonMain/sqldelight/com/sphereon/oid/fed/persistence/models/5.sqm
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,11 @@ | ||
CREATE TABLE entityConfigurationMetadata ( | ||
id SERIAL PRIMARY KEY, | ||
account_id INT NOT NULL, | ||
key TEXT NOT NULL, | ||
value TEXT NOT NULL, | ||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
deleted_at TIMESTAMP, | ||
CONSTRAINT FK_ParentEntityConfigurationMetadata FOREIGN KEY (account_id) REFERENCES account (id) | ||
); | ||
|
||
CREATE INDEX entity_configuration_metadata_account_id_index ON subordinate (account_id); |
18 changes: 18 additions & 0 deletions
18
...monMain/sqldelight/com/sphereon/oid/fed/persistence/models/EntityConfigurationMetadata.sq
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,18 @@ | ||
create: | ||
INSERT INTO entityConfigurationMetadata ( | ||
account_id, | ||
key, | ||
value | ||
) VALUES (?, ?, ?) RETURNING *; | ||
|
||
delete: | ||
UPDATE entityConfigurationMetadata SET deleted_at = CURRENT_TIMESTAMP WHERE id = ? AND deleted_at IS NULL RETURNING *; | ||
|
||
findByAccountId: | ||
SELECT * FROM entityConfigurationMetadata WHERE account_id = ? AND deleted_at IS NULL; | ||
|
||
findByAccountIdAndKey: | ||
SELECT * FROM entityConfigurationMetadata WHERE account_id = ? AND key = ? AND deleted_at IS NULL; | ||
|
||
findById: | ||
SELECT * FROM entityConfigurationMetadata WHERE id = ? AND deleted_at IS NULL; |
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
53 changes: 53 additions & 0 deletions
53
...src/commonMain/kotlin/com/sphereon/oid/fed/services/EntityConfigurationMetadataService.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,53 @@ | ||
package com.sphereon.oid.fed.services | ||
|
||
import com.sphereon.oid.fed.persistence.Persistence | ||
import com.sphereon.oid.fed.persistence.models.EntityConfigurationMetadata | ||
import kotlinx.serialization.json.JsonObject | ||
|
||
class EntityConfigurationMetadataService { | ||
fun createEntityConfigurationMetadata( | ||
accountUsername: String, | ||
key: String, | ||
metadata: JsonObject | ||
): EntityConfigurationMetadata { | ||
val account = Persistence.accountQueries.findByUsername(accountUsername).executeAsOneOrNull() | ||
?: throw IllegalArgumentException("Account not found") | ||
|
||
val metadataAlreadyExists = | ||
Persistence.entityConfigurationMetadataQueries.findByAccountIdAndKey(account.id, key).executeAsOneOrNull() | ||
|
||
if (metadataAlreadyExists != null) { | ||
throw IllegalStateException("Entity configuration metadata already exists") | ||
} | ||
|
||
return Persistence.entityConfigurationMetadataQueries.create(account.id, key, metadata.toString()) | ||
.executeAsOneOrNull() | ||
?: throw IllegalStateException("Failed to create entity configuration metadata") | ||
} | ||
|
||
fun findByAccountId(accountId: Int): Array<EntityConfigurationMetadata> { | ||
return Persistence.entityConfigurationMetadataQueries.findByAccountId(accountId).executeAsList().toTypedArray() | ||
} | ||
|
||
fun findByAccountUsername(accountUsername: String): Array<EntityConfigurationMetadata> { | ||
val account = Persistence.accountQueries.findByUsername(accountUsername).executeAsOneOrNull() | ||
?: throw IllegalArgumentException("Account not found") | ||
return Persistence.entityConfigurationMetadataQueries.findByAccountId(account.id).executeAsList().toTypedArray() | ||
} | ||
|
||
fun deleteEntityConfigurationMetadata(accountUsername: String, id: Int): EntityConfigurationMetadata { | ||
val account = Persistence.accountQueries.findByUsername(accountUsername).executeAsOneOrNull() | ||
?: throw IllegalArgumentException("Account not found") | ||
|
||
val metadata = | ||
Persistence.entityConfigurationMetadataQueries.findById(id).executeAsOneOrNull() | ||
?: throw IllegalArgumentException("Entity configuration metadata not found") | ||
|
||
if (metadata.account_id != account.id) { | ||
throw IllegalArgumentException("Entity configuration metadata not found") | ||
} | ||
|
||
return Persistence.entityConfigurationMetadataQueries.delete(id).executeAsOneOrNull() | ||
?: throw IllegalArgumentException("Entity configuration metadata not found") | ||
} | ||
} |
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