2020-04-29 22:44:16 +02:00
package org.hso.ecommerce.controller.shop ;
2020-04-28 22:41:29 +02:00
2020-05-08 10:37:42 +02:00
import org.hso.ecommerce.action.shop.GetRandomArticlesAction ;
2020-05-01 12:19:52 +02:00
import org.hso.ecommerce.entities.shop.Article ;
2020-05-07 15:48:00 +02:00
import org.hso.ecommerce.repos.shop.ArticleRepository ;
2020-05-15 12:54:28 +02:00
import org.hso.ecommerce.repos.warehouse.WarehouseBookingPositionSlotEntryRepository ;
2020-05-07 15:48:00 +02:00
import org.springframework.beans.factory.annotation.Autowired ;
2020-04-28 22:41:29 +02:00
import org.springframework.stereotype.Controller ;
2020-05-01 12:19:52 +02:00
import org.springframework.ui.Model ;
import org.springframework.web.bind.annotation.GetMapping ;
import org.springframework.web.bind.annotation.RequestMapping ;
2020-05-01 14:40:43 +02:00
import javax.servlet.http.HttpSession ;
2020-05-07 15:48:00 +02:00
import java.util.List ;
2020-04-28 22:41:29 +02:00
@Controller
2020-05-01 12:19:52 +02:00
@RequestMapping ( " / " )
2020-05-01 10:48:12 +02:00
public class ShopIndexController {
2020-05-01 12:19:52 +02:00
2020-05-07 15:48:00 +02:00
@Autowired
private final ArticleRepository articleRepository = null ;
2020-05-15 12:54:28 +02:00
@Autowired
private final WarehouseBookingPositionSlotEntryRepository warehouseBookingPositionSlotEntryRepository = null ;
2020-05-01 12:19:52 +02:00
@GetMapping ( " / " )
public String home ( ) {
return " redirect:/shop/ " ;
}
@GetMapping ( " /shop/ " )
2020-05-01 14:40:43 +02:00
public String shop ( Model model , HttpSession session ) {
2020-05-17 12:59:03 +02:00
List < Article > commercialArticles = GetRandomArticlesAction . getRandomArticles ( 8 , articleRepository . getAdvertisedArticles ( ) ) ; //get random advertised Articles
2020-05-01 14:40:43 +02:00
model . addAttribute ( " commercialArticles " , commercialArticles ) ;
boolean isLoggedIn = false ;
boolean hasOrders = false ;
2020-05-10 15:28:39 +02:00
2020-05-17 12:59:03 +02:00
if ( session ! = null & & session . getAttribute ( " userId " ) ! = null ) { //check if logged in
2020-05-10 15:03:45 +02:00
long userId = ( long ) session . getAttribute ( " userId " ) ;
2020-05-01 14:40:43 +02:00
isLoggedIn = true ;
2020-05-07 15:48:00 +02:00
2020-05-10 15:28:39 +02:00
List < Article > suggestedArticles = articleRepository . getOrderedArticles ( userId ) ;
2020-05-17 12:59:03 +02:00
suggestedArticles = suggestedArticles . size ( ) > 3 ? suggestedArticles . subList ( 0 , 4 ) : suggestedArticles ; //only latest 4 ordered articles
2020-05-08 10:37:42 +02:00
if ( suggestedArticles . size ( ) > 0 ) {
2020-05-07 15:48:00 +02:00
model . addAttribute ( " suggestedArticles " , suggestedArticles ) ;
hasOrders = true ;
2020-05-01 14:40:43 +02:00
}
}
2020-05-07 15:48:00 +02:00
2020-05-01 14:40:43 +02:00
model . addAttribute ( " isLoggedIn " , isLoggedIn ) ;
model . addAttribute ( " hasOrders " , hasOrders ) ;
2020-05-01 12:19:52 +02:00
return " shop/index " ;
}
@GetMapping ( " /about " )
public String about ( ) {
return " about " ;
}
@GetMapping ( " /terms " )
public String terms ( ) {
return " terms " ;
}
@GetMapping ( " /privacy " )
public String privacy ( ) {
return " privacy " ;
}
2020-04-28 22:41:29 +02:00
}