Implements the RegNo feature #158

Merged
LeoMoulin merged 3 commits from Leo/masterRegNo into master 2024-04-18 10:32:25 +02:00
6 changed files with 69 additions and 9 deletions
Showing only changes of commit 451944e4fb - Show all commits

View File

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

View File

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

View File

@ -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<User> list){
//S'assure que le compteur est bien a 0
RegNoGenerator.resetCount();
userRepo.saveAll(list);
}
public Iterable<User> getAll(){
return userRepo.findAll();
}

View File

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

View File

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

View File

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