From 451944e4fbbd988de8e418602ecdbbebba48d123 Mon Sep 17 00:00:00 2001 From: LeoMoulin Date: Mon, 15 Apr 2024 20:57:29 +0200 Subject: [PATCH] Implements the RegNo feature --- .../Clyde/EndPoints/MockController.java | 7 +-- .../Inscription/InscriptionService.java | 8 ++-- .../herisson/Clyde/Services/UserService.java | 8 ++++ .../herisson/Clyde/Tables/RegNoGenerator.java | 46 +++++++++++++++++++ .../java/ovh/herisson/Clyde/Tables/User.java | 4 +- .../Clyde/Repositories/UserRepoTest.java | 5 +- 6 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Tables/RegNoGenerator.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 14fd259..beb27fe 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java @@ -23,7 +23,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; @@ -40,7 +40,8 @@ public class MockController { public final ScholarshipRequestRepository scholarshipRequestRepository; - public MockController(UserRepository userRepo, TokenRepository tokenRepo, TokenService tokenService, CurriculumCourseService CurriculumCourseService, CurriculumService curriculumService, CourseService courseService, ExternalCurriculumRepository externalCurriculumRepository, InscriptionService inscriptionService, UserCurriculumRepository ucr, MinervalRepository minervalRepository, ScholarshipRequestRepository scholarshipRequestRepository){ + public MockController(UserService userService, UserRepository userRepo, TokenRepository tokenRepo, TokenService tokenService, CurriculumCourseService CurriculumCourseService, CurriculumService curriculumService, CourseService courseService, ExternalCurriculumRepository externalCurriculumRepository, InscriptionService inscriptionService, UserCurriculumRepository ucr, MinervalRepository minervalRepository, ScholarshipRequestRepository scholarshipRequestRepository){ + this.userService = userService; this.tokenRepo = tokenRepo; this.userRepo = userRepo; this.tokenService = tokenService; @@ -73,7 +74,7 @@ public class MockController { User popo = new User("Smith", "Paul", "paulsmith@gmail.com", "306 rue du poulet", "belgique", new Date(0), null, Role.Student, passwordEncoder.encode("jesuispaulleroi")); mockUsers = new ArrayList<>(Arrays.asList(herobrine,joe,meh,joke,lena,jojo, popo)); - userRepo.saveAll(mockUsers); + userService.saveAll(mockUsers); Minerval minerval = new Minerval(joe.getRegNo(), 0, 852, 2023); minervalRepository.save(minerval); diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/Inscription/InscriptionService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/Inscription/InscriptionService.java index c80e2d9..b889533 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/Inscription/InscriptionService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/Inscription/InscriptionService.java @@ -6,6 +6,7 @@ import ovh.herisson.Clyde.Repositories.*; import ovh.herisson.Clyde.Repositories.Inscription.ExternalCurriculumRepository; import ovh.herisson.Clyde.Repositories.Inscription.InscriptionRepository; import ovh.herisson.Clyde.Repositories.Inscription.MinervalRepository; +import ovh.herisson.Clyde.Services.UserService; import ovh.herisson.Clyde.Tables.*; import ovh.herisson.Clyde.Tables.Inscription.ExternalCurriculum; import ovh.herisson.Clyde.Tables.Inscription.InscriptionRequest; @@ -27,14 +28,15 @@ public class InscriptionService { private final MinervalRepository minervalRepository; private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); private final ExternalCurriculumRepository externalCurriculumRepository; - - public InscriptionService(InscriptionRepository inscriptionRepo, UserRepository userRepo, UserCurriculumRepository userCurriculumRepo, CurriculumRepository curriculumRepo, MinervalRepository minervalRepository, ExternalCurriculumRepository externalCurriculumRepository){ + private final UserService userService; + public InscriptionService(InscriptionRepository inscriptionRepo, UserRepository userRepo, UserCurriculumRepository userCurriculumRepo, CurriculumRepository curriculumRepo, MinervalRepository minervalRepository, ExternalCurriculumRepository externalCurriculumRepository, UserService userService){ this.inscriptionRepo = inscriptionRepo; this.userRepo = userRepo; this.userCurriculumRepo = userCurriculumRepo; this.curriculumRepo = curriculumRepo; this.minervalRepository = minervalRepository; this.externalCurriculumRepository = externalCurriculumRepository; + this.userService = userService; } public InscriptionRequest save(InscriptionRequest inscriptionRequest){ @@ -84,7 +86,7 @@ public class InscriptionService { inscrRequest.getPassword() ); - userRepo.save(userFromRequest); + userService.save(userFromRequest); userCurriculumRepo.save(new UserCurriculum(userFromRequest, curriculumRepo.findById(inscrRequest.getCurriculumId()),0)); //Create a minerval for the new student 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..9c6bd7f 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java @@ -2,6 +2,7 @@ package ovh.herisson.Clyde.Services; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; +import ovh.herisson.Clyde.Tables.RegNoGenerator; import ovh.herisson.Clyde.Repositories.UserRepository; import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.User; @@ -106,10 +107,17 @@ public class UserService { } public User save(User user){ + RegNoGenerator.resetCount(); user.setPassword(passwordEncoder.encode(user.getPassword())); return userRepo.save(user); } + public void saveAll(ArrayList list){ + //S'assure que le compteur est bien a 0 + RegNoGenerator.resetCount(); + userRepo.saveAll(list); + } + public Iterable getAll(){ return userRepo.findAll(); } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/RegNoGenerator.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/RegNoGenerator.java new file mode 100644 index 0000000..886cc09 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/RegNoGenerator.java @@ -0,0 +1,46 @@ +package ovh.herisson.Clyde.Tables; + +import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess; +import org.hibernate.engine.spi.SharedSessionContractImplementor; +import org.hibernate.id.IdentifierGenerator; + +import java.sql.*; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; + +public class RegNoGenerator implements IdentifierGenerator { + private static int count = 0; + @Override + public Object generate(SharedSessionContractImplementor session, Object object) { + try{ + JdbcConnectionAccess jdbccon = session.getJdbcConnectionAccess(); + Connection conn = jdbccon.obtainConnection(); + + Statement statement = conn.createStatement(); + + Calendar c = new GregorianCalendar(); + int y = c.get(Calendar.YEAR); + String query = "select count(reg_no) + "+count+" from Users where reg_no/10000 = " + y%1000; + + ResultSet set = statement.executeQuery(query); + long resp = 0; + if(set.next()){ + resp = set.getLong(1)+((y%1000)*10000); + count += 1; + } + + conn.close(); + statement.close(); + + return resp; + + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public static void resetCount(){ + count = 0; + } +} \ No newline at end of file diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java index 2badd32..d3668f3 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/User.java @@ -1,6 +1,7 @@ package ovh.herisson.Clyde.Tables; import jakarta.persistence.*; +import org.hibernate.annotations.GenericGenerator; import ovh.herisson.Clyde.Tables.Msg.Discussion; import ovh.herisson.Clyde.Tables.Msg.Message; @@ -12,7 +13,8 @@ import java.util.List; @Table(name = "Users") public class User { @Id - @GeneratedValue(strategy = GenerationType.AUTO) + @GenericGenerator(name = "userGen", type = ovh.herisson.Clyde.Tables.RegNoGenerator.class) + @GeneratedValue(generator = "userGen") private Long regNo; private String lastName; private String firstName; diff --git a/backend/src/test/java/ovh/herisson/Clyde/Repositories/UserRepoTest.java b/backend/src/test/java/ovh/herisson/Clyde/Repositories/UserRepoTest.java index f99ff2f..599ec60 100644 --- a/backend/src/test/java/ovh/herisson/Clyde/Repositories/UserRepoTest.java +++ b/backend/src/test/java/ovh/herisson/Clyde/Repositories/UserRepoTest.java @@ -24,6 +24,7 @@ public class UserRepoTest { @BeforeEach public void setup(){ + userRepo.deleteAll(); User herobrine = new User("brine","hero","admin@admin.com","in your WalLs","ShadowsLand", new GregorianCalendar(2005, 4, 3).getTime(), null, Role.Admin,"admin"); userRepo.save(herobrine); } @@ -34,8 +35,8 @@ public class UserRepoTest { } @Test public void usertest(){ - Assert.assertEquals("brine", userRepo.findById(1).getLastName()); - Assert.assertTrue(new GregorianCalendar(2005, 4, 3).getTime().equals(userRepo.findById(1).getBirthDate())); + Assert.assertEquals("brine", userRepo.findByEmail("admin@admin.com").getLastName()); + Assert.assertTrue(new GregorianCalendar(2005, 4, 3).getTime().equals(userRepo.findByEmail("admin@admin.com").getBirthDate())); } }