-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathAdminController.java
More file actions
264 lines (223 loc) · 8.91 KB
/
AdminController.java
File metadata and controls
264 lines (223 loc) · 8.91 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package com.jtspringproject.JtSpringProject.controller;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import DTO.productdto;
import com.jtspringproject.JtSpringProject.models.Logs;
import com.jtspringproject.JtSpringProject.services.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
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.models.Category;
import com.jtspringproject.JtSpringProject.models.Product;
import com.jtspringproject.JtSpringProject.models.User;
import com.jtspringproject.JtSpringProject.services.CategoryService;
import com.jtspringproject.JtSpringProject.services.ProductService;
import com.jtspringproject.JtSpringProject.services.UserService;
@Controller
@RequestMapping("/admin")
public class AdminController {
private final UserService userService;
private final CategoryService categoryService;
private final ProductService productService;
private final LogService logservice;
@Autowired
public AdminController(UserService userService, CategoryService categoryService, ProductService productService, LogService logservice) {
this.userService = userService;
this.categoryService = categoryService;
this.productService = productService;
this.logservice= logservice;
}
@GetMapping("/logs")
public List<Logs> getalllogs(){
return logservice.getalllogs();
}
@PostMapping("/setprice")
@ResponseBody
public void setprice( @RequestBody productdto product){
this.productService.updateProductPrice(product.id,product);
}
@GetMapping("/index")
public String index(Model model) {
String username = SecurityContextHolder.getContext().getAuthentication().getName();
model.addAttribute("username", username);
return "index";
}
@GetMapping("login")
public ModelAndView adminlogin(@RequestParam(required = false) String error) {
ModelAndView mv = new ModelAndView("adminlogin");
if ("true".equals(error)) {
mv.addObject("msg", "Invalid username or password. Please try again.");
}
return mv;
}
@GetMapping( value={"/","Dashboard"})
public ModelAndView adminHome(Model model) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
ModelAndView mv = new ModelAndView("adminHome");
mv.addObject("admin", authentication.getName());
return mv;
}
@GetMapping("categories")
public ModelAndView getcategory() {
ModelAndView mView = new ModelAndView("categories");
List<Category> categories = this.categoryService.getCategories();
mView.addObject("categories", categories);
return mView;
}
@PostMapping("/categories")
public String addCategory(@RequestParam("categoryname") String category_name)
{
System.out.println(category_name);
Category category = this.categoryService.addCategory(category_name);
if(category.getName().equals(category_name)) {
return "redirect:categories";
}else {
return "redirect:categories";
}
}
@GetMapping("categories/delete")
public String removeCategoryDb(@RequestParam("id") int id)
{
this.categoryService.deleteCategory(id);
return "redirect:/admin/categories";
}
@GetMapping("categories/update")
public String updateCategory(@RequestParam("categoryid") int id, @RequestParam("categoryname") String categoryname)
{
Category category = this.categoryService.updateCategory(id, categoryname);
return "redirect:/admin/categories";
}
// --------------------------Remaining --------------------
@GetMapping("products")
public ModelAndView getproduct() {
ModelAndView mView = new ModelAndView("products");
List<Product> products = this.productService.getProducts();
if (products.isEmpty()) {
mView.addObject("msg", "No products are available");
} else {
mView.addObject("products", products);
}
return mView;
}
@GetMapping("products/add")
public ModelAndView addProduct() {
ModelAndView mView = new ModelAndView("productsAdd");
List<Category> categories = this.categoryService.getCategories();
mView.addObject("categories",categories);
return mView;
}
@RequestMapping(value = "products/add",method=RequestMethod.POST)
public String addProduct(@RequestParam("name") String name,@RequestParam("categoryid") int categoryId ,@RequestParam("price") int price,@RequestParam("weight") int weight, @RequestParam("quantity")int quantity,@RequestParam("description") String description,@RequestParam("productImage") String productImage) {
System.out.println(categoryId);
Category category = this.categoryService.getCategory(categoryId);
Product product = new Product();
product.setId(categoryId);
product.setName(name);
product.setCategory(category);
product.setDescription(description);
product.setPrice(price);
product.setImage(productImage);
product.setWeight(weight);
product.setQuantity(quantity);
this.productService.addProduct(product);
return "redirect:/admin/products";
}
@GetMapping("products/update/{id}")
public ModelAndView updateproduct(@PathVariable("id") int id) {
ModelAndView mView = new ModelAndView("productsUpdate");
Product product = this.productService.getProduct(id);
List<Category> categories = this.categoryService.getCategories();
mView.addObject("categories",categories);
mView.addObject("product", product);
return mView;
}
@RequestMapping(value = "products/update/{id}",method=RequestMethod.POST)
public String updateProduct(@PathVariable("id") int id ,@RequestParam("name") String name,@RequestParam("categoryid") int categoryId ,@RequestParam("price") int price,@RequestParam("weight") int weight, @RequestParam("quantity")int quantity,@RequestParam("description") String description,@RequestParam("productImage") String productImage)
{
// this.productService.updateProduct();
return "redirect:/admin/products";
}
@GetMapping("products/delete")
public String removeProduct(@RequestParam("id") int id)
{
this.productService.deleteProduct(id);
return "redirect:/admin/products";
}
@PostMapping("products")
public String postproduct() {
return "redirect:/admin/categories";
}
@GetMapping("customers")
public ModelAndView getCustomerDetail() {
ModelAndView mView = new ModelAndView("displayCustomers");
List<User> users = this.userService.getUsers();
mView.addObject("customers", users);
return mView;
}
@GetMapping("profileDisplay")
public String profileDisplay(Model model) {
String displayusername,displaypassword,displayemail,displayaddress;
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ecommjava","root","");
PreparedStatement stmt = con.prepareStatement("select * from users where username = ?"+";");
String username = SecurityContextHolder.getContext().getAuthentication().getName();
stmt.setString(1, username);
ResultSet rst = stmt.executeQuery();
if(rst.next())
{
int userid = rst.getInt(1);
displayusername = rst.getString(2);
displayemail = rst.getString(3);
displaypassword = rst.getString(4);
displayaddress = rst.getString(5);
model.addAttribute("userid",userid);
model.addAttribute("username",displayusername);
model.addAttribute("email",displayemail);
model.addAttribute("password",displaypassword);
model.addAttribute("address",displayaddress);
}
}
catch(Exception e)
{
System.out.println("Exception:"+e);
}
System.out.println("Hello");
return "updateProfile";
}
@RequestMapping(value = "updateuser",method=RequestMethod.POST)
public String updateUserProfile(@RequestParam("userid") int userid,@RequestParam("username") String username, @RequestParam("email") String email, @RequestParam("password") String password, @RequestParam("address") String address)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ecommjava","root","");
PreparedStatement pst = con.prepareStatement("update users set username= ?,email = ?,password= ?, address= ? where uid = ?;");
pst.setString(1, username);
pst.setString(2, email);
pst.setString(3, password);
pst.setString(4, address);
pst.setInt(5, userid);
int i = pst.executeUpdate();
Authentication newAuthentication = new UsernamePasswordAuthenticationToken(
username,
password,
SecurityContextHolder.getContext().getAuthentication().getAuthorities());
SecurityContextHolder.getContext().setAuthentication(newAuthentication);
}
catch(Exception e)
{
System.out.println("Exception:"+e);
}
return "redirect:index";
}
}