-
Notifications
You must be signed in to change notification settings - Fork 8
/
AdminController.java
189 lines (169 loc) · 5.21 KB
/
AdminController.java
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
package com.example.demo.controllers;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.demo.count.*;
import com.example.demo.entities.*;
import com.example.demo.loginCredentials.*;
import com.example.demo.services.*;
import jakarta.validation.Valid;
@Controller
public class AdminController {
@Autowired
private UserServices services;
@Autowired
private AdminServices adminServices;
@Autowired
private ProductServices productServices;
@Autowired
private OrderServices orderServices;
private String email;
private User user;
@PostMapping("/adminLogin")
public String getAllData( @ModelAttribute("adminLogin") AdminLogin login, Model model)
{
String email=login.getEmail();
String password=login.getPassword();
if(adminServices.validateAdminCredentials(email, password))
{
return "redirect:/admin/services";
}
else {
model.addAttribute("error", "Invalid email or password");
return "Login";
}
}
@PostMapping("/userLogin")
public String userLogin( @ModelAttribute("userLogin") UserLogin login,Model model)
{
email=login.getUserEmail();
String password=login.getUserPassword();
if(services.validateLoginCredentials(email, password))
{
user = this.services.getUserByEmail(email);
List<Orders> orders = this.orderServices.getOrdersForUser(user);
model.addAttribute("orders", orders);
model.addAttribute("name", user.getUname());
return "BuyProduct";
}
else
{
model.addAttribute("error2", "Invalid email or password");
return "Login";
}
}
@PostMapping("/product/search")
public String seachHandler(@RequestParam("productName") String name,Model model)
{
Product product=this.productServices.getProductByName(name);
if(product==null)
{
model.addAttribute("message", "SORRY...! Product Unavailable");
model.addAttribute("product", product);
List<Orders> orders = this.orderServices.getOrdersForUser(user);
model.addAttribute("orders", orders);
return "BuyProduct";
}
List<Orders> orders = this.orderServices.getOrdersForUser(user);
model.addAttribute("orders", orders);
model.addAttribute("product", product);
return "BuyProduct";
}
@GetMapping("/admin/services")
public String returnBack(Model model)
{
List<User> users= this.services.getAllUser();
List<Admin>admins=this.adminServices.getAll();
List<Product>products=this.productServices.getAllProducts();
List<Orders> orders = this.orderServices.getOrders();
model.addAttribute("users",users);
model.addAttribute("admins", admins);
model.addAttribute("products", products);
model.addAttribute("orders", orders);
return "Admin_Page";
}
@GetMapping("/addAdmin")
public String addAdminPage()
{
return "Add_Admin";
}
@PostMapping("addingAdmin")
public String addAdmin( @ModelAttribute Admin admin)
{
this.adminServices.addAdmin(admin);
return "redirect:/admin/services";
}
@GetMapping("/updateAdmin/{adminId}")
public String update(@PathVariable("adminId") int id,Model model)
{
Admin admin = this.adminServices.getAdmin(id);
model.addAttribute("admin", admin);
return "Update_Admin";
}
@GetMapping("/updatingAdmin/{id}")
public String updateAdmin(@ModelAttribute Admin admin,@PathVariable("id") int id)
{
this.adminServices.update(admin, id);
return "redirect:/admin/services";
}
@GetMapping("/deleteAdmin/{id}")
public String deleteAdmin(@PathVariable("id") int id)
{
this.adminServices.delete(id);
return "redirect:/admin/services";
}
@GetMapping("/addProduct")
public String addProduct()
{
return "Add_Product";
}
@GetMapping("/updateProduct/{productId}")
public String updateProduct(@PathVariable("productId") int id,Model model)
{
Product product=this.productServices.getProduct(id);
System.out.println(product);
model.addAttribute("product", product);
return "Update_Product";
}
@GetMapping("/addUser")
public String addUser()
{
return "Add_User";
}
@GetMapping("/updateUser/{userId}")
public String updateUserPage(@PathVariable("userId") int id,Model model)
{
User user = this.services.getUser(id);
model.addAttribute("user", user);
return "Update_User";
}
@PostMapping("/product/order")
public String orderHandler(@ModelAttribute() Orders order,Model model)
{
double totalAmount = Logic.countTotal(order.getoPrice(),order.getoQuantity());
order.setTotalAmmout(totalAmount);
order.setUser(user);
Date d=new Date();
order.setOrderDate(d);
this.orderServices.saveOrder(order);
model.addAttribute("amount",totalAmount);
return "Order_success";
}
@GetMapping("/product/back")
public String back(Model model)
{
List<Orders> orders = this.orderServices.getOrdersForUser(user);
model.addAttribute("orders", orders);
return "BuyProduct";
}
}