WIP feature/listedArticles #15
|
@ -89,3 +89,5 @@ local.properties
|
||||||
|
|
||||||
# SQLite
|
# SQLite
|
||||||
prototype/*.db
|
prototype/*.db
|
||||||
|
|
||||||
|
prototype/images
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
./test.db
|
./e-commerce.db
|
||||||
./build
|
./build
|
||||||
./gradle
|
./gradle
|
||||||
./out
|
./out
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package org.hso.ecommerce.controller.shop;
|
package org.hso.ecommerce.controller.shop;
|
||||||
|
|
||||||
|
import org.apache.tomcat.util.http.fileupload.IOUtils;
|
||||||
import org.hso.ecommerce.entities.shop.Article;
|
import org.hso.ecommerce.entities.shop.Article;
|
||||||
import org.hso.ecommerce.entities.shop.ShoppingCart;
|
import org.hso.ecommerce.entities.shop.ShoppingCart;
|
||||||
import org.hso.ecommerce.repos.shop.ArticleRepository;
|
import org.hso.ecommerce.repos.shop.ArticleRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
@ -11,7 +13,10 @@ import org.springframework.web.bind.annotation.*;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.servlet.http.HttpSession;
|
import javax.servlet.http.HttpSession;
|
||||||
import java.util.ArrayList;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@ -23,40 +28,35 @@ public class ShopArticleController {
|
||||||
private final ArticleRepository articleRepository = null;
|
private final ArticleRepository articleRepository = null;
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public String shopArticlesById(Model model, @PathVariable("id") Integer Id) {
|
public String shopArticlesById(Model model,
|
||||||
|
@PathVariable("id") Long id,
|
||||||
|
HttpServletRequest request,
|
||||||
|
HttpServletResponse response
|
||||||
|
) {
|
||||||
|
|
||||||
//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);
|
Article article = articleRepository.findArticleById(id);
|
||||||
|
|
||||||
|
|
||||||
|
if(article == null) {
|
||||||
|
request.setAttribute("error", "Der Artikel wurde nicht gefunden.");
|
||||||
|
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
||||||
|
return "error/404";
|
||||||
|
}
|
||||||
|
|
||||||
|
model.addAttribute("article", article);
|
||||||
|
|
||||||
//TODO: Check if in Stock
|
//TODO: Check if in Stock
|
||||||
if(false){
|
if (false) {
|
||||||
model.addAttribute("inStock", true);
|
model.addAttribute("inStock", true);
|
||||||
}else{
|
} else {
|
||||||
model.addAttribute("inStock", false);
|
model.addAttribute("inStock", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: Get 2 Commercialised Articles
|
List<Article> commercialArticles = articleRepository.getCommercialisedArticles("2");
|
||||||
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);
|
model.addAttribute("commercialArticles", commercialArticles);
|
||||||
|
|
||||||
|
|
||||||
return "shop/articles/id";
|
return "shop/articles/id";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,17 +71,19 @@ public class ShopArticleController {
|
||||||
@RequestParam("fastcheckout") Boolean fastcheckout
|
@RequestParam("fastcheckout") Boolean fastcheckout
|
||||||
) {
|
) {
|
||||||
|
|
||||||
Optional<Article> article = articleRepository.findById(id);
|
|
||||||
if (!article.isPresent()) {
|
Article article = articleRepository.findArticleById(id);
|
||||||
request.setAttribute("error", "Der Artikel wurde nicht gefunden.");
|
|
||||||
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
// if (!article.isPresent()) {
|
||||||
return "error/404";
|
// request.setAttribute("error", "Der Artikel wurde nicht gefunden.");
|
||||||
}
|
// response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
||||||
|
// return "error/404";
|
||||||
|
// }
|
||||||
|
|
||||||
if (setAmount != null && setAmount) {
|
if (setAmount != null && setAmount) {
|
||||||
shoppingCart.setArticleCount(article.get(), quantity);
|
shoppingCart.setArticleCount(article, quantity);
|
||||||
} else {
|
} else {
|
||||||
shoppingCart.addArticle(article.get(), quantity);
|
shoppingCart.addArticle(article, quantity);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fastcheckout) {
|
if (!fastcheckout) {
|
||||||
|
@ -90,4 +92,20 @@ public class ShopArticleController {
|
||||||
return "redirect:/shop/checkout";
|
return "redirect:/shop/checkout";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}/image.jpg")
|
||||||
|
public void getImageAsByteArray(HttpServletRequest request,
|
||||||
|
HttpServletResponse response,
|
||||||
|
@PathVariable("id") Long id
|
||||||
|
) throws IOException {
|
||||||
|
|
||||||
|
Article article = articleRepository.findArticleById(id);
|
||||||
|
|
||||||
|
InputStream in = new FileInputStream(article.image.path);
|
||||||
|
response.setContentType(MediaType.IMAGE_JPEG_VALUE);
|
||||||
|
IOUtils.copy(in, response.getOutputStream());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package org.hso.ecommerce.controller.shop;
|
package org.hso.ecommerce.controller.shop;
|
||||||
|
|
||||||
import org.hso.ecommerce.entities.shop.Article;
|
import org.hso.ecommerce.entities.shop.Article;
|
||||||
|
import org.hso.ecommerce.repos.shop.ArticleRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
@ -8,13 +10,15 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
import javax.servlet.http.HttpSession;
|
import javax.servlet.http.HttpSession;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.util.List;
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("/")
|
@RequestMapping("/")
|
||||||
public class ShopIndexController {
|
public class ShopIndexController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private final ArticleRepository articleRepository = null;
|
||||||
|
|
||||||
@GetMapping("/")
|
@GetMapping("/")
|
||||||
public String home() {
|
public String home() {
|
||||||
return "redirect:/shop/";
|
return "redirect:/shop/";
|
||||||
|
@ -23,8 +27,7 @@ public class ShopIndexController {
|
||||||
@GetMapping("/shop/")
|
@GetMapping("/shop/")
|
||||||
public String shop(Model model, HttpSession session) {
|
public String shop(Model model, HttpSession session) {
|
||||||
|
|
||||||
//TODO: get commercialised Articles
|
List<Article> commercialArticles = articleRepository.getCommercialisedArticles("4");
|
||||||
ArrayList<Article> commercialArticles = getArticles();
|
|
||||||
model.addAttribute("commercialArticles", commercialArticles);
|
model.addAttribute("commercialArticles", commercialArticles);
|
||||||
|
|
||||||
//check if logged in
|
//check if logged in
|
||||||
|
@ -33,20 +36,19 @@ public class ShopIndexController {
|
||||||
if (session != null && session.getAttribute("id") != null) {
|
if (session != null && session.getAttribute("id") != null) {
|
||||||
long userId = (long) session.getAttribute("id");
|
long userId = (long) session.getAttribute("id");
|
||||||
isLoggedIn = true;
|
isLoggedIn = true;
|
||||||
if (false) {
|
}
|
||||||
hasOrders = true; //TODO: Find out whether user has orders!
|
|
||||||
|
if (isLoggedIn) {
|
||||||
|
List<Article> suggestedArticles = articleRepository.getLastOrderedArticles("4");
|
||||||
|
if(suggestedArticles.size() > 0) {
|
||||||
|
model.addAttribute("suggestedArticles", suggestedArticles);
|
||||||
|
hasOrders = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
model.addAttribute("isLoggedIn", isLoggedIn);
|
model.addAttribute("isLoggedIn", isLoggedIn);
|
||||||
model.addAttribute("hasOrders", hasOrders);
|
model.addAttribute("hasOrders", hasOrders);
|
||||||
|
|
||||||
|
|
||||||
if (hasOrders) {
|
|
||||||
//TODO: get up to last 4 Orders
|
|
||||||
ArrayList<Article> suggestedArticles = getArticles();
|
|
||||||
model.addAttribute("suggestedArticles", suggestedArticles);
|
|
||||||
}
|
|
||||||
|
|
||||||
return "shop/index";
|
return "shop/index";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,40 +67,4 @@ public class ShopIndexController {
|
||||||
return "privacy";
|
return "privacy";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public ArrayList<Article> getArticles() {
|
|
||||||
ArrayList<Article> dummyArticles = new ArrayList<Article>();
|
|
||||||
|
|
||||||
Article d1 = new Article();
|
|
||||||
d1.description = "this is dummy1";
|
|
||||||
d1.title = "dummy1";
|
|
||||||
d1.shopPricePerUnitNetCent = 1500;
|
|
||||||
d1.id = 1234;
|
|
||||||
dummyArticles.add(d1);
|
|
||||||
|
|
||||||
Article d2 = new Article();
|
|
||||||
d2.description = "this is dummy2";
|
|
||||||
d2.title = "dummy2";
|
|
||||||
d2.shopPricePerUnitNetCent = 2000;
|
|
||||||
d2.id = 2345;
|
|
||||||
dummyArticles.add(d2);
|
|
||||||
|
|
||||||
Article d3 = new Article();
|
|
||||||
d3.description = "this is dummy3";
|
|
||||||
d3.title = "dummy3";
|
|
||||||
d3.shopPricePerUnitNetCent = 2500;
|
|
||||||
d3.id = 3456;
|
|
||||||
dummyArticles.add(d3);
|
|
||||||
|
|
||||||
Article d4 = new Article();
|
|
||||||
d4.description = "this is dummy4";
|
|
||||||
d4.title = "dummy4";
|
|
||||||
d4.shopPricePerUnitNetCent = 3000;
|
|
||||||
d4.id = 4567;
|
|
||||||
dummyArticles.add(d4);
|
|
||||||
|
|
||||||
return dummyArticles;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,7 @@ public class Article {
|
||||||
public String description;
|
public String description;
|
||||||
|
|
||||||
@OneToOne(optional = true)
|
@OneToOne(optional = true)
|
||||||
|
@Basic(fetch = FetchType.LAZY)
|
||||||
public Image image;
|
public Image image;
|
||||||
|
|
||||||
@ManyToMany
|
@ManyToMany
|
||||||
|
|
|
@ -11,7 +11,6 @@ public class Image {
|
||||||
@Basic
|
@Basic
|
||||||
public long id;
|
public long id;
|
||||||
|
|
||||||
@Lob
|
public String path;
|
||||||
@Column(name = "data", columnDefinition = "BLOB", nullable = false)
|
|
||||||
private byte[] data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,31 @@
|
||||||
package org.hso.ecommerce.repos.shop;
|
package org.hso.ecommerce.repos.shop;
|
||||||
|
|
||||||
import org.hso.ecommerce.entities.shop.Article;
|
import org.hso.ecommerce.entities.shop.Article;
|
||||||
|
import org.hso.ecommerce.entities.user.User;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface ArticleRepository extends JpaRepository<Article, Long> {
|
public interface ArticleRepository extends JpaRepository<Article, Long> {
|
||||||
|
|
||||||
|
@Query(nativeQuery = true, value = "Select * FROM articles where articles.id = :articleId")
|
||||||
|
Article findArticleById(@Param("articleId") long articleId);
|
||||||
|
|
||||||
|
|
||||||
|
//TODO: getCommercialisedArticles(int quantity)
|
||||||
|
@Query("SELECT c FROM User c WHERE c.email = :quantity")
|
||||||
|
List<Article> getCommercialisedArticles(@Param("quantity") String quantity);
|
||||||
|
|
||||||
|
|
||||||
|
//TODO: getLastOrderedArticles(int quantity)
|
||||||
|
@Query("SELECT c FROM User c WHERE c.email = :quantity")
|
||||||
|
List<Article> getLastOrderedArticles(@Param("quantity") String quantity);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
<p th:text="${article.description}"></p>
|
<p th:text="${article.description}"></p>
|
||||||
</div>
|
</div>
|
||||||
<div class="s">
|
<div class="s">
|
||||||
<img th:src="@{/img/product-1.jpg}"/>
|
<img th:src="@{/shop/articles/{id}/image.jpg(id=${article.id})}" class="s"/>
|
||||||
</div>
|
</div>
|
||||||
<div class="s"></div>
|
<div class="s"></div>
|
||||||
<form class="s" method="POST">
|
<form class="s" method="POST">
|
||||||
|
@ -56,7 +56,7 @@
|
||||||
<h1>Weitere Schnäppchen</h1>
|
<h1>Weitere Schnäppchen</h1>
|
||||||
<div class='grid m base shadow'>
|
<div class='grid m base shadow'>
|
||||||
<section th:each="article: ${commercialArticles}"><a th:href="@{/shop/articles/{id}(id = ${article.id})}" class="section">
|
<section th:each="article: ${commercialArticles}"><a th:href="@{/shop/articles/{id}(id = ${article.id})}" class="section">
|
||||||
<img th:src="@{/img/product-4.jpg}">
|
<img th:src="@{/shop/articles/{id}/image.jpg(id=${article.id})}" class="s"/>
|
||||||
<h2 th:text="${article.title}"></h2>
|
<h2 th:text="${article.title}"></h2>
|
||||||
<p class='price' th:text="${article.shopPricePerUnitNetCent}"></p>
|
<p class='price' th:text="${article.shopPricePerUnitNetCent}"></p>
|
||||||
<p th:text="${article.description}"></p>
|
<p th:text="${article.description}"></p>
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
<section th:each="article: ${commercialArticles}">
|
<section th:each="article: ${commercialArticles}">
|
||||||
<a th:href="@{/shop/articles/{id}(id=${article.id})}" class="section">
|
<a th:href="@{/shop/articles/{id}(id=${article.id})}" class="section">
|
||||||
|
|
||||||
<img th:src="@{/img/product-1.jpg}"/>
|
<img th:src="@{/shop/articles/{id}/image.jpg(id=${article.id})}" class="s"/>
|
||||||
<h2 th:text="${article.title}" />
|
<h2 th:text="${article.title}" />
|
||||||
<p class='price' th:text="${article.shopPricePerUnitNetCent}" />
|
<p class='price' th:text="${article.shopPricePerUnitNetCent}" />
|
||||||
<p th:text="${article.description}" />
|
<p th:text="${article.description}" />
|
||||||
|
@ -53,17 +53,17 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- If User is logged in but hasn't ordered anything yet-->
|
<!-- If User is logged in but hasn't ordered anything yet-->
|
||||||
<div th:if="$isLoggedIn == true and $hasOrders == false">
|
<div th:if="${isLoggedIn == true and hasOrders == false}">
|
||||||
<h1>Jetzt Shoppen und Empfehlungen erhalten!</h1>
|
<h1>Jetzt Shoppen und Empfehlungen erhalten!</h1>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- If User is logged in and has ordered something before-->
|
<!-- If User is logged in and has ordered something before-->
|
||||||
<div th:if="$hasOrders == true">
|
<div th:if="${hasOrders == true}">
|
||||||
<div class='grid m base shadow'>
|
<div class='grid m base shadow'>
|
||||||
<section th:each="article: ${suggestedArticles}">
|
<section th:each="article: ${suggestedArticles}">
|
||||||
<a th:href="@{/shop/articles/{id}(id=${article.id})}" class="section">
|
<a th:href="@{/shop/articles/{id}(id=${article.id})}" class="section">
|
||||||
|
|
||||||
<img th:src="@{/img/product-1.jpg}"/>
|
<img th:src="@{/shop/articles/{id}/image.jpg(id=${article.id})}"/>
|
||||||
<h2 th:text="${article.title}" />
|
<h2 th:text="${article.title}" />
|
||||||
<p class='price' th:text="${article.shopPricePerUnitNetCent}" />
|
<p class='price' th:text="${article.shopPricePerUnitNetCent}" />
|
||||||
<p th:text="${article.description}" />
|
<p th:text="${article.description}" />
|
||||||
|
|
Reference in New Issue