Commit 7556ff7c authored by Darrick Yong's avatar Darrick Yong

add uniqueness to email in Model and findByEmail

parent a51c8c01
package com.student.details.controllers;
import com.student.details.exceptions.ResourceNotFoundException;
import com.student.details.models.Student;
import com.student.details.services.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@CrossOrigin(origins = "*")
@RestController
......@@ -24,4 +24,37 @@ public class DetailsController {
return studentService.findAllStudents();
}
@PostMapping("/students")
public Student createStudent(@RequestBody Student student) {
return studentService.addStudent(student);
}
@GetMapping("/students/{id}")
public ResponseEntity<Student> getStudentById(@PathVariable(value = "id") Long studentId) throws ResourceNotFoundException {
Student student = studentService.getStudentById(studentId)
.orElseThrow(() -> new ResourceNotFoundException("Student by Id Not Found" + studentId));
return ResponseEntity.ok().body(student);
}
@GetMapping("/students/email/{email:.+}")
public ResponseEntity<Student> getStudentByEmail(@PathVariable(value = "email") String email) throws ResourceNotFoundException {
Student student = studentService.getStudentByEmail(email)
.orElseThrow(() -> new ResourceNotFoundException("Student with email" + email + "not found."));
return ResponseEntity.ok().body(student);
}
@PutMapping("/students/{id}")
public Student updateStudent(@PathVariable(value = "id") Long studentId, @RequestBody Student student) throws ResourceNotFoundException {
Student studentToUpdate = studentService.getStudentById(studentId)
.orElseThrow(() -> new ResourceNotFoundException("Student by Id Not Found" + studentId));
return studentService.updateStudent(studentId, student);
}
@DeleteMapping("/students/{id}")
public Map<String, Boolean> deleteEmployee(@PathVariable(value = "id") Long studentId) throws ResourceNotFoundException {
Student student = studentService.getStudentById(studentId)
.orElseThrow(() -> new ResourceNotFoundException("Student by Id Not Found" + studentId));
return studentService.deleteStudent(studentId);
}
}
......@@ -11,7 +11,9 @@ public class Student {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true)
private String email;
private String firstName;
private String lastName;
private LocalDate dateOfBirth;
......
......@@ -2,10 +2,16 @@ package com.student.details.repositories;
import com.student.details.models.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {
@Query("from Student s where s.email = ?1")
Optional<Student> findStudentByEmail(String email);
}
......@@ -30,6 +30,10 @@ public class StudentService {
return studentRepository.findById(studentId);
}
public Optional<Student> getStudentByEmail(String email) {
return studentRepository.findStudentByEmail(email);
}
public Student updateStudent(Long studentId, Student studentDetails) throws ResourceNotFoundException {
Student student = studentRepository.findById(studentId)
.orElseThrow(() -> new ResourceNotFoundException("Student id: " + studentId + " not found"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment