Use repository

This commit is contained in:
Andrea Cavalli 2021-11-26 01:18:55 +01:00
parent 0244372efb
commit 9c76626c70
1 changed files with 7 additions and 7 deletions

View File

@ -28,19 +28,19 @@ public class ChatResource {
@GET @GET
public Multi<Chat> listSessions() { public Multi<Chat> listSessions() {
return Chat.streamAll(); return chatRepository.streamAll();
} }
@GET @GET
@Path("/{id}") @Path("/{id}")
public Uni<Chat> get(@PathParam("id") String id) { public Uni<Chat> get(@PathParam("id") String id) {
return Chat.findById(id); return chatRepository.findById(id);
} }
@POST @POST
@Transactional @Transactional
public Uni<Response> create(Chat chat) { public Uni<Response> create(Chat chat) {
return chat.persist() return chatRepository.persist(chat)
// Return success // Return success
.replaceWith(() -> Response.created(URI.create("/api/" + chat.id)).build()); .replaceWith(() -> Response.created(URI.create("/api/" + chat.id)).build());
} }
@ -50,11 +50,11 @@ public class ChatResource {
@Transactional @Transactional
public Uni<Chat> update(@PathParam("id") String id, Chat chat) { public Uni<Chat> update(@PathParam("id") String id, Chat chat) {
// Find chat by id // Find chat by id
return Chat.<Chat>findById(id) return chatRepository.<Chat>findById(id)
.flatMap(entity -> { .flatMap(entity -> {
if (entity == null) { if (entity == null) {
// Persist the chat if not found // Persist the chat if not found
return chat.persist(); return chatRepository.persist(chat);
} else { } else {
// Update all fields // Update all fields
entity.name = chat.name; entity.name = chat.name;
@ -68,7 +68,7 @@ public class ChatResource {
@Path("/{id}") @Path("/{id}")
@Transactional @Transactional
public Uni<Void> delete(@PathParam("id") String id) { public Uni<Void> delete(@PathParam("id") String id) {
return Chat.findById(id) return chatRepository.findById(id)
.onItem().ifNull().failWith(NotFoundException::new) .onItem().ifNull().failWith(NotFoundException::new)
.flatMap(PanacheEntityBase::delete); .flatMap(PanacheEntityBase::delete);
} }
@ -82,6 +82,6 @@ public class ChatResource {
@GET @GET
@Path("/count") @Path("/count")
public Uni<Long> count() { public Uni<Long> count() {
return Chat.count(); return chatRepository.count();
} }
} }