Skip to content

Commit c3f7843

Browse files
refactor: improve the classes while looking for possible bugs
1 parent 7708641 commit c3f7843

5 files changed

Lines changed: 24 additions & 14 deletions

File tree

src/main/java/com/vianavitor/simplelibrarygame/controller/GlobalExceptionHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,7 @@ public ResponseEntity<ApiResponse<Void>> handleJWTCreationException(JWTCreationE
6767
ApiResponse<Void> response = ApiResponse.error(ex.getMessage(), request.getRequestURI());
6868
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
6969
}
70+
71+
// TODO: handle the JWT token expired exception with a redirection
72+
// TODO: handle the IllegalArgumentException in ClassroomService or create another Exception to match the error
7073
}

src/main/java/com/vianavitor/simplelibrarygame/controller/StudentController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public ResponseEntity<ApiResponse<Student>> register(@Valid @RequestBody Registe
3333

3434
studentService.register(student, request.classroomCode(), request.favoriteGenres());
3535

36+
// TODO: many to many relationship is creating a loop in the response, fix it
3637
return ResponseEntity.status(HttpStatus.CREATED)
3738
.body(ApiResponse.success(student, "Student registered", req.getRequestURI()));
3839
}

src/main/java/com/vianavitor/simplelibrarygame/service/ProfessorService.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.vianavitor.simplelibrarygame.exception.InvalidOperationException;
66
import com.vianavitor.simplelibrarygame.exception.ResourceNotFoundException;
77
import com.vianavitor.simplelibrarygame.exception.UserDeactivatedException;
8+
import com.vianavitor.simplelibrarygame.model.Classroom;
89
import com.vianavitor.simplelibrarygame.model.Professor;
910
import com.vianavitor.simplelibrarygame.repository.ClassroomRepository;
1011
import com.vianavitor.simplelibrarygame.repository.ProfessorRepository;
@@ -42,14 +43,14 @@ public Professor register(Professor entity) throws DuplicateResourceException {
4243
}
4344

4445
public void register(Professor newProfessor, String classroomCode) throws DuplicateResourceException, ResourceNotFoundException {
45-
classroomRepository.findByPublicCode(classroomCode)
46-
.ifPresentOrElse((classroom ->
47-
newProfessor.getClassrooms().add(classroom)
48-
), () -> {
49-
throw new ResourceNotFoundException("not found classroom");
50-
});
51-
52-
this.register(newProfessor);
46+
Classroom classroom = classroomRepository.findByPublicCode(classroomCode)
47+
.orElseThrow(() -> new ResourceNotFoundException("not found classroom"));
48+
49+
Professor professor = this.register(newProfessor);
50+
classroom.getUsers().add(professor);
51+
52+
classroomRepository.save(classroom);
53+
// newProfessor.getClassrooms().add(classroom);
5354
}
5455

5556
public List<Professor> getAll() {

src/main/java/com/vianavitor/simplelibrarygame/service/StudentService.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.vianavitor.simplelibrarygame.exception.InvalidOperationException;
66
import com.vianavitor.simplelibrarygame.exception.ResourceNotFoundException;
77
import com.vianavitor.simplelibrarygame.exception.UserDeactivatedException;
8+
import com.vianavitor.simplelibrarygame.model.Classroom;
89
import com.vianavitor.simplelibrarygame.model.Genre;
910
import com.vianavitor.simplelibrarygame.model.Student;
1011
import com.vianavitor.simplelibrarygame.model.StudentStats;
@@ -68,20 +69,22 @@ public Student register(Student entity) throws DuplicateResourceException {
6869
}
6970

7071
public void register(Student newStudent, String classroomCode, Set<Genre> favoriteGenres) throws DuplicateResourceException, ResourceNotFoundException {
71-
classroomRepository.findByPublicCode(classroomCode)
72-
.ifPresentOrElse((classroom ->
73-
newStudent.getClassrooms().add(classroom)
74-
), () -> {
75-
throw new ResourceNotFoundException("not found classroom");
76-
});
72+
Classroom classroom = classroomRepository.findByPublicCode(classroomCode)
73+
.orElseThrow(() -> new ResourceNotFoundException("not found classroom"));
7774

7875
newStudent.setFavoriteGenre(favoriteGenres);
7976
Student student = this.register(newStudent);
8077

8178
stats.setUser(student);
79+
stats.setLevel(1);
80+
stats.setMaxLvlExperience(150);
8281
stats = statsRepository.save(stats);
8382

83+
classroom.getUsers().add(student);
84+
classroomRepository.save(classroom);
85+
8486
student.setStats(stats);
87+
student.getClassrooms().add(classroom);
8588
repository.save(student);
8689
}
8790

src/main/java/com/vianavitor/simplelibrarygame/service/StudentStatsService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public StudentStats create(Long userId) throws ResourceNotFoundException, Duplic
7878
}
7979

8080
stats.setUser(student);
81+
stats.setLevel(1);
82+
stats.setMaxLvlExperience(150);
8183
stats = repository.save(stats);
8284

8385
student.setStats(stats);

0 commit comments

Comments
 (0)