2020-04-29 22:44:16 +02:00
package org.hso.ecommerce.controller.shop ;
2020-04-28 22:41:29 +02:00
2020-05-07 15:48:00 +02:00
import org.apache.tomcat.util.http.fileupload.IOUtils ;
2020-05-08 10:37:42 +02:00
import org.hso.ecommerce.action.shop.GetRandomArticlesAction ;
2020-05-02 14:38:40 +02:00
import org.hso.ecommerce.entities.shop.Article ;
2020-05-05 17:24:25 +02:00
import org.hso.ecommerce.entities.shop.ShoppingCart ;
import org.hso.ecommerce.repos.shop.ArticleRepository ;
2020-05-14 15:43:16 +02:00
import org.hso.ecommerce.repos.shop.CategoryRepository ;
2020-05-08 13:01:29 +02:00
import org.hso.ecommerce.repos.warehouse.WarehouseBookingPositionSlotEntryRepository ;
2020-05-05 17:24:25 +02:00
import org.springframework.beans.factory.annotation.Autowired ;
2020-05-07 15:48:00 +02:00
import org.springframework.http.MediaType ;
2020-04-28 22:41:29 +02:00
import org.springframework.stereotype.Controller ;
2020-05-02 14:38:40 +02:00
import org.springframework.ui.Model ;
import org.springframework.web.bind.annotation.* ;
2020-05-05 23:34:17 +02:00
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;
2020-05-02 14:38:40 +02:00
import javax.servlet.http.HttpSession ;
2020-06-14 15:54:56 +02:00
import java.io.File ;
2020-05-07 15:48:00 +02:00
import java.io.FileInputStream ;
import java.io.IOException ;
import java.io.InputStream ;
2020-05-02 14:38:40 +02:00
import java.util.List ;
2020-04-28 22:41:29 +02:00
@Controller
2020-05-02 14:38:40 +02:00
@RequestMapping ( " /shop/articles " )
2020-05-01 10:48:12 +02:00
public class ShopArticleController {
2020-05-02 14:38:40 +02:00
2020-05-05 17:24:25 +02:00
@Autowired
private final ArticleRepository articleRepository = null ;
2020-05-08 13:01:29 +02:00
@Autowired
private final WarehouseBookingPositionSlotEntryRepository warehouseBookingPositionSlotEntryRepository = null ;
2020-05-14 15:43:16 +02:00
@Autowired
private final CategoryRepository categoryRepository = null ;
2020-05-02 14:38:40 +02:00
@GetMapping ( " /{id} " )
2020-05-07 15:48:00 +02:00
public String shopArticlesById ( Model model ,
2020-06-25 16:52:13 +02:00
@RequestAttribute ( value = " shoppingCart " ) ShoppingCart shoppingCart ,
2020-05-07 15:48:00 +02:00
@PathVariable ( " id " ) Long id ,
HttpServletRequest request ,
HttpServletResponse response
) {
2020-05-15 12:23:32 +02:00
model . addAttribute ( " categories " , categoryRepository . getCategories ( ) ) ; //for sidebar
2020-05-14 15:43:16 +02:00
2020-05-07 15:48:00 +02:00
Article article = articleRepository . findArticleById ( id ) ;
2020-05-02 14:38:40 +02:00
2020-05-08 10:37:42 +02:00
if ( article = = null ) {
2020-05-07 15:48:00 +02:00
request . setAttribute ( " error " , " Der Artikel wurde nicht gefunden. " ) ;
response . setStatus ( HttpServletResponse . SC_NOT_FOUND ) ;
return " error/404 " ;
}
model . addAttribute ( " article " , article ) ;
2020-05-02 14:38:40 +02:00
2020-06-25 16:52:13 +02:00
int inStock = warehouseBookingPositionSlotEntryRepository
2020-06-24 02:05:25 +02:00
. getByArticle ( id )
. stream ( )
. mapToInt ( e - > e . newSumSlot )
2020-06-25 16:52:13 +02:00
. sum ( ) ;
model . addAttribute ( " inStock " , Math . min ( inStock , 10 ) ) ;
model . addAttribute ( " inCart " , shoppingCart . getArticleCount ( article ) ) ;
2020-05-02 14:38:40 +02:00
2020-05-17 12:59:03 +02:00
List < Article > commercialArticles = GetRandomArticlesAction . getRandomArticles ( 3 , articleRepository . getAdvertisedArticles ( ) ) ; //get 3 advertised Articles
2020-05-02 14:38:40 +02:00
model . addAttribute ( " commercialArticles " , commercialArticles ) ;
return " shop/articles/id " ;
}
@PostMapping ( " /{id} " )
2020-05-05 23:34:17 +02:00
public String shopArticlesByIdBuy ( HttpServletRequest request ,
HttpServletResponse response ,
HttpSession session ,
2020-05-05 17:24:25 +02:00
@RequestAttribute ( value = " shoppingCart " ) ShoppingCart shoppingCart ,
@PathVariable ( " id " ) Long id ,
@RequestParam ( " quantity " ) Integer quantity ,
2020-05-05 18:59:15 +02:00
@RequestParam ( value = " set_amount " , required = false ) Boolean setAmount ,
2020-05-02 14:38:40 +02:00
@RequestParam ( " fastcheckout " ) Boolean fastcheckout
) {
2020-06-25 16:52:13 +02:00
Article article = articleRepository . findById ( id ) . orElse ( null ) ;
2020-05-07 15:48:00 +02:00
2020-05-08 11:44:30 +02:00
if ( article = = null ) {
request . setAttribute ( " error " , " Der Artikel wurde nicht gefunden. " ) ;
response . setStatus ( HttpServletResponse . SC_NOT_FOUND ) ;
return " error/404 " ;
}
2020-05-05 17:24:25 +02:00
2020-05-05 18:59:15 +02:00
if ( setAmount ! = null & & setAmount ) {
2020-05-07 15:48:00 +02:00
shoppingCart . setArticleCount ( article , quantity ) ;
2020-05-05 18:59:15 +02:00
} else {
2020-05-07 15:48:00 +02:00
shoppingCart . addArticle ( article , quantity ) ;
2020-05-05 18:59:15 +02:00
}
2020-05-05 17:24:25 +02:00
if ( ! fastcheckout ) {
return " shop/articles/post_add " ;
2020-05-02 14:38:40 +02:00
} else {
2020-05-05 18:59:15 +02:00
return " redirect:/shop/checkout " ;
2020-05-02 14:38:40 +02:00
}
}
2020-05-07 15:48:00 +02:00
@GetMapping ( " /{id}/image.jpg " )
public void getImageAsByteArray ( HttpServletRequest request ,
HttpServletResponse response ,
@PathVariable ( " id " ) Long id
) throws IOException {
Article article = articleRepository . findArticleById ( id ) ;
2020-06-14 16:51:59 +02:00
2020-06-01 10:53:10 +02:00
if ( article . image ! = null ) {
2020-06-14 15:54:56 +02:00
File file = new File ( article . image . path ) ;
2020-06-14 16:51:59 +02:00
File allowedPath = new File ( " ./data/img/ " ) ;
if ( file . getCanonicalPath ( ) . startsWith ( allowedPath . getCanonicalPath ( ) ) ) {
2020-06-14 15:54:56 +02:00
InputStream in = new FileInputStream ( file ) ;
response . setContentType ( MediaType . IMAGE_JPEG_VALUE ) ;
IOUtils . copy ( in , response . getOutputStream ( ) ) ;
} else {
throw new RuntimeException ( " Got illegal file path. DB was modified. " ) ;
}
2020-06-01 10:53:10 +02:00
}
2020-05-07 15:48:00 +02:00
}
2020-05-15 12:23:32 +02:00
}