diff --git a/src/main/kotlin/fr/dcproject/repository/Citizen.kt b/src/main/kotlin/fr/dcproject/repository/Citizen.kt index 778c1be..3e858b7 100644 --- a/src/main/kotlin/fr/dcproject/repository/Citizen.kt +++ b/src/main/kotlin/fr/dcproject/repository/Citizen.kt @@ -24,6 +24,12 @@ class Citizen(override var requester: Requester) : RepositoryI { .selectOne("user_id" to user.id) } + fun findByUsername(unsername: String): CitizenEntity? { + return requester + .getFunction("find_citizen_by_username") + .selectOne("username" to unsername) + } + fun find( page: Int = 1, limit: Int = 50, diff --git a/src/main/resources/sql/functions/citizen/find_citizen_by_username.sql b/src/main/resources/sql/functions/citizen/find_citizen_by_username.sql new file mode 100644 index 0000000..0e54d2a --- /dev/null +++ b/src/main/resources/sql/functions/citizen/find_citizen_by_username.sql @@ -0,0 +1,18 @@ +create or replace function find_citizen_by_username(username text, out resource json) language plpgsql as +$$ +declare + _username alias for username; +begin + select to_json(t) into resource + from ( + select + z.*, + find_user_by_id(u.id) as "user" + from citizen as z + join "user" as u on z.user_id = u.id + where u.username = _username + ) as t; +end; +$$; + +-- drop function if exists find_citizen_by_username(text, out json); \ No newline at end of file