@@ -59,6 +59,20 @@ public boolean getEnableSearchFeature() {
5959 return this .enableSearchFeature ;
6060 }
6161
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+
6276 @ RequestMapping ("/" )
6377 public String viewHomePage (Model model , Principal principal , @ RequestParam (defaultValue = "0" ) int page , HttpSession session ) {
6478 String lastSearchQuery = (String ) session .getAttribute ("lastSearchQuery" );
@@ -91,6 +105,7 @@ public ModelAndView showNewForm() {
91105 public ModelAndView showEditForm (@ PathVariable (name = "serialNumber" ) String serialNumber ) {
92106 ModelAndView mav = new ModelAndView ("edit_form" );
93107 Sale sale = dao .get (serialNumber );
108+ sale .setEditing (true );
94109 mav .addObject ("sale" , sale );
95110 mav .addObject ("enableSearchFeature" , enableSearchFeature );
96111 return mav ;
@@ -109,22 +124,20 @@ public String search(@ModelAttribute("q") String query, Model model, HttpSession
109124
110125 @ RequestMapping (value = "/save" , method = RequestMethod .POST )
111126 public String save (@ ModelAttribute ("sale" ) Sale sale , Model model , HttpSession session , RedirectAttributes redirectAttributes ) {
127+ boolean originalEnableSearchFeature = this .enableSearchFeature ; // store the original value
128+
112129 try {
113130 if (sale .getDate () == null ) {
114131 sale .setDate (new Date ());
115132 }
116133 dao .save (sale );
134+ sale .setEditing (false );
117135 } catch (DuplicateKeyException e ) {
118136 sale .setSerialNumber (null ); // clear the serial number
119137 model .addAttribute ("sale" , sale ); // add the sale object to the model
120138 model .addAttribute ("errorMessage" , e .getMessage ());
121139
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
128141
129142 return "new_form" ; // return the form view
130143 }
@@ -158,41 +171,19 @@ public String loginPost(HttpServletRequest request, Model model) {
158171
159172 @ RequestMapping (value = "/update" , method = RequestMethod .POST )
160173 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 ));
170175 }
171176
172177 @ RequestMapping ("/delete/{serialNumber}" )
173178 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 ));
183181 }
184182
185183 @ RequestMapping ("/clear/{serialNumber}" )
186184 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 ));
196187 }
197188
198189 @ RequestMapping ("/export" )
0 commit comments