@@ -59,6 +59,20 @@ public boolean getEnableSearchFeature() {
59
59
return this .enableSearchFeature ;
60
60
}
61
61
62
+ private String handleSale (Sale sale , HttpSession session , RedirectAttributes redirectAttributes , Runnable action ) {
63
+ sale .setEditing (true ); // set isEditing to true
64
+ action .run ();
65
+ sale .setEditing (false ); // set isEditing to false after action is performed successfully
66
+
67
+ String lastSearchQuery = (String ) session .getAttribute ("lastSearchQuery" );
68
+ if (lastSearchQuery != null && !lastSearchQuery .isEmpty ()) {
69
+ redirectAttributes .addAttribute ("fromSearch" , true );
70
+ return "redirect:/search?q=" + lastSearchQuery ;
71
+ } else {
72
+ return "redirect:/" ;
73
+ }
74
+ }
75
+
62
76
@ RequestMapping ("/" )
63
77
public String viewHomePage (Model model , Principal principal , @ RequestParam (defaultValue = "0" ) int page , HttpSession session ) {
64
78
String lastSearchQuery = (String ) session .getAttribute ("lastSearchQuery" );
@@ -91,6 +105,7 @@ public ModelAndView showNewForm() {
91
105
public ModelAndView showEditForm (@ PathVariable (name = "serialNumber" ) String serialNumber ) {
92
106
ModelAndView mav = new ModelAndView ("edit_form" );
93
107
Sale sale = dao .get (serialNumber );
108
+ sale .setEditing (true );
94
109
mav .addObject ("sale" , sale );
95
110
mav .addObject ("enableSearchFeature" , enableSearchFeature );
96
111
return mav ;
@@ -109,22 +124,20 @@ public String search(@ModelAttribute("q") String query, Model model, HttpSession
109
124
110
125
@ RequestMapping (value = "/save" , method = RequestMethod .POST )
111
126
public String save (@ ModelAttribute ("sale" ) Sale sale , Model model , HttpSession session , RedirectAttributes redirectAttributes ) {
127
+ boolean originalEnableSearchFeature = this .enableSearchFeature ; // store the original value
128
+
112
129
try {
113
130
if (sale .getDate () == null ) {
114
131
sale .setDate (new Date ());
115
132
}
116
133
dao .save (sale );
134
+ sale .setEditing (false );
117
135
} catch (DuplicateKeyException e ) {
118
136
sale .setSerialNumber (null ); // clear the serial number
119
137
model .addAttribute ("sale" , sale ); // add the sale object to the model
120
138
model .addAttribute ("errorMessage" , e .getMessage ());
121
139
122
- String lastSearchQuery = (String ) session .getAttribute ("lastSearchQuery" );
123
- if (lastSearchQuery != null && !lastSearchQuery .isEmpty ()) {
124
- model .addAttribute ("enableSearchFeature" , true ); // set enableSearchFeature to true
125
- } else {
126
- model .addAttribute ("enableSearchFeature" , false ); // set enableSearchFeature to false
127
- }
140
+ model .addAttribute ("enableSearchFeature" , originalEnableSearchFeature ); // use the original value
128
141
129
142
return "new_form" ; // return the form view
130
143
}
@@ -158,41 +171,19 @@ public String loginPost(HttpServletRequest request, Model model) {
158
171
159
172
@ RequestMapping (value = "/update" , method = RequestMethod .POST )
160
173
public String update (@ ModelAttribute ("sale" ) Sale sale , HttpSession session , RedirectAttributes redirectAttributes ) {
161
- dao .update (sale );
162
-
163
- String lastSearchQuery = (String ) session .getAttribute ("lastSearchQuery" );
164
- if (lastSearchQuery != null && !lastSearchQuery .isEmpty ()) {
165
- redirectAttributes .addAttribute ("fromSearch" , true );
166
- return "redirect:/search?q=" + lastSearchQuery ;
167
- } else {
168
- return "redirect:/" ;
169
- }
174
+ return handleSale (sale , session , redirectAttributes , () -> dao .update (sale ));
170
175
}
171
176
172
177
@ RequestMapping ("/delete/{serialNumber}" )
173
178
public String delete (@ PathVariable (name = "serialNumber" ) String serialNumber , HttpSession session , RedirectAttributes redirectAttributes ) {
174
- dao .delete (serialNumber );
175
-
176
- String lastSearchQuery = (String ) session .getAttribute ("lastSearchQuery" );
177
- if (lastSearchQuery != null && !lastSearchQuery .isEmpty ()) {
178
- redirectAttributes .addAttribute ("fromSearch" , true );
179
- return "redirect:/search?q=" + lastSearchQuery ;
180
- } else {
181
- return "redirect:/" ;
182
- }
179
+ Sale sale = dao .get (serialNumber );
180
+ return handleSale (sale , session , redirectAttributes , () -> dao .delete (serialNumber ));
183
181
}
184
182
185
183
@ RequestMapping ("/clear/{serialNumber}" )
186
184
public String clearRecord (@ PathVariable (name = "serialNumber" ) String serialNumber , HttpSession session , RedirectAttributes redirectAttributes ) {
187
- dao .clearRecord (serialNumber );
188
-
189
- String lastSearchQuery = (String ) session .getAttribute ("lastSearchQuery" );
190
- if (lastSearchQuery != null && !lastSearchQuery .isEmpty ()) {
191
- redirectAttributes .addAttribute ("fromSearch" , true );
192
- return "redirect:/search?q=" + lastSearchQuery ;
193
- } else {
194
- return "redirect:/" ;
195
- }
185
+ Sale sale = dao .get (serialNumber );
186
+ return handleSale (sale , session , redirectAttributes , () -> dao .clearRecord (serialNumber ));
196
187
}
197
188
198
189
@ RequestMapping ("/export" )
0 commit comments