Skip to content

Commit

Permalink
update method getmodules
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedlouay committed Feb 11, 2022
1 parent 808eb05 commit 7fa8d8b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
23 changes: 12 additions & 11 deletions src/main/java/Model/Controllers/ModuleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public class ModuleController {
@GetMapping("/who")
public ArrayList<String> getPersonne(Principal principal) {
ArrayList<String> data = new ArrayList<>();
data.add("louay");
data.add(principal.getName());
data.add("la personne connectée est " +principal.getName());
return data ;

}
Expand All @@ -63,16 +62,16 @@ public ArrayList<String> getPersonne(Principal principal) {
@GetMapping("/{idUser}")
public ArrayList<String> getmodules(@PathVariable Long idUser){
ArrayList<String> userModules = new ArrayList<>();
userModules.add("test1");
Optional<User> ouser = userRepository.findById(idUser);
if (!ouser.isPresent()) {
return null;
}
User user= ouser.get();

Set<Module> modules = user.getModules();

for (Module module:modules) {
userModules.add(module.name + "/" + module.id);
userModules.add("module name : "+module.name + "| id : " + module.id);
}
return userModules;
}
Expand Down Expand Up @@ -179,22 +178,24 @@ public ResponseEntity<?> addUser(Principal principal, @PathVariable long id, @Pa
User actor = userRepository.findByUsername(principal.getName()).get();

Set<User> participants = module.getParticipants();
if ((participants.isEmpty() && actor.equals(user))
|| participants.contains(actor)) {
// if ((participants.isEmpty() && actor.equals(user))
// || participants.contains(actor)) {
// verifie si user n'apartient pas déjà à participants
if(!participants.contains(user)) {
participants.add(user);
user.getModules().add(module);
}else{
return ResponseEntity
.badRequest()
.body(new MessageResponse("Error: User y apartient deja !"));
}
} else {
return ResponseEntity
.badRequest()
.body(new MessageResponse("Error: Not allowed to add user!"));
}
// } else {
// return ResponseEntity
// .badRequest()
// .body(new MessageResponse("Error: Not allowed to add user!"));
// }
moduleRepository.save(module);
userRepository.save(user);
return ResponseEntity.ok(new MessageResponse("User successfully added to module!"));
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/Model/Documents/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public class Module {
inverseJoinColumns = @JoinColumn(name = "ressource_id"))
public Set<Ressource> ressources = new HashSet<>();

@ManyToMany(fetch = FetchType.LAZY)
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable( name = "module_user",
joinColumns = @JoinColumn(name = "module_id"),
inverseJoinColumns = @JoinColumn(name = "user_id"))
public Set<User> users;
public Set<User> users = new HashSet<>();

public Module() {
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/Model/Repositories/ModuleRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@
public interface ModuleRepository extends JpaRepository<Module, Long> {
Optional<Module> findByName(String name);

Optional<Module> findById(String name);
}

0 comments on commit 7fa8d8b

Please sign in to comment.