This repository has been archived on 2020-08-02. You can view files and clone it, but cannot push or open issues or pull requests.
e-commerce/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopArticleController.java

88 lines
2.9 KiB
Java
Raw Normal View History

2020-04-29 22:44:16 +02:00
package org.hso.ecommerce.controller.shop;
2020-04-28 22:41:29 +02:00
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;
import org.springframework.beans.factory.annotation.Autowired;
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.*;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
2020-05-05 17:24:25 +02:00
import java.util.Optional;
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-02 14:38:40 +02:00
@GetMapping("/{id}")
public String shopArticlesById(Model model, @PathVariable("id") Integer Id) {
//TODO: Get Article by Id instead of this dummy shit
Article d1 = new Article();
d1.description = "this is dummy1";
d1.title = "dummy1";
d1.shopPricePerUnitNetCent = 1500;
d1.id = 1234;
model.addAttribute("article", d1);
//TODO: Check if in Stock
if(false){
model.addAttribute("inStock", true);
}else{
model.addAttribute("inStock", false);
}
//TODO: Get 2 Commercialised Articles
List<Article> commercialArticles = new ArrayList<Article>();
Article d2 = new Article();
d2.description = "this is dummy2";
d2.title = "dummy2";
d2.shopPricePerUnitNetCent = 2000;
d2.id = 2345;
Article d3 = new Article();
d3.description = "this is dummy3";
d3.title = "dummy3";
d3.shopPricePerUnitNetCent = 2500;
d3.id = 3456;
commercialArticles.add(d2);
commercialArticles.add(d3);
model.addAttribute("commercialArticles", commercialArticles);
return "shop/articles/id";
}
@PostMapping("/{id}")
public String shopArticlesByIdBuy(HttpSession session,
2020-05-05 17:24:25 +02:00
@RequestAttribute(value = "shoppingCart") ShoppingCart shoppingCart,
@PathVariable("id") Long id,
@RequestParam("quantity") Integer quantity,
@RequestParam(value = "set_amount", required = false) Boolean setAmount,
2020-05-02 14:38:40 +02:00
@RequestParam("fastcheckout") Boolean fastcheckout
) {
2020-05-05 17:24:25 +02:00
Optional<Article> article = articleRepository.findById(id);
if (!article.isPresent()) {
throw new RuntimeException("Article not found!");
}
if (setAmount != null && setAmount) {
shoppingCart.setArticleCount(article.get(), quantity);
} else {
shoppingCart.addArticle(article.get(), quantity);
}
2020-05-05 17:24:25 +02:00
if (!fastcheckout) {
return "shop/articles/post_add";
2020-05-02 14:38:40 +02:00
} else {
return "redirect:/shop/checkout";
2020-05-02 14:38:40 +02:00
}
}
2020-04-28 22:41:29 +02:00
}