package com.utopiaindustries.controller; import com.utopiaindustries.auth.AdminRole; import com.utopiaindustries.model.ctp.InventoryAccount; import com.utopiaindustries.service.InventoryAccountService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.mvc.support.RedirectAttributes; @Controller @AdminRole @RequestMapping( "/inventory-accounts" ) public class InventoryAccountController { private final InventoryAccountService inventoryAccountService; public InventoryAccountController(InventoryAccountService inventoryAccountService) { this.inventoryAccountService = inventoryAccountService; } @GetMapping public String showInventoryAccounts( Model model ){ model.addAttribute( "accounts", inventoryAccountService.findInventoryAccounts() ); return "inventory-account-list"; } @GetMapping( "/new" ) public String showNewForm( Model model ){ model.addAttribute( "account", inventoryAccountService.createNewAccount() ); model.addAttribute("processes", inventoryAccountService.getAllProcess() ); return "inventory-account-add"; } @GetMapping( "/edit/{id}" ) public String showEditForm( @PathVariable long id, Model model ){ model.addAttribute("account", inventoryAccountService.find(id) ); model.addAttribute("processes", inventoryAccountService.getAllProcess() ); return "inventory-account-edit"; } @PostMapping( "/edit" ) public String saveNewForm( @ModelAttribute InventoryAccount inventoryAccount, RedirectAttributes redirectAttributes ){ try { inventoryAccountService.saveAccount( inventoryAccount ); redirectAttributes.addFlashAttribute("success", "Inventory Account Successfully Added" ); } catch ( Exception e ){ redirectAttributes.addFlashAttribute("error", e.getMessage() ); } return "redirect:/inventory-accounts"; } @PostMapping( "/edit/{id}" ) public String saveNewForm( @ModelAttribute InventoryAccount inventoryAccount, @PathVariable long id, RedirectAttributes redirectAttributes ){ try { inventoryAccountService.saveAccount( inventoryAccount ); redirectAttributes.addFlashAttribute("success", "Inventory Account Successfully Added" ); } catch ( Exception e ){ redirectAttributes.addFlashAttribute("error", e.getMessage() ); } return "redirect:/inventory-accounts"; } }