44import com .agungsetiawan .finalproject .domain .Category ;
55import com .agungsetiawan .finalproject .service .BookService ;
66import com .agungsetiawan .finalproject .service .CartService ;
7+ import com .agungsetiawan .finalproject .service .CartServiceInterface ;
78import com .agungsetiawan .finalproject .service .CategoryService ;
89import java .util .List ;
910import org .springframework .beans .factory .annotation .Autowired ;
2223@ Controller
2324public class CartController {
2425 @ Autowired
25- private CartService cart ;
26+ private CartServiceInterface cart ;
2627
2728 @ Autowired
2829 private BookService bookService ;
2930
3031 @ Autowired
3132 private CategoryService categoryService ;
3233
34+ public CartController (CartServiceInterface cart ,BookService bookService ,CategoryService categoryService ){
35+ this .cart =cart ;
36+ this .bookService =bookService ;
37+ this .categoryService =categoryService ;
38+ }
39+
40+ public CartController (){
41+
42+ }
43+
3344 @ ModelAttribute (value = "listCategory" )
3445 public List <Category > listCategory (){
3546 return categoryService .findAll ();
3647 }
3748
3849 @ ModelAttribute (value = "randomBooks" )
3950 public List <Book > listBook (){
40- return bookService .findAll ();
51+ return bookService .findRandom ();
4152 }
4253
4354 @ ModelAttribute (value = "cartSize" )
@@ -56,21 +67,24 @@ public String showCart(Model model){
5667 @ RequestMapping (value = "public/cart/add/{bookId}" ,method = RequestMethod .POST )
5768 public String addBook (@ PathVariable ("bookId" ) Long bookId , Model model ){
5869 Book book =bookService .findOne (bookId );
59- cart .save (book );
70+ Book bookSaved =cart .save (book );
71+ model .addAttribute ("id" , bookSaved .getId ());
6072 return "redirect:/public/cart" ;
6173 }
6274
6375 @ RequestMapping (value = "public/cart/remove/{bookId}" ,method = RequestMethod .GET )
6476 public String removeBook (@ PathVariable ("bookId" ) Long bookId ,Model model ){
6577 Book book =bookService .findOne (bookId );
66- cart .delete (book );
78+ Book bookDeleted =cart .delete (book );
79+ model .addAttribute ("id" , bookDeleted .getId ());
6780 return "redirect:/public/cart" ;
6881 }
6982
7083 @ RequestMapping (value = "public/cart/update" ,method = RequestMethod .POST )
71- public String update (@ RequestParam (value = "bookId" ) Long bookId ,@ RequestParam (value = "quantity" ) Integer quantity ){
84+ public String update (@ RequestParam (value = "bookId" ) Long bookId ,@ RequestParam (value = "quantity" ) Integer quantity , Model model ){
7285 Book book =bookService .findOne (bookId );
73- cart .update (book , quantity );
86+ Book bookUpdated =cart .update (book , quantity );
87+ model .addAttribute ("id" , bookUpdated .getId ());
7488 return "redirect:/public/cart" ;
7589 }
7690
0 commit comments