1
0
forked from PGL/Clyde

Functionnal RegNo generator wich only needs to be more parametrized

This commit is contained in:
LeoMoulin 2024-03-21 14:32:53 +01:00
parent a1bdae9e83
commit 0c73f6f44d
6 changed files with 61 additions and 21 deletions

View File

@ -15,7 +15,7 @@ import java.util.Date;
public class MockController { public class MockController {
private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
public final UserService userService;
public final UserRepository userRepo; public final UserRepository userRepo;
public final TokenRepository tokenRepo; public final TokenRepository tokenRepo;
public final TokenService tokenService; public final TokenService tokenService;
@ -27,7 +27,8 @@ public class MockController {
ArrayList<User> mockUsers; ArrayList<User> 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.tokenRepo = tokenRepo;
this.userRepo = userRepo; this.userRepo = userRepo;
this.tokenService = tokenService; this.tokenService = tokenService;
@ -45,21 +46,21 @@ public class MockController {
@PostMapping("/mock") @PostMapping("/mock")
public void postMock(){ public void postMock(){
// user part // user part
User herobrine = new User("brine","hero","admin@admin.com","behind","ShadowsLand",new Date(0), null,Role.Admin,passwordEncoder.encode("admin")); 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 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 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 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 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")); 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 infoBab1 = new Curriculum(1,"info");
Curriculum chemistryBab1 = new Curriculum(1,"chemistry"); Curriculum chemistryBab1 = new Curriculum(1,"chemistry");

View File

@ -1,18 +1,15 @@
package ovh.herisson.Clyde; package ovh.herisson.Clyde;
import org.hibernate.HibernateException;
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess; import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator; import org.hibernate.id.IdentifierGenerator;
import java.io.Serializable;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
public class RegNoGenerator implements IdentifierGenerator { public class RegNoGenerator implements IdentifierGenerator {
private final UserSaveCounter usv = UserSaveCounter.getInstance();
@Override @Override
public Object generate(SharedSessionContractImplementor session, Object object) { public Object generate(SharedSessionContractImplementor session, Object object) {
try{ try{
@ -20,16 +17,25 @@ public class RegNoGenerator implements IdentifierGenerator {
Connection conn = jdbccon.obtainConnection(); Connection conn = jdbccon.obtainConnection();
Statement statement = conn.createStatement(); 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); ResultSet set = statement.executeQuery(query);
long resp = 0;
if (set.next()){ if(set.next()){
return set.getLong(1)+1000; resp = set.getLong(1)+1000;
System.out.println(resp);
} }
conn.close();
statement.close();
return resp;
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
return null;
} }
} }

View File

@ -5,13 +5,10 @@ import org.springframework.data.repository.CrudRepository;
import ovh.herisson.Clyde.Tables.User; import ovh.herisson.Clyde.Tables.User;
public interface UserRepository extends CrudRepository<User, Long> { public interface UserRepository extends CrudRepository<User, Long> {
User findById(long id); User findById(long id);
User findByEmail(String email); User findByEmail(String email);
@Query("select u from User u where u.role = ovh.herisson.Clyde.Tables.Role.Teacher") @Query("select u from User u where u.role = ovh.herisson.Clyde.Tables.Role.Teacher")
Iterable<User> findAllTeachers(); Iterable<User> findAllTeachers();

View File

@ -5,13 +5,15 @@ import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.Repositories.UserRepository; import ovh.herisson.Clyde.Repositories.UserRepository;
import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.User; import ovh.herisson.Clyde.Tables.User;
import ovh.herisson.Clyde.UserSaveCounter;
import java.util.*; import java.util.*;
@Service @Service
public class UserService { public class UserService {
private final UserRepository userRepo; private final UserRepository userRepo;
private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
private final UserSaveCounter usc = UserSaveCounter.getInstance();
public UserService(UserRepository userRepo){ public UserService(UserRepository userRepo){
this.userRepo = userRepo; this.userRepo = userRepo;
} }
@ -106,10 +108,19 @@ public class UserService {
} }
public User save(User user){ public User save(User user){
usc.reset();
user.setPassword(passwordEncoder.encode(user.getPassword())); user.setPassword(passwordEncoder.encode(user.getPassword()));
return userRepo.save(user); return userRepo.save(user);
} }
public void saveAll(ArrayList<User> 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<User> getAll(){ public Iterable<User> getAll(){
return userRepo.findAll(); return userRepo.findAll();
} }
@ -131,4 +142,5 @@ public class UserService {
public void delete(User user) { public void delete(User user) {
userRepo.delete(user); userRepo.delete(user);
} }
} }

View File

@ -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;
}
}

View File

@ -1,3 +1,2 @@
spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.sql.init.mode=always