implement imageserving and first part of ArticleRepository

This commit is contained in:
Hannes Huber 2020-05-07 15:48:00 +02:00 committed by localhorst
parent 4048d6c5ad
commit 1e7266e554
9 changed files with 98 additions and 92 deletions

2
.gitignore vendored
View File

@ -89,3 +89,5 @@ local.properties
# SQLite
prototype/*.db
prototype/images

View File

@ -1,4 +1,4 @@
./test.db
./e-commerce.db
./build
./gradle
./out

View File

@ -1,9 +1,11 @@
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.ShoppingCart;
import org.hso.ecommerce.repos.shop.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.HttpServletResponse;
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.Optional;
@ -23,40 +28,35 @@ public class ShopArticleController {
private final ArticleRepository articleRepository = null;
@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
if(false){
if (false) {
model.addAttribute("inStock", true);
}else{
} 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);
List<Article> commercialArticles = articleRepository.getCommercialisedArticles("2");
model.addAttribute("commercialArticles", commercialArticles);
return "shop/articles/id";
}
@ -71,17 +71,19 @@ public class ShopArticleController {
@RequestParam("fastcheckout") Boolean fastcheckout
) {
Optional<Article> article = articleRepository.findById(id);
if (!article.isPresent()) {
request.setAttribute("error", "Der Artikel wurde nicht gefunden.");
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return "error/404";
}
Article article = articleRepository.findArticleById(id);
// if (!article.isPresent()) {
// request.setAttribute("error", "Der Artikel wurde nicht gefunden.");
// response.setStatus(HttpServletResponse.SC_NOT_FOUND);
// return "error/404";
// }
if (setAmount != null && setAmount) {
shoppingCart.setArticleCount(article.get(), quantity);
shoppingCart.setArticleCount(article, quantity);
} else {
shoppingCart.addArticle(article.get(), quantity);
shoppingCart.addArticle(article, quantity);
}
if (!fastcheckout) {
@ -90,4 +92,20 @@ public class ShopArticleController {
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());
}
}

View File

@ -1,6 +1,8 @@
package org.hso.ecommerce.controller.shop;
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.ui.Model;
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.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/")
public class ShopIndexController {
@Autowired
private final ArticleRepository articleRepository = null;
@GetMapping("/")
public String home() {
return "redirect:/shop/";
@ -23,8 +27,7 @@ public class ShopIndexController {
@GetMapping("/shop/")
public String shop(Model model, HttpSession session) {
//TODO: get commercialised Articles
ArrayList<Article> commercialArticles = getArticles();
List<Article> commercialArticles = articleRepository.getCommercialisedArticles("4");
model.addAttribute("commercialArticles", commercialArticles);
//check if logged in
@ -33,20 +36,19 @@ public class ShopIndexController {
if (session != null && session.getAttribute("id") != null) {
long userId = (long) session.getAttribute("id");
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("hasOrders", hasOrders);
if (hasOrders) {
//TODO: get up to last 4 Orders
ArrayList<Article> suggestedArticles = getArticles();
model.addAttribute("suggestedArticles", suggestedArticles);
}
return "shop/index";
}
@ -65,40 +67,4 @@ public class ShopIndexController {
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;
}
}

View File

@ -32,6 +32,7 @@ public class Article {
public String description;
@OneToOne(optional = true)
@Basic(fetch = FetchType.LAZY)
public Image image;
@ManyToMany

View File

@ -11,7 +11,6 @@ public class Image {
@Basic
public long id;
@Lob
@Column(name = "data", columnDefinition = "BLOB", nullable = false)
private byte[] data;
public String path;
}

View File

@ -1,11 +1,31 @@
package org.hso.ecommerce.repos.shop;
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.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
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);
}

View File

@ -27,7 +27,7 @@
<p th:text="${article.description}"></p>
</div>
<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 class="s"></div>
<form class="s" method="POST">
@ -56,7 +56,7 @@
<h1>Weitere Schnäppchen</h1>
<div class='grid m base shadow'>
<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>
<p class='price' th:text="${article.shopPricePerUnitNetCent}"></p>
<p th:text="${article.description}"></p>

View File

@ -21,7 +21,7 @@
<section th:each="article: ${commercialArticles}">
<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}" />
<p class='price' th:text="${article.shopPricePerUnitNetCent}" />
<p th:text="${article.description}" />
@ -53,17 +53,17 @@
</div>
<!-- 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>
</div>
<!-- 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'>
<section th:each="article: ${suggestedArticles}">
<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}" />
<p class='price' th:text="${article.shopPricePerUnitNetCent}" />
<p th:text="${article.description}" />