Ejemplo n.º 1
0
  @Test
  public void getUserByEmail() {

    User user1 = new User("testEmail", "testPW", "testUser", "testRole");
    userRepository.addUser(user1);
    User user2 = userRepository.getUserByEmail("testEmail");

    assertThat(user1.getUserId(), is(user2.getUserId()));
  }
Ejemplo n.º 2
0
  @RequestMapping(value = "/robots/add", method = RequestMethod.POST)
  public String toAddRobot(
      Model model,
      @ModelAttribute("AddRobot") AddRobot addRobot,
      @RequestParam("image") MultipartFile file,
      HttpServletRequest request) {

    HttpSession session = request.getSession();
    User user = (User) session.getAttribute("user");
    if (user != null) {
      if (!addRobot.getImage().isEmpty()) {
        byte[] bytes;
        try {
          bytes = addRobot.getImage().getBytes();

          String name_file =
              addRobot.getName()
                  + file.getOriginalFilename()
                      .substring(file.getOriginalFilename().lastIndexOf("."));
          name_file = name_file.replace(' ', '_');

          File server_file =
              new File(
                  "D:/Workspace/insta/jee/WorldOfRobots/src/main/webapp/resources/images/robots/"
                      + name_file);

          BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(server_file));
          stream.write(bytes);
          stream.close();

          Robot robot =
              this.robotService.createRobot(
                  addRobot.getTechnologies(),
                  addRobot.getStrong_point(),
                  addRobot.getName(),
                  addRobot.getCreation_date(),
                  name_file);
          this.userService.createRobot(user.getId(), robot.getId());

          request.setAttribute("result", true);
          model.addAttribute("message", "L'ajout de votre robot a bien été enregistrée");
          model.addAttribute("robot", this.robotService.findById(robot.getId()));

        } catch (IOException e) {
          e.printStackTrace();
          request.setAttribute("result", false);
          model.addAttribute("message", "L'ajout de votre robot a échoué");
        }
      } else {
        Robot robot =
            this.robotService.createRobot(
                addRobot.getTechnologies(),
                addRobot.getStrong_point(),
                addRobot.getName(),
                addRobot.getCreation_date(),
                null);

        if (robot != null) {
          session.setAttribute("user", user);
          this.userService.createRobot(user.getId(), robot.getId());
          model.addAttribute("robot", this.robotService.findById(robot.getId()));

          request.setAttribute("result", true);
          model.addAttribute("message", "L'ajout de votre robot a bien été enregistrée");

        } else {
          request.setAttribute("result", false);
          model.addAttribute("message", "L'ajout de votre robot a échoué");
        }
      }
      user = this.userService.findById(user.getId());
      session.setAttribute("user", user);
      return this.cardRobot(model, user.getId_robot(), request);
    } else {
      request.setAttribute("result", false);
      model.addAttribute("message", "Veuillez vous connecter avant d'ajouter un robot");
      model.addAttribute("connexion", new Connexion());
      model.addAttribute("register", new Register());
      return "connexion";
    }
  }