From 0c73f6f44d24e8e89ede0eeee87bf1300f4f2d3e Mon Sep 17 00:00:00 2001 From: LeoMoulin Date: Thu, 21 Mar 2024 14:32:53 +0100 Subject: [PATCH] Functionnal RegNo generator wich only needs to be more parametrized --- .../Clyde/EndPoints/MockController.java | 15 +++++------ .../ovh/herisson/Clyde/RegNoGenerator.java | 24 +++++++++++------- .../Clyde/Repositories/UserRepository.java | 3 --- .../herisson/Clyde/Services/UserService.java | 14 ++++++++++- .../ovh/herisson/Clyde/UserSaveCounter.java | 25 +++++++++++++++++++ .../src/main/resources/application.properties | 1 - 6 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/UserSaveCounter.java 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 0b7a1b4..d3a1e05 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java @@ -15,7 +15,7 @@ import java.util.Date; public class MockController { private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); - + public final UserService userService; public final UserRepository userRepo; public final TokenRepository tokenRepo; public final TokenService tokenService; @@ -27,7 +27,8 @@ public class MockController { ArrayList mockUsers; - public MockController(UserRepository userRepo, TokenRepository tokenRepo, TokenService tokenService, CurriculumCourseService CurriculumCourseService, CurriculumService curriculumService, CourseService courseService, InscriptionService inscriptionService){ + public MockController(UserService userService, UserRepository userRepo, TokenRepository tokenRepo, TokenService tokenService, CurriculumCourseService CurriculumCourseService, CurriculumService curriculumService, CourseService courseService, InscriptionService inscriptionService){ + this.userService = userService; this.tokenRepo = tokenRepo; this.userRepo = userRepo; this.tokenService = tokenService; @@ -45,21 +46,21 @@ public class MockController { @PostMapping("/mock") public void postMock(){ - // user part - User herobrine = new User("brine","hero","admin@admin.com","behind","ShadowsLand",new Date(0), null,Role.Admin,passwordEncoder.encode("admin")); User joe = new User("Mama","Joe","student@student.com","roundabout","England",new Date(0), null,Role.Student,passwordEncoder.encode("student")); User meh = new User("Polo","Marco","secretary@secretary.com","a Box","Monaco",new Date(0), null,Role.Secretary,passwordEncoder.encode("secretary")); User joke = new User("Gaillard","Corentin","teacher@teacher.com","lab","faculty",new Date(0), null,Role.Teacher,passwordEncoder.encode("teacher")); User jojo = new User("Bridoux","Justin","teacher2@teacher2.com","lab","faculty",new Date(0), null,Role.Teacher,passwordEncoder.encode("teacher")); User lena = new User("Louille","Lena","inscriptionService@InscriptionService.com","no","yes",new Date(0), null,Role.InscriptionService,passwordEncoder.encode("inscriptionService")); - mockUsers = new ArrayList<>(Arrays.asList(herobrine,joe,meh,joke,lena,jojo)); - userRepo.saveAll(mockUsers); + mockUsers = new ArrayList<>(Arrays.asList(joke, herobrine, joe, meh, jojo)); + userService.saveAll(mockUsers); + userService.save(lena); - // Course / Curriculum part + + //Course / Curriculum part Curriculum infoBab1 = new Curriculum(1,"info"); Curriculum chemistryBab1 = new Curriculum(1,"chemistry"); diff --git a/backend/src/main/java/ovh/herisson/Clyde/RegNoGenerator.java b/backend/src/main/java/ovh/herisson/Clyde/RegNoGenerator.java index efa2047..783ceb7 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/RegNoGenerator.java +++ b/backend/src/main/java/ovh/herisson/Clyde/RegNoGenerator.java @@ -1,18 +1,15 @@ package ovh.herisson.Clyde; -import org.hibernate.HibernateException; import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess; -import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.id.IdentifierGenerator; - -import java.io.Serializable; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class RegNoGenerator implements IdentifierGenerator { + private final UserSaveCounter usv = UserSaveCounter.getInstance(); @Override public Object generate(SharedSessionContractImplementor session, Object object) { try{ @@ -20,16 +17,25 @@ public class RegNoGenerator implements IdentifierGenerator { Connection conn = jdbccon.obtainConnection(); Statement statement = conn.createStatement(); - String query = "select count(reg_no)+1 from Users"; + + int n = usv.getCount(); + String query = "select count(reg_no)+"+n+" from Users"; + usv.increment(); ResultSet set = statement.executeQuery(query); - - if (set.next()){ - return set.getLong(1)+1000; + long resp = 0; + if(set.next()){ + resp = set.getLong(1)+1000; + System.out.println(resp); } + + conn.close(); + statement.close(); + + return resp; + } catch (SQLException e) { throw new RuntimeException(e); } - return null; } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java index 413f090..1e590a3 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserRepository.java @@ -5,13 +5,10 @@ import org.springframework.data.repository.CrudRepository; import ovh.herisson.Clyde.Tables.User; public interface UserRepository extends CrudRepository { - User findById(long id); User findByEmail(String email); - - @Query("select u from User u where u.role = ovh.herisson.Clyde.Tables.Role.Teacher") Iterable findAllTeachers(); diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java index 72eabd5..ef1ce1c 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java @@ -5,13 +5,15 @@ import org.springframework.stereotype.Service; import ovh.herisson.Clyde.Repositories.UserRepository; import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.User; +import ovh.herisson.Clyde.UserSaveCounter; + import java.util.*; @Service public class UserService { private final UserRepository userRepo; private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); - + private final UserSaveCounter usc = UserSaveCounter.getInstance(); public UserService(UserRepository userRepo){ this.userRepo = userRepo; } @@ -106,10 +108,19 @@ public class UserService { } public User save(User user){ + usc.reset(); user.setPassword(passwordEncoder.encode(user.getPassword())); return userRepo.save(user); } + public void saveAll(ArrayList list){ + //S'assure que le compteur est bien a 0 + usc.reset(); + userRepo.saveAll(list); + //Reset le compteur a zero pour les futurs ajouts + usc.reset(); + } + public Iterable getAll(){ return userRepo.findAll(); } @@ -131,4 +142,5 @@ public class UserService { public void delete(User user) { userRepo.delete(user); } + } diff --git a/backend/src/main/java/ovh/herisson/Clyde/UserSaveCounter.java b/backend/src/main/java/ovh/herisson/Clyde/UserSaveCounter.java new file mode 100644 index 0000000..3a8f696 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/UserSaveCounter.java @@ -0,0 +1,25 @@ +package ovh.herisson.Clyde; + +public final class UserSaveCounter { + private static UserSaveCounter instance; + private int count = 0; + + public void increment() { + this.count+=1; + } + + public int getCount() { + return count; + } + + public void reset(){ + this.count = 0; + } + public static UserSaveCounter getInstance(){ + if (instance == null){ + instance = new UserSaveCounter(); + return instance; + } + return instance; + } +} diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index 5d00d8e..df288a6 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -1,3 +1,2 @@ spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect -spring.sql.init.mode=always \ No newline at end of file