Skip to content

Commit be8348b

Browse files
committed
add json ignore to student ✅
1 parent 8687ebc commit be8348b

File tree

11 files changed

+184
-26
lines changed

11 files changed

+184
-26
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.gdsc.springbootworkshop.controllers;
2+
3+
import com.gdsc.springbootworkshop.entites.Classroom;
4+
import com.gdsc.springbootworkshop.entites.Student;
5+
import com.gdsc.springbootworkshop.services.classroom.ClassroomService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.*;
8+
9+
import java.util.List;
10+
11+
@RestController
12+
@RequestMapping("classrooms")
13+
public class ClassroomController {
14+
15+
@Autowired
16+
ClassroomService classroomService;
17+
18+
19+
@GetMapping("")
20+
List<Classroom> findAllClassrooms(){
21+
return classroomService.getAll() ;
22+
}
23+
24+
@PostMapping("")
25+
Classroom addClassroom(@RequestBody Classroom classroom) throws Exception {
26+
return classroomService.add(classroom);
27+
}
28+
29+
@PutMapping("/{id}")
30+
Classroom updateClassroom(@PathVariable("id") Long id,@RequestBody Classroom classroom) throws Exception {
31+
return classroomService.update(id,classroom);
32+
}
33+
34+
@DeleteMapping("/{id}")
35+
void deleteStudent(@PathVariable("id") Long id){
36+
classroomService.delete(id);
37+
}
38+
39+
@GetMapping("/{id}")
40+
Classroom findById(@PathVariable("id") Long id){
41+
return classroomService.findById(id);
42+
}
43+
44+
// @GetMapping("findStudentsByClassroomName/{name}")
45+
// public List<Student> getStudentsByClassroomName(@PathVariable("name") String name) {
46+
// return classroomService.getStudentsByClassroomName(name);
47+
// }
48+
49+
50+
}

src/main/java/com/gdsc/springbootworkshop/controllers/StudentController.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.gdsc.springbootworkshop.controllers;
22

33
import com.gdsc.springbootworkshop.entites.Student;
4-
import com.gdsc.springbootworkshop.services.StudentService;
4+
import com.gdsc.springbootworkshop.services.student.StudentService;
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.web.bind.annotation.*;
77

8-
import javax.websocket.server.PathParam;
98
import java.util.List;
109

