Skip to content

Commit

Permalink
Add editing functionality to Sale class and handleSale method
Browse files Browse the repository at this point in the history
  • Loading branch information
tsviz committed Feb 2, 2024
1 parent dc58074 commit 7d764eb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
57 changes: 24 additions & 33 deletions src/main/java/net/codejava/AppController.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ public boolean getEnableSearchFeature() {
return this.enableSearchFeature;
}

private String handleSale(Sale sale, HttpSession session, RedirectAttributes redirectAttributes, Runnable action) {
sale.setEditing(true); // set isEditing to true
action.run();
sale.setEditing(false); // set isEditing to false after action is performed successfully

String lastSearchQuery = (String) session.getAttribute("lastSearchQuery");
if (lastSearchQuery != null && !lastSearchQuery.isEmpty()) {
redirectAttributes.addAttribute("fromSearch", true);
return "redirect:/search?q=" + lastSearchQuery;
} else {
return "redirect:/";
}
}

@RequestMapping("/")
public String viewHomePage(Model model , Principal principal, @RequestParam(defaultValue = "0") int page, HttpSession session) {
String lastSearchQuery = (String) session.getAttribute("lastSearchQuery");
Expand Down Expand Up @@ -91,6 +105,7 @@ public ModelAndView showNewForm() {
public ModelAndView showEditForm(@PathVariable(name = "serialNumber") String serialNumber) {
ModelAndView mav = new ModelAndView("edit_form");
Sale sale = dao.get(serialNumber);
sale.setEditing(true);
mav.addObject("sale", sale);
mav.addObject("enableSearchFeature", enableSearchFeature);
return mav;
Expand All @@ -109,22 +124,20 @@ public String search(@ModelAttribute("q") String query, Model model, HttpSession

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@ModelAttribute("sale") Sale sale, Model model, HttpSession session, RedirectAttributes redirectAttributes) {
boolean originalEnableSearchFeature = this.enableSearchFeature; // store the original value

try {
if (sale.getDate() == null) {
sale.setDate(new Date());
}
dao.save(sale);
sale.setEditing(false);
} catch (DuplicateKeyException e) {
sale.setSerialNumber(null); // clear the serial number
model.addAttribute("sale", sale); // add the sale object to the model
model.addAttribute("errorMessage", e.getMessage());

String lastSearchQuery = (String) session.getAttribute("lastSearchQuery");
if (lastSearchQuery != null && !lastSearchQuery.isEmpty()) {
model.addAttribute("enableSearchFeature", true); // set enableSearchFeature to true
} else {
model.addAttribute("enableSearchFeature", false); // set enableSearchFeature to false
}
model.addAttribute("enableSearchFeature", originalEnableSearchFeature); // use the original value

return "new_form"; // return the form view
}
Expand Down Expand Up @@ -158,41 +171,19 @@ public String loginPost(HttpServletRequest request, Model model) {

@RequestMapping(value = "/update", method = RequestMethod.POST)
public String update(@ModelAttribute("sale") Sale sale, HttpSession session, RedirectAttributes redirectAttributes) {
dao.update(sale);

String lastSearchQuery = (String) session.getAttribute("lastSearchQuery");
if (lastSearchQuery != null && !lastSearchQuery.isEmpty()) {
redirectAttributes.addAttribute("fromSearch", true);
return "redirect:/search?q=" + lastSearchQuery;
} else {
return "redirect:/";
}
return handleSale(sale, session, redirectAttributes, () -> dao.update(sale));
}

@RequestMapping("/delete/{serialNumber}")
public String delete(@PathVariable(name = "serialNumber") String serialNumber, HttpSession session, RedirectAttributes redirectAttributes) {
dao.delete(serialNumber);

String lastSearchQuery = (String) session.getAttribute("lastSearchQuery");
if (lastSearchQuery != null && !lastSearchQuery.isEmpty()) {
redirectAttributes.addAttribute("fromSearch", true);
return "redirect:/search?q=" + lastSearchQuery;
} else {
return "redirect:/";
}
Sale sale = dao.get(serialNumber);
return handleSale(sale, session, redirectAttributes, () -> dao.delete(serialNumber));
}

@RequestMapping("/clear/{serialNumber}")
public String clearRecord(@PathVariable(name = "serialNumber") String serialNumber, HttpSession session, RedirectAttributes redirectAttributes) {
dao.clearRecord(serialNumber);

String lastSearchQuery = (String) session.getAttribute("lastSearchQuery");
if (lastSearchQuery != null && !lastSearchQuery.isEmpty()) {
redirectAttributes.addAttribute("fromSearch", true);
return "redirect:/search?q=" + lastSearchQuery;
} else {
return "redirect:/";
}
Sale sale = dao.get(serialNumber);
return handleSale(sale, session, redirectAttributes, () -> dao.clearRecord(serialNumber));
}

@RequestMapping("/export")
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/net/codejava/Sale.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class Sale {
private String item;
private int quantity;
private float amount;
private boolean isEditing;

@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date date;
Expand Down Expand Up @@ -70,6 +71,14 @@ public void setDate(final Date date) {
this.date = date;
}

public boolean isEditing() {
return isEditing;
}

public void setEditing(boolean isEditing) {
this.isEditing = isEditing;
}

@Override
public String toString() {
return "Sale [serial_number=" + serialNumber + ", item=" + item + ", quantity=" + quantity + ", amount=" + amount + ", date=" + date + "]";
Expand Down

0 comments on commit 7d764eb

Please sign in to comment.