Skip to content

Instantly share code, notes, and snippets.

@mayankchoubey
Created May 22, 2024 00:25
Show Gist options
  • Save mayankchoubey/1b126be73400fbb70a47c6c569e2e9fa to your computer and use it in GitHub Desktop.
Save mayankchoubey/1b126be73400fbb70a47c6c569e2e9fa to your computer and use it in GitHub Desktop.
Quarkus - PG read use case
quarkus.datasource.db-kind=postgresql
quarkus.datasource.username=${dbUser}
quarkus.datasource.password=${dbUserPass}
quarkus.datasource.reactive.url=postgresql://localhost:5432/${dbName}
quarkus.datasource.reactive.max-size=10
quarkus.http.port=3000
package com.example;
public class NoUserResponse {
}
package com.example;
import java.time.LocalDateTime;
import io.vertx.mutiny.sqlclient.Row;
public record User(
String email,
String first,
String last,
String city,
String county,
int age) {
public static User of(String email, String first, String last, String city, String county, int age) {
return new User(email, first, last, city, county, age);
}
public static User from(Row row) {
return new User(
row.getString("email"),
row.getString("first"),
row.getString("last"),
row.getString("city"),
row.getString("county"),
row.getInteger("age"));
}
}
package com.example;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import io.vertx.mutiny.pgclient.PgPool;
import io.vertx.mutiny.sqlclient.Row;
import io.vertx.mutiny.sqlclient.RowSet;
import io.vertx.mutiny.sqlclient.Tuple;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.StreamSupport;
@ApplicationScoped
public class UserRepository {
@Inject
private PgPool client;
public UserRepository(PgPool _client) {
this.client = _client;
}
public Uni<User> query(String userEmail) {
return this.client
.preparedQuery("SELECT * from USERS WHERE email = $1")
.execute(Tuple.of(userEmail))
.onItem().transform(RowSet::iterator)
.onItem().transform(iterator -> iterator.hasNext() ? User.from(iterator.next()) : null);
}
}
package com.example;
public class UserRequest {
public String userEmail;
}
package com.example;
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.validation.Valid;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.net.URI;
import java.util.UUID;
import java.util.logging.Logger;
import static jakarta.ws.rs.core.Response.*;
import io.vertx.mutiny.pgclient.PgPool;
import com.example.UserRequest;
import com.example.UserResponse;
import com.example.UserResponseError;
import java.time.LocalDateTime;
import com.aventrix.jnanoid.jnanoid.*;
@Path("/")
@RequestScoped
public class UserResource {
private final static Logger LOGGER = Logger.getLogger(UserResource.class.getName());
@Inject
PgPool client;
@Inject
UserRepository userRepository;
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Uni<Response> handleRequest(@Valid UserRequest userRequest) {
if(userRequest.userEmail == null) {
return Uni.createFrom()
.item(
Response.status(400)
.type(MediaType.APPLICATION_JSON)
.entity(new UserResponseError("Parameter 'userEmail' is missing"))
.build());
}
Uni<User> userRecordData = this.userRepository.query(userRequest.userEmail);
return userRecordData.map(userRecord -> {
if(userRecord.email() == null) {
return Response.status(500)
.type(MediaType.APPLICATION_JSON)
.entity(new UserResponseError("Record not found"))
.build();
}
UserResponse resp = new UserResponse(userRecord.email(),
userRecord.first(),
userRecord.last(),
userRecord.city(),
userRecord.county(),
userRecord.age());
return Response.ok()
.type(MediaType.APPLICATION_JSON)
.entity(resp)
.build();
});
}
}
package com.example;
public class UserResponse {
public String email;
public String first;
public String last;
public String city;
public String county;
public int age;
public UserResponse(String email, String first, String last, String city, String county, int age) {
this.email = email;
this.first = first;
this.last = last;
this.city = city;
this.county = county;
this.age = age;
}
}
package com.example;
public class UserResponseError {
private String errMsg;
public UserResponseError() {
}
public UserResponseError(String errMsg) {
this.errMsg = errMsg;
}
public String getErrMsg() {
return this.errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment