- 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
This commit is contained in:
Okechi Onyeje 2023-10-31 09:23:24 -04:00
parent 1f40cc4675
commit 4fa547a09d
4 changed files with 83 additions and 2 deletions

View File

@ -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);
}
}

View File

@ -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<Student, Long> {
// SELECT * FROM student WHERE email = ?1;
@Query("SELECT s FROM Student s WHERE s.email = ?1")
Optional<Student> findStudentByEmail(String email);
}

View File

@ -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<Student> getStudents() {
return studentRepository.findAll();
}
public void addStudent(Student student) {
System.out.println(student);
Optional<Student> 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<Student> 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<Student> studentThatExistsAlready = studentRepository
.findStudentByEmail(student.getEmail());
if(studentThatExistsAlready.isPresent()) {
throw new IllegalStateException("Email Taken");
}
student.setEmail(email);
}
studentRepository.save(student);
}
}

View File

@ -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