-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
resteasy reactive in legumes #18
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,29 @@ | ||
package com.brunobat.rest; | ||
|
||
import com.brunobat.rest.data.LegumeItem; | ||
import com.brunobat.rest.model.Legume; | ||
import io.quarkus.hibernate.orm.panache.PanacheRepository; | ||
|
||
import io.quarkus.hibernate.reactive.panache.PanacheRepository; | ||
import io.quarkus.hibernate.reactive.panache.common.WithSession; | ||
import io.smallrye.mutiny.Uni; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import jakarta.persistence.EntityManager; | ||
|
||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
@ApplicationScoped | ||
public class LegumeRepository implements PanacheRepository<Legume> { | ||
|
||
@Inject | ||
EntityManager manager; | ||
|
||
public Stream<Legume> listLegumes(int pageIndex) { | ||
return find("SELECT h FROM Legume h").page(pageIndex, 16).stream(); | ||
@WithSession | ||
public Uni<List<Legume>> listLegumes(int pageIndex) { | ||
return find("SELECT h FROM Legume h").page(pageIndex, 16).list(); | ||
} | ||
|
||
public void remove(final LegumeItem legume) { | ||
manager.remove(legume); | ||
public void removeLegume(final String legumeId) { | ||
find("SELECT h FROM Legume h WHERE h.id=?1", legumeId).firstResult().onItem().transform(legume -> { | ||
delete(legume); | ||
return legume; | ||
}); | ||
} | ||
|
||
public Legume merge(final Legume legumeToAdd) { | ||
return manager.merge(legumeToAdd); | ||
public Uni<Legume> createLegume(final Legume legumeToAdd) { | ||
return persist(legumeToAdd); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,19 +6,19 @@ | |
import com.brunobat.rest.data.LegumeNew; | ||
import com.brunobat.rest.message.MessageSender; | ||
import com.brunobat.rest.model.Legume; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import io.quarkus.hibernate.reactive.panache.common.WithTransaction; | ||
import io.smallrye.mutiny.Uni; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import jakarta.transaction.Transactional; | ||
import jakarta.validation.Valid; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.util.Arrays.asList; | ||
import static jakarta.ws.rs.core.Response.Status.CREATED; | ||
import static jakarta.ws.rs.core.Response.Status.NOT_FOUND; | ||
import static jakarta.ws.rs.core.Response.Status.NO_CONTENT; | ||
|
@@ -33,68 +33,84 @@ public class LegumeResource implements LegumeApi { | |
@Inject | ||
MessageSender messageSender; | ||
|
||
@Transactional | ||
public Response provision() { | ||
@WithTransaction | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reactive code requires a different annotation. I got a warning message. |
||
public Uni<Response> provision() { | ||
final LegumeNew carrot = LegumeNew.builder() | ||
.name("Carrot") | ||
.description("Root vegetable, usually orange") | ||
.build(); | ||
.name("Carrot") | ||
.description("Root vegetable, usually orange") | ||
.build(); | ||
final LegumeNew zucchini = LegumeNew.builder() | ||
.name("Zucchini") | ||
.description("Summer squash") | ||
.build(); | ||
return Response.status(CREATED).entity(asList( | ||
addLegume(carrot), | ||
addLegume(zucchini))).build(); | ||
.name("Zucchini") | ||
.description("Summer squash") | ||
.build(); | ||
|
||
return Uni.combine().all().unis( | ||
addLegume(carrot), | ||
addLegume(zucchini)) | ||
.asTuple() | ||
.onItem().transform(tuple -> { | ||
List<LegumeItem> resultList = new ArrayList<>(); | ||
resultList.add(tuple.getItem1()); | ||
resultList.add(tuple.getItem2()); | ||
return Response.status(CREATED).entity(resultList).build(); | ||
}); | ||
} | ||
|
||
@Transactional | ||
public Response add(@Valid final LegumeNew legumeNew) { | ||
return Response.status(CREATED).entity(addLegume(legumeNew)).build(); | ||
@WithTransaction | ||
public Uni<Response> add(@Valid final LegumeNew legumeNew) { | ||
return addLegume(legumeNew) | ||
.onItem().transform(legumeItem -> Response | ||
.status(CREATED) | ||
.entity(legumeItem) | ||
.build()); | ||
} | ||
|
||
@Transactional | ||
public Response delete(@NotEmpty final String legumeId) { | ||
@WithTransaction | ||
public Uni<Response> delete(@NotEmpty final String legumeId) { | ||
return find(legumeId) | ||
.map(legume -> { | ||
repository.remove(legume); | ||
return Response.status(NO_CONTENT).build(); | ||
}) | ||
.orElse(Response.status(NOT_FOUND).build()); | ||
.onItem().transform(legumeOpt -> legumeOpt | ||
.map(legume -> { | ||
repository.removeLegume(legume.getId()); | ||
return Response.status(NO_CONTENT).build(); | ||
}) | ||
.orElse(Response.status(NOT_FOUND).build())); | ||
} | ||
|
||
public List<LegumeItem> list(int pageIndex) { | ||
public Uni<List<LegumeItem>> list(int pageIndex) { | ||
// log.info("someone asked for a list for index: " + pageIndex); | ||
return repository.listLegumes(pageIndex) | ||
.map(this::getLegumeItem) | ||
.collect(Collectors.toList()); | ||
.onItem().transform(legumes -> legumes.stream() | ||
.map(this::getLegumeItem) | ||
.collect(Collectors.toList())); | ||
} | ||
|
||
private Optional<LegumeItem> find(final String legumeId) { | ||
return repository.find("id", legumeId).stream() | ||
.map(this::getLegumeItem) | ||
.findFirst(); | ||
private Uni<Optional<LegumeItem>> find(final String legumeId) { | ||
return repository.find("id", legumeId).list() | ||
.onItem().transform(legumes -> legumes.stream() | ||
.map(this::getLegumeItem) | ||
.findFirst()); | ||
} | ||
|
||
private LegumeItem addLegume(final @Valid LegumeNew legumeNew) { | ||
private Uni<LegumeItem> addLegume(final LegumeNew legumeNew) { | ||
final Legume legumeToAdd = Legume.builder() | ||
.name(legumeNew.getName()) | ||
.description((legumeNew.getDescription())) | ||
.build(); | ||
|
||
final Legume addedLegume = repository.merge(legumeToAdd); | ||
final LegumeItem legumeItem = getLegumeItem(addedLegume); | ||
.name(legumeNew.getName()) | ||
.description((legumeNew.getDescription())) | ||
.build(); | ||
|
||
messageSender.send(legumeItem.getName()); | ||
final Uni<Legume> addedLegume = repository.createLegume(legumeToAdd); | ||
|
||
return legumeItem; | ||
return addedLegume.onItem().transform(legume -> { | ||
LegumeItem legumeItem = getLegumeItem(legume); | ||
messageSender.send(legumeItem.getName()); | ||
return legumeItem; | ||
}); | ||
} | ||
|
||
private LegumeItem getLegumeItem(final Legume addedLegume) { | ||
return LegumeItem.builder() | ||
.id(addedLegume.getId()) | ||
.name(addedLegume.getName()) | ||
.description(addedLegume.getDescription()) | ||
.build(); | ||
.id(addedLegume.getId()) | ||
.name(addedLegume.getName()) | ||
.description(addedLegume.getDescription()) | ||
.build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
## configure your datasource | ||
quarkus.datasource.db-kind=h2 | ||
quarkus.datasource.jdbc.url=jdbc:h2:mem:test;MODE=PostgreSQL;DB_CLOSE_DELAY=-1 | ||
quarkus.datasource.username: sa | ||
quarkus.datasource.password: | ||
quarkus.datasource.jdbc.max-size: 20 | ||
#quarkus.datasource.db-kind=h2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Commented out on purpose? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is no reactive driver support for H2 |
||
#quarkus.datasource.reactive.url=h2:mem:test;MODE=PostgreSQL;DB_CLOSE_DELAY=-1 | ||
#quarkus.datasource.username: sa | ||
#quarkus.datasource.password: | ||
#quarkus.datasource.jdbc.max-size: 20 | ||
# drop and create the database at startup (use `update` to only update the schema) | ||
#quarkus.hibernate-orm.dialect=org.hibernate.dialect.H2Dialect | ||
quarkus.hibernate-orm.database.generation=drop-and-create | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it have to be select?
Can't you just do "delete from x where x.id = ?!"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol, yeah.🤦🏻♂️