- 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:
parent
1f40cc4675
commit
4fa547a09d
@ -1,9 +1,14 @@
|
|||||||
package com.example.springcrashcourse.student;
|
package com.example.springcrashcourse.student;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
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.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import com.example.springcrashcourse.student.Student;
|
import com.example.springcrashcourse.student.Student;
|
||||||
@ -11,6 +16,7 @@ import com.example.springcrashcourse.student.Student;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.Month;
|
import java.time.Month;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/v1")
|
@RequestMapping("/api/v1")
|
||||||
@ -28,5 +34,23 @@ public class StudentController {
|
|||||||
return studentService.getStudents();
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,9 +1,16 @@
|
|||||||
package com.example.springcrashcourse.student;
|
package com.example.springcrashcourse.student;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface StudentRepository extends JpaRepository<Student, Long> {
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,13 @@ package com.example.springcrashcourse.student;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.Month;
|
import java.time.Month;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import jakarta.transaction.Transactional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class StudentService {
|
public class StudentService {
|
||||||
|
|
||||||
@ -20,4 +23,51 @@ public class StudentService {
|
|||||||
public List<Student> getStudents() {
|
public List<Student> getStudents() {
|
||||||
return studentRepository.findAll();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
spring.datasource.url=jdbc:postgresql://localhost:5432/student
|
spring.datasource.url=jdbc:postgresql://localhost:3636/student
|
||||||
spring.datasource.username=postgres
|
spring.datasource.username=okechi
|
||||||
spring.datasource.password=testing
|
spring.datasource.password=testing
|
||||||
spring.jpa.hibernate.ddl-auto=create-drop
|
spring.jpa.hibernate.ddl-auto=create-drop
|
||||||
spring.jpa.show-sql=true
|
spring.jpa.show-sql=true
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user