Skip to content

Commit 56cbc07

Browse files
Updated the member controller to have apis of post for memberclasses to add relations here, the json managed references opens opportunities to do so without cyclic dependency so add them to classes and memberclass as well for future use, not adding delete api as cascading is present for both ends ensuring automatic deletions.
1 parent 594af08 commit 56cbc07

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

‎src/main/java/com/example/easynotes/controller/MemberController.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,31 @@ public HashMap getClassesById(@PathVariable Long memberId, @PathVariable Long cl
5555
return null;
5656
}
5757

58+
@PostMapping("/memclasses")
59+
public MemberClass assignClassToMember(@RequestBody Map<String, Long> body) {
60+
// Log for debugging to check if body is received correctly
61+
System.out.println("Request body: " + body);
62+
63+
Long memberId = body.get("memberId");
64+
Long classId = body.get("classId");
65+
66+
if (memberId == null || classId == null) {
67+
throw new RuntimeException("memberId and classId are required");
68+
}
69+
70+
Member member = memberRepository.findById(memberId)
71+
.orElseThrow(() -> new RuntimeException("Member not found"));
72+
Classes classes = classesRepository.findById(classId)
73+
.orElseThrow(() -> new RuntimeException("Class not found"));
74+
75+
MemberClass memberClass = new MemberClass();
76+
memberClass.setMember(member);
77+
memberClass.setClasses(classes);
78+
79+
return memberClassRepository.save(memberClass);
80+
}
81+
82+
5883
@GetMapping("/{memberId}")
5984
public Optional<Member> getMemberById(@PathVariable Long memberId) {
6085
return memberRepository.findById(memberId);
@@ -96,4 +121,5 @@ public List<Feedback> getMemberFeedback(@PathVariable Long memberId) {
96121
// POST: Assign a member to a class
97122

98123

124+
99125
}

‎src/main/java/com/example/easynotes/model/Classes.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.ArrayList;
88
import java.util.List;
99
import com.fasterxml.jackson.annotation.JsonIgnore;
10+
import com.fasterxml.jackson.annotation.JsonManagedReference;
1011

1112
@Entity
1213
public class Classes {
@@ -31,6 +32,7 @@ public class Classes {
3132

3233
@OneToMany(mappedBy = "classes", cascade = CascadeType.ALL, orphanRemoval = true)
3334
@JsonIgnore
35+
@JsonManagedReference
3436
private List<MemberClass> memberClass = new ArrayList<>();
3537

3638

‎src/main/java/com/example/easynotes/model/MemberClass.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example.easynotes.model;
22

33
import com.fasterxml.jackson.annotation.JsonBackReference;
4+
import com.fasterxml.jackson.annotation.JsonIgnore;
45

56
import javax.persistence.*;
67

@@ -18,6 +19,7 @@ public class MemberClass {
1819

1920
@ManyToOne
2021
@JoinColumn(name = "class_id")
22+
@JsonBackReference
2123
private Classes classes;
2224

2325

0 commit comments

Comments
 (0)