Delete userSaveCounter and replace it by a static method
All checks were successful
Build and test backend / Build-backend (pull_request) Successful in 2m6s
Build and test FrontEnd / Build-frontend (pull_request) Successful in 24s

This commit is contained in:
LeoMoulin 2024-03-23 18:12:06 +01:00
parent 49b99e14c9
commit c0890ac76a
3 changed files with 11 additions and 34 deletions

View File

@ -10,7 +10,7 @@ import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
public class RegNoGenerator implements IdentifierGenerator { public class RegNoGenerator implements IdentifierGenerator {
private final UserSaveCounter usv = UserSaveCounter.getInstance(); private static int count = 0;
@Override @Override
public Object generate(SharedSessionContractImplementor session, Object object) { public Object generate(SharedSessionContractImplementor session, Object object) {
try{ try{
@ -19,16 +19,15 @@ public class RegNoGenerator implements IdentifierGenerator {
Statement statement = conn.createStatement(); Statement statement = conn.createStatement();
int n = usv.getCount();
Calendar c = new GregorianCalendar(); Calendar c = new GregorianCalendar();
int y = c.get(Calendar.YEAR); int y = c.get(Calendar.YEAR);
String query = "select count(reg_no) + "+n+" from Users where reg_no/10000 = " + y%1000; String query = "select count(reg_no) + "+count+" from Users where reg_no/10000 = " + y%1000;
ResultSet set = statement.executeQuery(query); ResultSet set = statement.executeQuery(query);
long resp = 0; long resp = 0;
if(set.next()){ if(set.next()){
resp = set.getLong(1)+((y%1000)*10000); resp = set.getLong(1)+((y%1000)*10000);
usv.increment(); count += 1;
} }
conn.close(); conn.close();
@ -40,4 +39,8 @@ public class RegNoGenerator implements IdentifierGenerator {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public static void resetCount(){
count = 0;
}
} }

View File

@ -2,10 +2,10 @@ package ovh.herisson.Clyde.Services;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.RegNoGenerator;
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.*;
@ -13,7 +13,6 @@ import java.util.*;
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;
} }
@ -108,17 +107,17 @@ public class UserService {
} }
public User save(User user){ public User save(User user){
usc.reset(); RegNoGenerator.resetCount();
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){ public void saveAll(ArrayList<User> list){
//S'assure que le compteur est bien a 0 //S'assure que le compteur est bien a 0
usc.reset(); RegNoGenerator.resetCount();
userRepo.saveAll(list); userRepo.saveAll(list);
//Reset le compteur a zero pour les futurs ajouts //Reset le compteur a zero pour les futurs ajouts
usc.reset(); RegNoGenerator.resetCount();
} }
public Iterable<User> getAll(){ public Iterable<User> getAll(){

View File

@ -1,25 +0,0 @@
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;
}
}