From 4fa547a09db3b0203016ff9b60435fc4731b820b Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Tue, 31 Oct 2023 09:23:24 -0400 Subject: [PATCH] - went over PUT, POST, and DELETE mappings - went over using the RequestBody and RequestParams in RestControllers - went over writing business logic in Service Layer classes --- .../student/StudentController.java | 24 +++++++++ .../student/StudentRepository.java | 7 +++ .../student/StudentService.java | 50 +++++++++++++++++++ src/main/resources/application.properties | 4 +- 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/example/springcrashcourse/student/StudentController.java b/src/main/java/com/example/springcrashcourse/student/StudentController.java index d6ade60..430bc92 100644 --- a/src/main/java/com/example/springcrashcourse/student/StudentController.java +++ b/src/main/java/com/example/springcrashcourse/student/StudentController.java @@ -1,9 +1,14 @@ package com.example.springcrashcourse.student; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.example.springcrashcourse.student.Student; @@ -11,6 +16,7 @@ import com.example.springcrashcourse.student.Student; import java.time.LocalDate; import java.time.Month; import java.util.List; +import java.util.Optional; @RestController @RequestMapping("/api/v1") @@ -28,5 +34,23 @@ public class StudentController { return studentService.getStudents(); } + @PostMapping(path = "/student/registerStudent") + public void registerStudent(@RequestBody Student student) { + studentService.addStudent(student); + } + + @DeleteMapping(path = "/student/{studentId}") + public void deleteStudent(@PathVariable("studentId") Long studentId) { + studentService.deleteStudent(studentId); + } + + @PutMapping(path = "/student/{studentId}") + public void updateStudent( + @PathVariable("studentId") Long studentId, + @RequestParam(required = false) String name, + @RequestParam(required = false) String email + ) { + studentService.updateStudent(studentId, name, email); + } } \ No newline at end of file diff --git a/src/main/java/com/example/springcrashcourse/student/StudentRepository.java b/src/main/java/com/example/springcrashcourse/student/StudentRepository.java index 7b24249..0a78886 100644 --- a/src/main/java/com/example/springcrashcourse/student/StudentRepository.java +++ b/src/main/java/com/example/springcrashcourse/student/StudentRepository.java @@ -1,9 +1,16 @@ package com.example.springcrashcourse.student; +import java.util.Optional; + import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; @Repository public interface StudentRepository extends JpaRepository { + + // SELECT * FROM student WHERE email = ?1; + @Query("SELECT s FROM Student s WHERE s.email = ?1") + Optional findStudentByEmail(String email); } diff --git a/src/main/java/com/example/springcrashcourse/student/StudentService.java b/src/main/java/com/example/springcrashcourse/student/StudentService.java index 4cf27cf..5991030 100644 --- a/src/main/java/com/example/springcrashcourse/student/StudentService.java +++ b/src/main/java/com/example/springcrashcourse/student/StudentService.java @@ -3,10 +3,13 @@ package com.example.springcrashcourse.student; import java.time.LocalDate; import java.time.Month; import java.util.List; +import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import jakarta.transaction.Transactional; + @Service public class StudentService { @@ -20,4 +23,51 @@ public class StudentService { public List getStudents() { return studentRepository.findAll(); } + + public void addStudent(Student student) { + System.out.println(student); + Optional studentOptional = studentRepository + .findStudentByEmail(student.getEmail()); + + if(studentOptional.isPresent()) { + throw new IllegalStateException("Email Taken"); + } + studentRepository.save(student); + } + + public void deleteStudent(Long studentId) { + boolean studentExists = studentRepository.existsById(studentId); + + if(!studentExists) { + throw new IllegalStateException("Student with id " + studentId + " does not exists"); + } + if(studentExists) { + studentRepository.deleteById(studentId); + } + } + + @Transactional + public void updateStudent(Long studentId, String name, String email) { + Optional studentOptional = studentRepository.findById(studentId); + + if(!studentOptional.isPresent()) { + throw new IllegalStateException("Student with id " + studentId + " does not exists"); + } + + Student student = studentOptional.get(); + + if (name != null) student.setName(name); + + if (email != null) { + Optional studentThatExistsAlready = studentRepository + .findStudentByEmail(student.getEmail()); + + if(studentThatExistsAlready.isPresent()) { + throw new IllegalStateException("Email Taken"); + } + student.setEmail(email); + } + + studentRepository.save(student); + } } \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6f6b417..c088ad8 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,6 +1,6 @@ -spring.datasource.url=jdbc:postgresql://localhost:5432/student -spring.datasource.username=postgres +spring.datasource.url=jdbc:postgresql://localhost:3636/student +spring.datasource.username=okechi spring.datasource.password=testing spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.show-sql=true