-
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.
- Loading branch information
Showing
10 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...in-server/src/main/kotlin/com/sphereon/oid/fed/server/admin/controllers/CritController.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,41 @@ | ||
package com.sphereon.oid.fed.server.admin.controllers | ||
|
||
import com.sphereon.oid.fed.openapi.models.CreateCritDTO | ||
import com.sphereon.oid.fed.persistence.models.Crit | ||
import com.sphereon.oid.fed.services.CritService | ||
import org.springframework.web.bind.annotation.DeleteMapping | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
@RequestMapping("/accounts/{accountUsername}/crits") | ||
class CritController { | ||
private val critService = CritService() | ||
|
||
@PostMapping | ||
fun createCrit( | ||
@PathVariable accountUsername: String, | ||
@RequestBody body: CreateCritDTO | ||
): Crit { | ||
return critService.create(accountUsername, body.claim) | ||
} | ||
|
||
@GetMapping | ||
fun getCrits( | ||
@PathVariable accountUsername: String | ||
): Array<Crit> { | ||
return critService.findByAccountUsername(accountUsername) | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
fun deleteCrit( | ||
@PathVariable accountUsername: String, | ||
@PathVariable id: Int | ||
): Crit { | ||
return critService.delete(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
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
10 changes: 10 additions & 0 deletions
10
modules/persistence/src/commonMain/sqldelight/com/sphereon/oid/fed/persistence/models/7.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,10 @@ | ||
CREATE TABLE Crit ( | ||
id SERIAL PRIMARY KEY, | ||
account_id INT NOT NULL, | ||
claim TEXT NOT NULL, | ||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
deleted_at TIMESTAMP, | ||
CONSTRAINT FK_ParentCrit FOREIGN KEY (account_id) REFERENCES Account (id) | ||
); | ||
|
||
CREATE INDEX crit_account_id_index ON Crit (account_id); |
17 changes: 17 additions & 0 deletions
17
...les/persistence/src/commonMain/sqldelight/com/sphereon/oid/fed/persistence/models/Crit.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,17 @@ | ||
findByAccountId: | ||
SELECT * FROM Crit WHERE account_id = ? AND deleted_at IS NULL; | ||
|
||
findByAccountIdAndClaim: | ||
SELECT * FROM Crit WHERE account_id = ? AND claim = ? AND deleted_at IS NULL; | ||
|
||
deleteByAccountIdAndId: | ||
UPDATE Crit SET deleted_at = CURRENT_TIMESTAMP WHERE account_id = ? AND id = ? AND deleted_at IS NULL RETURNING *; | ||
|
||
deleteById: | ||
UPDATE Crit SET deleted_at = CURRENT_TIMESTAMP WHERE id = ? RETURNING *; | ||
|
||
create: | ||
INSERT INTO Crit (account_id, claim) VALUES (?, ?) RETURNING *; | ||
|
||
findByAccountIdAndId: | ||
SELECT * FROM Crit WHERE account_id = ? AND 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
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
41 changes: 41 additions & 0 deletions
41
modules/services/src/commonMain/kotlin/com/sphereon/oid/fed/services/CritService.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,41 @@ | ||
package com.sphereon.oid.fed.services | ||
|
||
import com.sphereon.oid.fed.persistence.Persistence | ||
import com.sphereon.oid.fed.persistence.models.Crit | ||
|
||
class CritService { | ||
|
||
fun create(accountUsername: String, claim: String): Crit { | ||
val account = Persistence.accountQueries.findByUsername(accountUsername).executeAsOneOrNull() | ||
?: throw IllegalArgumentException(Constants.ACCOUNT_NOT_FOUND) | ||
|
||
val critAlreadyExists = | ||
Persistence.critQueries.findByAccountIdAndClaim(account.id, claim).executeAsOneOrNull() | ||
|
||
if (critAlreadyExists != null) { | ||
throw IllegalArgumentException(Constants.CRIT_ALREADY_EXISTS) | ||
} | ||
|
||
return Persistence.critQueries.create(account.id, claim).executeAsOneOrNull() | ||
?: throw IllegalStateException(Constants.FAILED_TO_CREATE_CRIT) | ||
} | ||
|
||
fun delete(accountUsername: String, id: Int): Crit { | ||
val account = Persistence.accountQueries.findByUsername(accountUsername).executeAsOneOrNull() | ||
?: throw IllegalArgumentException(Constants.ACCOUNT_NOT_FOUND) | ||
|
||
return Persistence.critQueries.deleteByAccountIdAndId(account.id, id).executeAsOneOrNull() | ||
?: throw IllegalStateException(Constants.FAILED_TO_DELETE_CRIT) | ||
} | ||
|
||
fun findByAccountId(accountId: Int): Array<Crit> { | ||
return Persistence.critQueries.findByAccountId(accountId).executeAsList().toTypedArray() | ||
} | ||
|
||
fun findByAccountUsername(accountUsername: String): Array<Crit> { | ||
val account = Persistence.accountQueries.findByUsername(accountUsername).executeAsOneOrNull() | ||
?: throw IllegalArgumentException(Constants.ACCOUNT_NOT_FOUND) | ||
|
||
return findByAccountId(account.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