add Quarkus configuration and implement Student entity with repository and resource
This commit is contained in:
parent
a455575493
commit
c54878a3cb
29
pom.xml
29
pom.xml
@ -21,8 +21,8 @@
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${quarkus.platform.group-id}</groupId>
|
||||
<artifactId>${quarkus.platform.artifact-id}</artifactId>
|
||||
<groupId>io.quarkus.platform</groupId>
|
||||
<artifactId>quarkus-bom</artifactId>
|
||||
<version>${quarkus.platform.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
@ -30,6 +30,19 @@
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>central</id>
|
||||
<name>Maven Central</name>
|
||||
<url>https://repo.maven.apache.org/maven2</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>quarkus</id>
|
||||
<name>Quarkus</name>
|
||||
<url>https://maven.quarkus.io</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
@ -39,6 +52,10 @@
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-arc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-jdbc-postgresql</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-junit5</artifactId>
|
||||
@ -55,12 +72,16 @@
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-hibernate-orm</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>${quarkus.platform.group-id}</groupId>
|
||||
<groupId>io.quarkus.platform</groupId>
|
||||
<artifactId>quarkus-maven-plugin</artifactId>
|
||||
<version>${quarkus.platform.version}</version>
|
||||
<extensions>true</extensions>
|
||||
@ -135,4 +156,4 @@
|
||||
</properties>
|
||||
</profile>
|
||||
</profiles>
|
||||
</project>
|
||||
</project>
|
||||
|
||||
22
src/main/java/org/acme/StudentEntity.java
Normal file
22
src/main/java/org/acme/StudentEntity.java
Normal file
@ -0,0 +1,22 @@
|
||||
package org.acme;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@NoArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@EqualsAndHashCode
|
||||
@Builder
|
||||
@Entity
|
||||
public class StudentEntity {
|
||||
@Id
|
||||
private UUID id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
}
|
||||
41
src/main/java/org/acme/StudentRepository.java
Normal file
41
src/main/java/org/acme/StudentRepository.java
Normal file
@ -0,0 +1,41 @@
|
||||
package org.acme;
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.transaction.Transactional;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.UUID;
|
||||
|
||||
@ApplicationScoped
|
||||
public class StudentRepository {
|
||||
@Inject
|
||||
EntityManager entityManager;
|
||||
|
||||
@Transactional
|
||||
public UUID create (final StudentEntity student){
|
||||
entityManager.persist(student);
|
||||
return student.getId();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void update( final StudentEntity student){
|
||||
entityManager.merge(student);
|
||||
}
|
||||
|
||||
public Collection<StudentEntity> findAll(){
|
||||
return entityManager.createQuery("from StudentEntity s", StudentEntity.class).getResultList();
|
||||
}
|
||||
|
||||
public StudentEntity findById(final UUID id){
|
||||
return entityManager.find(StudentEntity.class, id);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void delete (final StudentEntity student){
|
||||
entityManager.remove(student);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -1,46 +1,68 @@
|
||||
package org.acme;
|
||||
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.POST;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.Optional;
|
||||
|
||||
@Path("api/v1/students")
|
||||
@Path("api/v1/students/")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Slf4j
|
||||
public class StudentResource {
|
||||
|
||||
private final Map<String, Student> students = new HashMap<>(Map.of(
|
||||
"0854bc12-842a-47c1-b3fe-10d08949fcc8", Student.builder().id("0854bc12-842a-47c1-b3fe-10d08949fcc8").firstName("John").lastName("Doe").build()));
|
||||
@Inject
|
||||
StudentService studentService;
|
||||
|
||||
@GET
|
||||
public Response find() {
|
||||
return Response.ok(students.values()).build();
|
||||
log.info(studentService.toString());
|
||||
return Response.ok(studentService.findAll()).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("{id}")
|
||||
public Response findById(String id) {
|
||||
final Student student = students.get(id);
|
||||
if (student != null) {
|
||||
return Response.ok(student).build();
|
||||
} else {
|
||||
@Path("{uuid}")
|
||||
public Response findById(@PathParam("uuid") final String id){
|
||||
final Optional<Student> student = studentService.findById(id);
|
||||
|
||||
if (student.isEmpty()) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
|
||||
return Response.ok(student.get()).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
public Response create(final Student student) {
|
||||
try {
|
||||
student.setId(UUID.randomUUID().toString());
|
||||
students.put(student.getId(), student);
|
||||
return Response.created(new URI("/api/v1/students/" + student.getId())).build();
|
||||
public Response create(final Student student){
|
||||
if (student == null) {
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity("Student cannot be null").build();
|
||||
}
|
||||
catch (Exception e) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
|
||||
try{
|
||||
final String id = studentService.create(student);
|
||||
return Response.created(new URI("/api/v1/students/" + id)).build();
|
||||
} catch (final Exception e) {
|
||||
log.error("Error creating student", e);
|
||||
return Response.serverError().entity(e.getMessage()).build();
|
||||
}
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("{uuid}")
|
||||
public Response update(@PathParam("uuid") final String id, final Student student) {
|
||||
if (student == null) {
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity("Student cannot be null").build();
|
||||
}
|
||||
student.setId(id);
|
||||
studentService.update(student);
|
||||
return Response.ok().build();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("{uuid}")
|
||||
public Response delete(@PathParam("uuid") final String id){
|
||||
studentService.delete(id);
|
||||
return Response.noContent().build();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
# Quarkus datasource configuration for PostgreSQL
|
||||
quarkus.datasource.db-kind=postgresql
|
||||
quarkus.datasource.username=schul-user-jonas
|
||||
quarkus.datasource.password=Qsczjm123
|
||||
quarkus.datasource.jdbc.url=jdbc:postgresql://ionas999.at:9234/schul-user-jonas
|
||||
quarkus.hibernate-orm.schema-management.strategy=update
|
||||
quarkus.datasource.jdbc.max-size=8
|
||||
quarkus.datasource.jdbc.min-size=2
|
||||
# Optional: show SQL statements in logs
|
||||
quarkus.hibernate-orm.log.sql=true
|
||||
# Optional: configure default schema if needed
|
||||
# quarkus.datasource.schema=public
|
||||
|
||||
# Example custom property used in StudentService
|
||||
service2.apiKey=your-api-key-here
|
||||
Loading…
Reference in New Issue
Block a user