Skip to content

Commit

Permalink
Merge pull request #86 from educ-ai-org/feat/remove-participant
Browse files Browse the repository at this point in the history
EA-20 | Feat - Remove Participant from Classroom
  • Loading branch information
luuh-oliveira authored Jun 21, 2024
2 parents 746fbae + 8b91ed6 commit 0ca829b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/main/java/api/educai/controllers/ClassroomController.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,14 @@ public ResponseEntity<ClassroomInfoDTO> updateClassroom(@PathVariable ObjectId i

return status(200).body(classroomService.updateClassroom(id, classroom, userId));
}

@Operation(summary = "Remove um aluno de uma sala de aula")
@DeleteMapping("/{id}/user/{userId}")
@Secured("ROLE_TEACHER")
public ResponseEntity<Void> removeUser(@PathVariable ObjectId id, @PathVariable ObjectId userId, HttpServletRequest request) {
ObjectId requestUserId = (ObjectId) request.getAttribute("userId");
classroomService.removeUser(id, userId, requestUserId);
return status(204).build();
}

}
19 changes: 18 additions & 1 deletion src/main/java/api/educai/services/ClassroomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import api.educai.entities.User;
import api.educai.repositories.AnswerRepository;
import api.educai.repositories.ClassroomRepository;
import api.educai.repositories.UserRespository;
import api.educai.utils.CSVGenerator;
import api.educai.utils.PasswordGenerator;
import api.educai.utils.email.EmailService;
Expand All @@ -36,7 +37,7 @@ public class ClassroomService {
@Autowired
private ClassroomRepository classroomRepository;
@Autowired
private AnswerRepository answerRepository;
private UserRespository userRespository;

@Autowired
private UserService userService;
Expand Down Expand Up @@ -223,4 +224,20 @@ public Classroom getClassroomByClassworkId(ObjectId classworkId) {
.orElseThrow(() -> new ResponseStatusException(HttpStatusCode.valueOf(404), "Classroom not found!"));
}

public void removeUser(ObjectId classroomId, ObjectId userId, ObjectId requestUserId) {

if (requestUserId.equals(userId)) {
throw new ResponseStatusException(HttpStatusCode.valueOf(403), "You cannot remove yourself from the classroom");
}

Classroom classroom = getClassroomById(classroomId);
User user = userService.getUserById(userId);

classroom.getParticipants().removeIf(participant -> participant.getId().equals(userId));
user.getClassrooms().removeIf(c -> c.getId().equals(classroomId));

userRespository.save(user);
classroomRepository.save(classroom);
}

}
47 changes: 44 additions & 3 deletions src/test/java/api/educai/services/ClassroomServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import api.educai.entities.Classroom;
import api.educai.entities.User;
import api.educai.enums.Role;
import api.educai.repositories.AnswerRepository;
import api.educai.repositories.ClassroomRepository;
import api.educai.repositories.UserRespository;
import org.bson.types.ObjectId;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
Expand All @@ -27,9 +28,8 @@ public class ClassroomServiceTest {

@Mock
private ClassroomRepository classroomRepository;

@Mock
private AnswerRepository answerRepository;
private UserRespository userRespository;

@Mock
private UserService userService;
Expand All @@ -41,6 +41,7 @@ public void setup() {
MockitoAnnotations.openMocks(this);
}

@DisplayName("Create Classroom - Happy Path")
@Test
public void createClassroom_HappyPath() {
Classroom classroom = new Classroom();
Expand All @@ -62,13 +63,15 @@ public void createClassroom_HappyPath() {
verify(classroomRepository, times(1)).save(classroom);
}

@DisplayName("Get Classroom Data By Id - Classroom Does Not Exists")
@Test
public void getClassroomDataById_NotFound() {
ObjectId id = new ObjectId();
when(classroomRepository.findById(id)).thenReturn(null);
assertThrows(ResponseStatusException.class, () -> classroomService.getClassroomDataById(id));
}

@DisplayName("Invite User - User Already Exists")
@Test
public void inviteUser_UserAlreadyExists() {
ObjectId id = new ObjectId();
Expand All @@ -80,6 +83,7 @@ public void inviteUser_UserAlreadyExists() {
assertThrows(ResponseStatusException.class, () -> classroomService.inviteUser(id, newUser));
}

@DisplayName("Get Classroom Participants - Empty List")
@Test
public void getClassroomParticipants_EmptyList() {
ObjectId id = new ObjectId();
Expand All @@ -89,6 +93,7 @@ public void getClassroomParticipants_EmptyList() {
assertTrue(classroomService.getClassroomParticipants(id).isEmpty());
}

@DisplayName("Get Classworks - Empty List")
@Test
public void getClassworks_EmptyList() {
ObjectId id = new ObjectId();
Expand All @@ -98,6 +103,7 @@ public void getClassworks_EmptyList() {
assertTrue(classroomService.getClassworks(id).isEmpty());
}

@DisplayName("Delete Classroom - User is Not a Teacher")
@Test
public void deleteClassroom_NotTeacher() {
ObjectId id = new ObjectId();
Expand All @@ -112,4 +118,39 @@ public void deleteClassroom_NotTeacher() {
when(userService.getUserById(userId)).thenReturn(user);
assertThrows(ResponseStatusException.class, () -> classroomService.deleteClassroom(id, userId));
}

@DisplayName("Remove User - Happy Path")
@Test
public void removeUser_UserExistsInClassroom() {
ObjectId classroomId = new ObjectId();
ObjectId reqUserId = new ObjectId();
ObjectId userId = new ObjectId();
Classroom classroom = new Classroom();
User user = new User("name", "email", "password", Role.STUDENT);
user.setId(userId);
user.getClassrooms().add(classroom);
classroom.addParticipant(user);
classroom.setId(classroomId);

when(classroomRepository.findById(classroomId)).thenReturn(classroom);
when(userService.getUserById(userId)).thenReturn(user);

classroomService.removeUser(classroomId, userId, reqUserId);

assertFalse(classroom.getParticipants().contains(user));
assertFalse(user.getClassrooms().contains(classroom));
verify(classroomRepository, times(1)).save(classroom);
}

@DisplayName("Remove User - Classroom Does Not Exist")
@Test
public void removeUser_ClassroomDoesNotExist() {
ObjectId classroomId = new ObjectId();
ObjectId userId = new ObjectId();
ObjectId reqUserId = new ObjectId();

when(classroomRepository.findById(classroomId)).thenReturn(null);

assertThrows(ResponseStatusException.class, () -> classroomService.removeUser(classroomId, userId, reqUserId));
}
}

0 comments on commit 0ca829b

Please sign in to comment.