Refactoring Opinion Tests
upsert_opinion_choice
This commit is contained in:
@@ -8,7 +8,7 @@ import fr.postgresjson.entity.mutable.EntityDeletedAtImp
|
||||
import java.util.*
|
||||
|
||||
class OpinionChoice(
|
||||
id: UUID,
|
||||
id: UUID? = null,
|
||||
val name: String,
|
||||
val target: List<String>?
|
||||
) : OpinionChoiceRef(id),
|
||||
@@ -16,5 +16,5 @@ class OpinionChoice(
|
||||
EntityDeletedAt by EntityDeletedAtImp()
|
||||
|
||||
open class OpinionChoiceRef(
|
||||
id: UUID
|
||||
) : UuidEntity(id)
|
||||
id: UUID?
|
||||
) : UuidEntity(id ?: UUID.randomUUID())
|
||||
@@ -43,6 +43,12 @@ open class OpinionChoice(override val requester: Requester) : RepositoryI {
|
||||
.selectOne(
|
||||
"id" to id
|
||||
)
|
||||
|
||||
fun upsertOpinionChoice(opinionChoice: OpinionChoiceEntity): OpinionChoiceEntity = requester
|
||||
.getFunction("upsert_opinion_choice")
|
||||
.selectOne(
|
||||
"resource" to opinionChoice
|
||||
)!!
|
||||
}
|
||||
|
||||
open class Opinion<T : TargetRef>(requester: Requester) : OpinionChoice(requester) {
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
create or replace function upsert_opinion_choice(inout resource json)
|
||||
language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_id uuid = coalesce((resource->>'id')::uuid, uuid_generate_v4());
|
||||
begin
|
||||
insert into opinion_choice (id, name, target)
|
||||
select
|
||||
_id,
|
||||
name,
|
||||
target
|
||||
from json_populate_record(null::opinion_choice, resource)
|
||||
on conflict (name) do update set
|
||||
target = excluded.target;
|
||||
|
||||
select find_opinion_choice_by_id(_id) into resource;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists upsert_opinion_choice(json);
|
||||
Reference in New Issue
Block a user