add Student service, mapper, and repository methods for CRUD operations
This commit is contained in:
parent
c54878a3cb
commit
c441e7563f
4
pom.xml
4
pom.xml
@ -56,6 +56,10 @@
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-jdbc-postgresql</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-smallrye-openapi</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-junit5</artifactId>
|
||||
|
||||
31
src/main/java/org/acme/StudentMapper.java
Normal file
31
src/main/java/org/acme/StudentMapper.java
Normal file
@ -0,0 +1,31 @@
|
||||
package org.acme;
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
@ApplicationScoped
|
||||
public class StudentMapper {
|
||||
|
||||
public Student toDto(StudentEntity entity) {
|
||||
if (entity == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Student.builder()
|
||||
.id(entity.getId() != null ? entity.getId().toString() : null)
|
||||
.firstName(entity.getFirstName())
|
||||
.lastName(entity.getLastName())
|
||||
.build();
|
||||
}
|
||||
|
||||
public StudentEntity toEntity(Student dto) {
|
||||
if (dto == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return StudentEntity.builder()
|
||||
.id(dto.getId() != null ? java.util.UUID.fromString(dto.getId()) : null)
|
||||
.firstName(dto.getFirstName())
|
||||
.lastName(dto.getLastName())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@ -37,5 +37,17 @@ public class StudentRepository {
|
||||
entityManager.remove(student);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteById(final UUID id) {
|
||||
int count = entityManager.createQuery("delete from StudentEntity s where s.id = :id")
|
||||
.setParameter("id", id)
|
||||
.executeUpdate();
|
||||
|
||||
if(count > 1){
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -35,6 +35,7 @@ public class StudentResource {
|
||||
}
|
||||
|
||||
@POST
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public Response create(final Student student){
|
||||
if (student == null) {
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity("Student cannot be null").build();
|
||||
|
||||
62
src/main/java/org/acme/StudentService.java
Normal file
62
src/main/java/org/acme/StudentService.java
Normal file
@ -0,0 +1,62 @@
|
||||
package org.acme;
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ApplicationScoped
|
||||
public class StudentService {
|
||||
|
||||
@Inject
|
||||
StudentRepository studentRepository;
|
||||
|
||||
@Inject
|
||||
StudentMapper studentMapper;
|
||||
|
||||
public Collection<Student> findAll() {
|
||||
return studentRepository.findAll().stream()
|
||||
.map(studentMapper::toDto)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public Optional<Student> findById(final String id) {
|
||||
if (id == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
try {
|
||||
UUID uuid = UUID.fromString(id);
|
||||
StudentEntity entity = studentRepository.findById(uuid);
|
||||
return Optional.ofNullable(studentMapper.toDto(entity));
|
||||
} catch (IllegalArgumentException e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public String create(final Student student) {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
student.setId(uuid.toString());
|
||||
StudentEntity entity = studentMapper.toEntity(student);
|
||||
studentRepository.create(entity);
|
||||
return uuid.toString();
|
||||
}
|
||||
|
||||
public void update(final Student student) {
|
||||
StudentEntity entity = studentMapper.toEntity(student);
|
||||
studentRepository.update(entity);
|
||||
}
|
||||
|
||||
public void delete(final String id) {
|
||||
if (id == null) return;
|
||||
try {
|
||||
UUID uuid = UUID.fromString(id);
|
||||
StudentEntity entity = studentRepository.findById(uuid);
|
||||
if (entity != null) {
|
||||
studentRepository.delete(entity);
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
// Invalid UUID, do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,18 +0,0 @@
|
||||
package org.acme;
|
||||
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import static io.restassured.RestAssured.*;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@QuarkusTest
|
||||
public class StudentResourceTest {
|
||||
private static final String BASE_PATH = "/api/v1/students";
|
||||
|
||||
@Test
|
||||
void getAll_shouldReturnOneElement() {
|
||||
given().when().get(BASE_PATH).then().statusCode(200).body("size()", is(1)).body("[0].firstName", equalTo("John")).body("[0].lastName", equalTo("Doe"));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user