2020-04-29 22:44:16 +02:00
package org.hso.ecommerce.controller.shop ;
2020-04-28 22:41:29 +02:00
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 ;
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-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-08 09:58:29 +02:00
List < Article > commercialArticles = articleRepository . getAdvertisedArticles ( ) ;
2020-05-01 14:40:43 +02:00
model . addAttribute ( " commercialArticles " , commercialArticles ) ;
//check if logged in
boolean isLoggedIn = false ;
boolean hasOrders = false ;
if ( session ! = null & & session . getAttribute ( " id " ) ! = null ) {
long userId = ( long ) session . getAttribute ( " id " ) ;
isLoggedIn = true ;
2020-05-07 15:48:00 +02:00
}
if ( isLoggedIn ) {
List < Article > suggestedArticles = articleRepository . getLastOrderedArticles ( " 4 " ) ;
if ( suggestedArticles . size ( ) > 0 ) {
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
}