Skip to content

Commit

Permalink
Merge pull request #73 from shubhranshii/master2
Browse files Browse the repository at this point in the history
added profileDisplay mapping in UserController
  • Loading branch information
NisargPipaliya authored Aug 25, 2024
2 parents 27f2c1e + f5aa2a5 commit 70170ca
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -101,6 +102,7 @@ public ModelAndView getproduct() {

return mView;
}

@RequestMapping(value = "newuserregister", method = RequestMethod.POST)
public ModelAndView newUseRegister(@ModelAttribute User user)
{
Expand All @@ -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";
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,17 @@ public boolean userExists(String username) {
query.setParameter("username",username);
return !query.getResultList().isEmpty();
}

@Transactional
public User getUserByUsername(String username) {
Query<User> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 70170ca

Please sign in to comment.