diff --git a/JtProject/src/main/java/com/jtspringproject/JtSpringProject/controller/UserController.java b/JtProject/src/main/java/com/jtspringproject/JtSpringProject/controller/UserController.java index c5643bbb..687af51f 100644 --- a/JtProject/src/main/java/com/jtspringproject/JtSpringProject/controller/UserController.java +++ b/JtProject/src/main/java/com/jtspringproject/JtSpringProject/controller/UserController.java @@ -14,6 +14,7 @@ import java.util.List; import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jtspringproject.JtSpringProject.services.cartService; @@ -101,6 +102,7 @@ public ModelAndView getproduct() { return mView; } + @RequestMapping(value = "newuserregister", method = RequestMethod.POST) public ModelAndView newUseRegister(@ModelAttribute User user) { @@ -122,6 +124,43 @@ public ModelAndView newUseRegister(@ModelAttribute User user) return mView; } } + + @GetMapping("/profileDisplay") + public String profileDisplay(Model model, HttpServletRequest request) { + try { + Cookie[] cookies = request.getCookies(); + String username = null; + + if (cookies != null) { + for (Cookie cookie : cookies) { + if ("username".equals(cookie.getName())) { + username = cookie.getValue(); + break; + } + } + } + + if (username != null) { + User user = userService.getUserByUsername(username); + + if (user != null) { + model.addAttribute("userid", user.getId()); + model.addAttribute("username", user.getUsername()); + model.addAttribute("email", user.getEmail()); + model.addAttribute("password", user.getPassword()); + model.addAttribute("address", user.getAddress()); + } else { + model.addAttribute("msg", "User not found"); + } + } else { + model.addAttribute("msg", "Username not found in cookies"); + } + } catch (Exception e) { + System.out.println("Exception: " + e); + model.addAttribute("msg", "An error occurred while retrieving user details"); + } + return "updateProfile"; + } diff --git a/JtProject/src/main/java/com/jtspringproject/JtSpringProject/dao/userDao.java b/JtProject/src/main/java/com/jtspringproject/JtSpringProject/dao/userDao.java index f2478a97..9bbf9dbd 100644 --- a/JtProject/src/main/java/com/jtspringproject/JtSpringProject/dao/userDao.java +++ b/JtProject/src/main/java/com/jtspringproject/JtSpringProject/dao/userDao.java @@ -67,4 +67,17 @@ public boolean userExists(String username) { query.setParameter("username",username); return !query.getResultList().isEmpty(); } + + @Transactional + public User getUserByUsername(String username) { + Query query = sessionFactory.getCurrentSession().createQuery("from User where username = :username", User.class); + query.setParameter("username", username); + + try { + return query.getSingleResult(); + } catch (Exception e) { + System.out.println(e.getMessage()); + return null; + } + } } diff --git a/JtProject/src/main/java/com/jtspringproject/JtSpringProject/services/userService.java b/JtProject/src/main/java/com/jtspringproject/JtSpringProject/services/userService.java index f234c069..92f48166 100644 --- a/JtProject/src/main/java/com/jtspringproject/JtSpringProject/services/userService.java +++ b/JtProject/src/main/java/com/jtspringproject/JtSpringProject/services/userService.java @@ -35,4 +35,8 @@ public User checkLogin(String username,String password) { public boolean checkUserExists(String username) { return this.userDao.userExists(username); } + + public User getUserByUsername(String username) { + return userDao.getUserByUsername(username); + } }