-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathUserController.java
More file actions
222 lines (177 loc) · 6.66 KB
/
UserController.java
File metadata and controls
222 lines (177 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package com.jtspringproject.JtSpringProject.controller;
import com.jtspringproject.JtSpringProject.models.Cart;
import com.jtspringproject.JtSpringProject.models.Product;
import com.jtspringproject.JtSpringProject.models.User;
import java.io.Console;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.jtspringproject.JtSpringProject.services.cartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.jtspringproject.JtSpringProject.services.userService;
import com.jtspringproject.JtSpringProject.services.productService;
import com.jtspringproject.JtSpringProject.services.cartService;
import org.springframework.security.core.Authentication;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@Controller
public class UserController{
private final userService userService;
private final productService productService;
private final PasswordEncoder passwordencoder;
public UserController(userService userService, productService productService,PasswordEncoder passwordencoder) {
this.userService = userService;
this.productService = productService;
this.passwordencoder=passwordencoder;
}
@GetMapping("/register")
public String registerUser()
{
return "register";
}
@GetMapping("/buy")
public String buy()
{
return "buy";
}
@GetMapping("/login")
public ModelAndView userlogin(@RequestParam(required = false) String error) {
ModelAndView mv = new ModelAndView("userLogin");
if ("true".equals(error)) {
mv.addObject("msg", "Please enter correct email and password");
}
return mv;
}
@GetMapping("/")
public ModelAndView indexPage()
{
ModelAndView mView = new ModelAndView("index");
String username = SecurityContextHolder.getContext().getAuthentication().getName();
System.out.println( "home page"+username);
mView.addObject("username", username);
List<Product> products = this.productService.getProducts();
if (products.isEmpty()) {
mView.addObject("msg", "No products are available");
} else {
mView.addObject("products", products);
}
return mView;
}
@GetMapping("/user/products")
public ModelAndView getproduct() {
ModelAndView mView = new ModelAndView("uproduct");
List<Product> products = this.productService.getProducts();
if(products.isEmpty()) {
mView.addObject("msg","No products are available");
}else {
mView.addObject("products",products);
}
return mView;
}
@PostMapping("/userloginvalidate")
public ModelAndView userLoginValidate(@RequestParam String username,
@RequestParam String password) {
System.out.println(username);
System.out.println(password);
User user = userService.getUserByUsername(username);
System.out.println(user.getUsername());
ModelAndView mv = new ModelAndView();
if (user != null && passwordencoder.matches(password, user.getPassword())) {
// Login successful
// Optionally, manually set authentication in Spring Security context
Authentication auth = new UsernamePasswordAuthenticationToken(
user.getUsername(),
null,
List.of(new SimpleGrantedAuthority(user.getRole().replace("ROLE_", "")))
);
SecurityContextHolder.getContext().setAuthentication(auth);
mv.setViewName("redirect:/"); // redirect to home page
} else {
// Login failed
mv.setViewName("userLogin");
mv.addObject("msg", "Invalid username or password");
}
return mv;
}
@RequestMapping(value = "newuserregister", method = RequestMethod.POST)
public ModelAndView newUseRegister(@ModelAttribute User user)
{
// Check if username already exists in database
boolean exists = this.userService.checkUserExists(user.getUsername());
if(!exists) {
System.out.println(user.getEmail());
user.setRole("ROLE_NORMAL");
user.setPassword(passwordencoder.encode( user.getPassword()));
this.userService.addUser(user);
System.out.println("New user created: " + user.getUsername());
ModelAndView mView = new ModelAndView("userLogin");
return mView;
} else {
System.out.println("New user not created - username taken: " + user.getUsername());
ModelAndView mView = new ModelAndView("register");
mView.addObject("msg", user.getUsername() + " is taken. Please choose a different username.");
return mView;
}
}
@GetMapping("/profileDisplay")
public String profileDisplay(Model model, HttpServletRequest request) {
String username = SecurityContextHolder.getContext().getAuthentication().getName();
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");
}
return "updateProfile";
}
//for Learning purpose of model
@GetMapping("/test")
public String Test(Model model)
{
System.out.println("test page");
model.addAttribute("author","jay gajera");
model.addAttribute("id",40);
List<String> friends = new ArrayList<String>();
model.addAttribute("f",friends);
friends.add("xyz");
friends.add("abc");
return "test";
}
// for learning purpose of model and view ( how data is pass to view)
@GetMapping("/test2")
public ModelAndView Test2()
{
System.out.println("test page");
//create modelandview object
ModelAndView mv=new ModelAndView();
mv.addObject("name","jay gajera 17");
mv.addObject("id",40);
mv.setViewName("test2");
List<Integer> list=new ArrayList<Integer>();
list.add(10);
list.add(25);
mv.addObject("marks",list);
return mv;
}
// @GetMapping("carts")
// public ModelAndView getCartDetail()
// {
// ModelAndView mv= new ModelAndView();
// List<Cart>carts = cartService.getCarts();
// }
}