creation of the user when request accepted
This commit is contained in:
		| @ -83,7 +83,7 @@ public class InscriptionController { | |||||||
|         toReturn.put("email",inscriptionRequest.getEmail()); |         toReturn.put("email",inscriptionRequest.getEmail()); | ||||||
|         toReturn.put("birthDate", inscriptionRequest.getBirthDate()); |         toReturn.put("birthDate", inscriptionRequest.getBirthDate()); | ||||||
|         toReturn.put("country", inscriptionRequest.getCountry()); |         toReturn.put("country", inscriptionRequest.getCountry()); | ||||||
|         toReturn.put("curriculum", inscriptionRequest.getCurriculum()); |         toReturn.put("curriculum", inscriptionRequest.getCurriculumId()); | ||||||
|         toReturn.put("state", inscriptionRequest.getState()); |         toReturn.put("state", inscriptionRequest.getState()); | ||||||
|         toReturn.put("profilePictureUrl", inscriptionRequest.getProfilePicture()); |         toReturn.put("profilePictureUrl", inscriptionRequest.getProfilePicture()); | ||||||
|  |  | ||||||
|  | |||||||
| @ -44,7 +44,7 @@ public class LoginController { | |||||||
|         return ResponseEntity.ok().headers(responseHeaders).build(); |         return ResponseEntity.ok().headers(responseHeaders).build(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @PostMapping("/request/register") |     @PostMapping("/register") | ||||||
|     public ResponseEntity<InscriptionRequest> register(@RequestBody InscriptionRequest inscriptionRequest){ |     public ResponseEntity<InscriptionRequest> register(@RequestBody InscriptionRequest inscriptionRequest){ | ||||||
|         return new ResponseEntity<>(authServ.register(inscriptionRequest), HttpStatus.CREATED); |         return new ResponseEntity<>(authServ.register(inscriptionRequest), HttpStatus.CREATED); | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -0,0 +1,7 @@ | |||||||
|  | package ovh.herisson.Clyde.Repositories; | ||||||
|  |  | ||||||
|  | import org.springframework.data.repository.CrudRepository; | ||||||
|  | import ovh.herisson.Clyde.Tables.UserCurriculum; | ||||||
|  |  | ||||||
|  | public interface UserCurriculumRepository extends CrudRepository<UserCurriculum, Long> { | ||||||
|  | } | ||||||
| @ -2,9 +2,7 @@ package ovh.herisson.Clyde.Services; | |||||||
|  |  | ||||||
| import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||||
| import ovh.herisson.Clyde.Tables.*; | import ovh.herisson.Clyde.Tables.*; | ||||||
|  |  | ||||||
| import java.util.Date; | import java.util.Date; | ||||||
| import java.util.HashMap; |  | ||||||
|  |  | ||||||
| @Service | @Service | ||||||
| public class AuthenticatorService { | public class AuthenticatorService { | ||||||
|  | |||||||
| @ -1,20 +1,39 @@ | |||||||
| package ovh.herisson.Clyde.Services; | package ovh.herisson.Clyde.Services; | ||||||
|  |  | ||||||
|  | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||||||
| import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||||
|  | import ovh.herisson.Clyde.Repositories.CurriculumRepository; | ||||||
| import ovh.herisson.Clyde.Repositories.InscriptionRepository; | import ovh.herisson.Clyde.Repositories.InscriptionRepository; | ||||||
|  | import ovh.herisson.Clyde.Repositories.UserCurriculumRepository; | ||||||
|  | import ovh.herisson.Clyde.Repositories.UserRepository; | ||||||
| import ovh.herisson.Clyde.Tables.InscriptionRequest; | import ovh.herisson.Clyde.Tables.InscriptionRequest; | ||||||
| import ovh.herisson.Clyde.Tables.RequestState; | import ovh.herisson.Clyde.Tables.RequestState; | ||||||
|  | import ovh.herisson.Clyde.Tables.User; | ||||||
|  | import ovh.herisson.Clyde.Tables.UserCurriculum; | ||||||
|  |  | ||||||
| @Service | @Service | ||||||
| public class InscriptionService { | public class InscriptionService { | ||||||
|  |  | ||||||
|     InscriptionRepository inscriptionRepo; |     private final InscriptionRepository inscriptionRepo; | ||||||
|  |  | ||||||
|     public InscriptionService(InscriptionRepository inscriptionRepo){ |     private final UserRepository userRepo; | ||||||
|  |  | ||||||
|  |     private final UserCurriculumRepository userCurriculumRepo; | ||||||
|  |  | ||||||
|  |     private final CurriculumRepository curriculumRepo; | ||||||
|  |  | ||||||
|  |     private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     public InscriptionService(InscriptionRepository inscriptionRepo, UserRepository userRepo, UserCurriculumRepository userCurriculumRepo, CurriculumRepository curriculumRepo){ | ||||||
|         this.inscriptionRepo = inscriptionRepo; |         this.inscriptionRepo = inscriptionRepo; | ||||||
|  |         this.userRepo = userRepo; | ||||||
|  |         this.userCurriculumRepo = userCurriculumRepo; | ||||||
|  |         this.curriculumRepo = curriculumRepo; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public InscriptionRequest save(InscriptionRequest inscriptionRequest){ |     public InscriptionRequest save(InscriptionRequest inscriptionRequest){ | ||||||
|  |         inscriptionRequest.setPassword(passwordEncoder.encode(inscriptionRequest.getPassword())); | ||||||
|         return inscriptionRepo.save(inscriptionRequest); |         return inscriptionRepo.save(inscriptionRequest); | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @ -27,23 +46,45 @@ public class InscriptionService { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     public boolean modifyState(long id, RequestState requestState) { |     public boolean modifyState(long id, RequestState requestState) { | ||||||
|         InscriptionRequest inscriptionRequest = getById(id); |         InscriptionRequest inscrRequest = getById(id); | ||||||
|  |  | ||||||
|         if (inscriptionRequest == null) |         if (inscrRequest == null) | ||||||
|             return false; |             return false; | ||||||
|  |  | ||||||
|         // if th state is the same we don't send an email |         // if th state is the same we don't send an email | ||||||
|         if (requestState == inscriptionRequest.getState()) |         if (requestState == inscrRequest.getState()) | ||||||
|             return false; |             return false; | ||||||
|  |  | ||||||
|         /** todo send an email to tell the poster of the inscriptionRequest (inscriptionRequest.getEmail()) |         /** todo send an email to tell the poster of the inscrRequest (inscrRequest.getEmail()) | ||||||
|          *  to notify them that the state of their request changed |          *  to notify them that the state of their request changed | ||||||
|          *  FooEmailFormat toSend = (String.format("Your request state changed from %s to %s"), |          *  FooEmailFormat toSend = (String.format("Your request state changed from %s to %s"), | ||||||
|          *                                              inscriptionRequest.getState(), requestState) |          *                                              inscrRequest.getState(), requestState) | ||||||
|          * FooEmailSender.send(toSend, inscriptionRequest.getEmail()) |          * FooEmailSender.send(toSend, inscrRequest.getEmail()) | ||||||
|          */ |          */ | ||||||
|         inscriptionRequest.setState(requestState); |  | ||||||
|         save(inscriptionRequest); |  | ||||||
|  |         //saves the user from the request if accepted | ||||||
|  |         if (requestState == RequestState.Accepted) | ||||||
|  |         { | ||||||
|  |             if (curriculumRepo.findById(inscrRequest.getCurriculumId()) == null) | ||||||
|  |                 return false; | ||||||
|  |  | ||||||
|  |             User userFromRequest = new User( | ||||||
|  |                     inscrRequest.getLastName(), | ||||||
|  |                     inscrRequest.getFirstName(), | ||||||
|  |                     inscrRequest.getEmail(), | ||||||
|  |                     inscrRequest.getAddress(), | ||||||
|  |                     inscrRequest.getCountry(), | ||||||
|  |                     inscrRequest.getBirthDate(), | ||||||
|  |                     inscrRequest.getProfilePicture(), | ||||||
|  |                     inscrRequest.getPassword() | ||||||
|  |             ); | ||||||
|  |  | ||||||
|  |             userRepo.save(userFromRequest); | ||||||
|  |             userCurriculumRepo.save(new UserCurriculum(userFromRequest, curriculumRepo.findById(inscrRequest.getCurriculumId()))); | ||||||
|  |         } | ||||||
|  |         inscrRequest.setState(requestState); | ||||||
|  |         save(inscrRequest); | ||||||
|         return true; |         return true; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -16,22 +16,20 @@ public class InscriptionRequest { | |||||||
|     private String country; |     private String country; | ||||||
|     private Date birthDate; |     private Date birthDate; | ||||||
|  |  | ||||||
|     @ManyToOne(fetch = FetchType.EAGER) |     private Long curriculumId; | ||||||
|     @JoinColumn(name="Curriculum") |  | ||||||
|     private Curriculum curriculum; |  | ||||||
|     private RequestState state; |     private RequestState state; | ||||||
|     private String profilePicture; |     private String profilePicture; | ||||||
|  |  | ||||||
|     private String password; |     private String password; | ||||||
|     public InscriptionRequest(){} |     public InscriptionRequest(){} | ||||||
|     public InscriptionRequest(String lastName, String firstName, String address, String email, String country, Date birthDate,Curriculum curriculum, RequestState state, String profilePicture, String password){ |     public InscriptionRequest(String lastName, String firstName, String address, String email, String country, Date birthDate,Long curriculumId, RequestState state, String profilePicture, String password){ | ||||||
|         this.lastName = lastName; |         this.lastName = lastName; | ||||||
|         this.firstName = firstName; |         this.firstName = firstName; | ||||||
|         this.address = address; |         this.address = address; | ||||||
|         this.email = email; |         this.email = email; | ||||||
|         this.country = country; |         this.country = country; | ||||||
|         this.birthDate = birthDate; |         this.birthDate = birthDate; | ||||||
|         this.curriculum = curriculum; |         this.curriculumId = curriculumId; | ||||||
|         this.state = state; |         this.state = state; | ||||||
|         this.profilePicture = profilePicture; |         this.profilePicture = profilePicture; | ||||||
|         this.password = password; |         this.password = password; | ||||||
| @ -89,12 +87,12 @@ public class InscriptionRequest { | |||||||
|         this.birthDate = birthDate; |         this.birthDate = birthDate; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public Curriculum getCurriculum() { |     public long getCurriculumId() { | ||||||
|         return curriculum; |         return curriculumId; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public void setCurriculum(Curriculum curriculum) { |     public void setCurriculumId(long curriculum) { | ||||||
|         this.curriculum = curriculum; |         this.curriculumId = curriculum; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public RequestState getState() { |     public RequestState getState() { | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user