From 2f2a72bfa0b4f74ca45b17b79150b171160f8764 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Thu, 7 Mar 2024 17:29:31 +0100 Subject: [PATCH] added the possibility to remove the mocks --- .../Clyde/EndPoints/MockController.java | 20 ++++++++++++++----- .../Clyde/Repositories/TokenRepository.java | 2 ++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java index a01c7ec..0fc9a24 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java @@ -2,6 +2,7 @@ package ovh.herisson.Clyde.EndPoints; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import ovh.herisson.Clyde.Repositories.TokenRepository; @@ -23,6 +24,8 @@ public class MockController { public final UserRepository userRepo; public final TokenRepository tokenRepo; + ArrayList mockUsers; + public MockController(UserRepository userRepo, TokenRepository tokenRepo){ this.tokenRepo = tokenRepo; @@ -35,7 +38,7 @@ public class MockController { * They all have silly names */ - @PostMapping("/generateMock") + @PostMapping("/mock") public void postMock(){ User herobrine = new User("brine","hero","admin@admin.com","in your WalLs","ShadowsLand",new Date(0), Role.Admin,passwordEncoder.encode("admin")); @@ -43,13 +46,20 @@ public class MockController { User meh = new User("Inspiration","lackOf","secretary@secretary.com","a Box","the street",new Date(0), Role.Teacher,passwordEncoder.encode("secretary")); User joke = new User("CthemBalls","Lemme","teacher@teacher.com","lab","faculty",new Date(0), Role.Teacher,passwordEncoder.encode("teacher")); + mockUsers = new ArrayList(Arrays.asList(herobrine,joe,meh,joke)); - ArrayList users = new ArrayList(Arrays.asList(herobrine,joe,meh,joke)); + userRepo.saveAll(mockUsers); - userRepo.saveAll(users); - - for (User user: users){ + for (User user: mockUsers){ tokenRepo.save(new Token(user,user.getPassword())); } } + + @DeleteMapping("/mock") + public void deleteMock(){ + for (User user:mockUsers){ + tokenRepo.deleteAll(tokenRepo.getByUser(user)); + } + userRepo.deleteAll(mockUsers); + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/TokenRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/TokenRepository.java index 719f11e..d375e7a 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/TokenRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/TokenRepository.java @@ -7,4 +7,6 @@ import ovh.herisson.Clyde.Tables.User; public interface TokenRepository extends CrudRepository { Token getByToken(String token); + + Iterable getByUser(User user); }