1110
@RestController
@@ -17,22 +16,22 @@ public class StudentController {
1716

1817
@GetMapping("")
1918
List<Student> findAllStudents(){
20-
return studentService.getAllStudents();
19+
return studentService.getAll();
2120
}
2221

2322
@PostMapping("")
2423
Student addStudent(@RequestBody Student student) throws Exception {
25-
return studentService.addStudent(student);
24+
return studentService.add(student);
2625
}
2726

2827
@PutMapping("/{id}")
2928
Student updateStudent(@PathVariable("id") Long id, @RequestBody Student student){
30-
return studentService.updateStudent(id, student);
29+
return studentService.update(id, student);
3130
}
3231

3332
@DeleteMapping("/{id}")
3433
void deleteStudent(@PathVariable("id") Long id){
35-
studentService.deleteStudent(id);
34+
studentService.delete(id);
3635
}
3736

3837
@GetMapping("/{id}")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.gdsc.springbootworkshop.entites;
2+
3+
import lombok.Getter;
4+
import lombok.NoArgsConstructor;
5+
import lombok.Setter;
6+
7+
import javax.persistence.*;
8+
import java.util.List;
9+
10+
@Entity
11+
@Getter
12+
@Setter
13+
@NoArgsConstructor
14+
public class Classroom {
15+
@Id
16+
@GeneratedValue(strategy = GenerationType.AUTO)
17+
private Long idClassroom;
18+
19+
@Column(unique = true)
20+
private String name;
21+
22+
@OneToMany()
23+
@JoinColumn(name = "idClassroom")
24+
List<Student> students;
25+
}

src/main/java/com/gdsc/springbootworkshop/entites/Student.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.gdsc.springbootworkshop.entites;
22

33

4+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
45
import lombok.AllArgsConstructor;
56
import lombok.Getter;
67
import lombok.NoArgsConstructor;
@@ -27,6 +28,11 @@ public class Student {
2728
@Enumerated(EnumType.STRING)
2829
private GENDER gender;
2930

31+
@ManyToOne()
32+
@JoinColumn(name = "idClassroom")
33+
@JsonIgnoreProperties({"students"})
34+
private Classroom classroom;
35+
3036
public Student(String firstName, String lastName, String email, GENDER gender) {
3137
this.firstName = firstName;
3238
this.lastName = lastName;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.gdsc.springbootworkshop.repositories;
2+
3+
import com.gdsc.springbootworkshop.entites.Classroom;
4+
import com.gdsc.springbootworkshop.entites.Student;
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.stereotype.Repository;
7+
8+
@Repository
9+
public interface ClassroomRepository extends JpaRepository<Classroom, Long> {
10+
11+
Classroom findByName(String name);
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.gdsc.springbootworkshop.services;
2+
3+
import java.util.List;
4+
5+
public interface ICrudService <Class,TypeId> {
6+
List<Class> getAll();
7+
Class add(Class class1) throws Exception;
8+
Class update(TypeId typeId,Class class1) throws Exception;
9+
void delete(TypeId typeId);
10+
}

src/main/java/com/gdsc/springbootworkshop/services/IStudentService.java

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.gdsc.springbootworkshop.services.classroom;
2+
3+
import com.gdsc.springbootworkshop.entites.Classroom;
4+
import com.gdsc.springbootworkshop.repositories.ClassroomRepository;
5+
import com.gdsc.springbootworkshop.services.ICrudService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Service;
8+
9+
import java.util.List;
10+
11+
@Service
12+
public class ClassroomService implements IClassroomService, ICrudService<Classroom,Long> {
13+
14+
@Autowired
15+
private ClassroomRepository studentRepository;
16+
17+
@Override
18+
public List<Classroom> getAll() {
19+
return studentRepository.findAll();
20+
}
21+
22+
@Override
23+
public Classroom add(Classroom classroom) throws Exception {
24+
if(studentRepository.findByName(classroom.getName()) != null)
25+
throw new Exception("Classroom already exists");
26+
return studentRepository.save(classroom);
27+
}
28+
29+
@Override
30+
public Classroom update(Long id, Classroom classroom) {
31+
if(studentRepository.findById(id).isPresent()){
32+
Classroom toUpdateClassroom = studentRepository.findById(id).get();
33+
toUpdateClassroom.setName(classroom.getName());
34+
return studentRepository.save(toUpdateClassroom);
35+
}
36+
return null;
37+
}
38+
39+
@Override
40+
public void delete(Long id) {
41+
studentRepository.deleteById(id);
42+
}
43+
44+
@Override
45+
public Classroom findById(Long id) {
46+
return studentRepository.findById(id).get();
47+
}
48+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.gdsc.springbootworkshop.services.classroom;
2+
3+
import com.gdsc.springbootworkshop.entites.Classroom;
4+
5+
import java.util.List;
6+
7+
public interface IClassroomService {
8+
9+
Classroom findById(Long id);
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.gdsc.springbootworkshop.services.student;
2+
3+
import com.gdsc.springbootworkshop.entites.Student;
4+
5+
import java.util.List;
6+
7+
public interface IStudentService {
8+
9+
Student findById(Long id);
10+
}

src/main/java/com/gdsc/springbootworkshop/services/StudentService.java renamed to src/main/java/com/gdsc/springbootworkshop/services/student/StudentService.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
1-
package com.gdsc.springbootworkshop.services;
1+
package com.gdsc.springbootworkshop.services.student;
22

3+
import com.gdsc.springbootworkshop.entites.Classroom;
34
import com.gdsc.springbootworkshop.entites.Student;
45
import com.gdsc.springbootworkshop.repositories.StudentRepository;
6+
import com.gdsc.springbootworkshop.services.ICrudService;
57
import org.springframework.beans.factory.annotation.Autowired;
68
import org.springframework.stereotype.Service;
79

810
import java.util.List;
911

1012
@Service
11-
public class StudentService implements IStudentService {
13+
public class StudentService implements IStudentService , ICrudService<Student,Long> {
1214

1315
@Autowired
1416
private StudentRepository studentRepository;
1517

1618
@Override
17-
public List<Student> getAllStudents() {
19+
public List<Student> getAll() {
1820
return studentRepository.findAll();
1921
}
2022

2123
@Override
22-
public Student addStudent(Student student) throws Exception {
24+
public Student add(Student student) throws Exception {
2325
if(studentRepository.findByEmail(student.getEmail()) != null)
2426
throw new Exception("Student already exists");
2527
return studentRepository.save(student);
2628
}
2729

2830
@Override
29-
public Student updateStudent(Long id, Student student) {
31+
public Student update(Long id, Student student) {
3032
if(studentRepository.findById(id).isPresent()){
3133
Student toUpdateStudent = studentRepository.findById(id).get();
3234
toUpdateStudent.setFirstName(student.getFirstName());
@@ -39,7 +41,7 @@ public Student updateStudent(Long id, Student student) {
3941
}
4042

4143
@Override
42-
public void deleteStudent(Long id) {
44+
public void delete(Long id) {
4345
studentRepository.deleteById(id);
4446
}
4547

0 commit comments

Comments
 (0)