From e99dfea609aa5d4ca6d1dafa6a2c1b2fef86d832 Mon Sep 17 00:00:00 2001 From: Seil0 Date: Wed, 26 Feb 2020 18:13:16 +0100 Subject: [PATCH] add Article update via /intern/listedArticles/ID --- .../hso/ecommerce/app/RequestController.java | 38 +++++++++++++------ .../hso/ecommerce/db/ArticleRepository.java | 11 ++++++ .../org/hso/ecommerce/entities/Article.java | 12 +++--- prototype/src/main/resources/db/customers.sql | 4 +- .../templates/intern/listedArticles/id.html | 20 +++++----- 5 files changed, 55 insertions(+), 30 deletions(-) diff --git a/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java b/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java index cc903b8..fd1af9d 100644 --- a/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java +++ b/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java @@ -155,23 +155,37 @@ public class RequestController { } @GetMapping("/intern/listedArticles/{id}") - public String internListedArticlesId() { - Article article = new Article(); - article.setTitle("Test-Artikel"); - article.setCategory("Ding"); - article.setDescription("Ein einfacher Artikel."); - article.setRefArticle(123); - article.setSellingPrice(1000); - article.setMaxPurchasePrice(5000); - article.setMaxStock(20); - article.setCurrentStock(4); + public String internListedArticlesId(Model model) { - articleRepo.save(article); - System.out.println("Article saved!"); + if (articleRepo.findByRefArticle(8405).size() == 0) { + // the article doesn't exist in the db, add it + Article article = new Article(); + article.setTitle("Kamera"); + article.setCategory("Überwachung\nElektronik"); + article.setDescription("Eine TOLLE Kamera\n" + + "Jaja du denkst jetzt bestimmt: \"Bei dem Preis kann sie gar nich sooo TOLL sein\".\n" + + "Aber glaub mir, sie is echt echt TOLL!\n" + + "Indianerehrenwort!"); + article.setRefArticle(8405); + article.setSellingPrice(84.45f); + article.setMaxPurchasePrice(80.98f); + article.setMaxStock(20); + article.setCurrentStock(12); + + articleRepo.save(article); + } + + model.addAttribute(articleRepo.findByRefArticle(8405).get(0)); return "intern/listedArticles/id"; } + @RequestMapping(value="/updateArticleAction", method=RequestMethod.POST, params="action=updateArticleAction") + public String updateArticleAction(@ModelAttribute Article article, HttpServletResponse response) { + articleRepo.updateByRefArticle(article.refArticle, article.title); + + return "redirect:intern/listedArticles/id"; + } @GetMapping("/intern/articles/") public String internArticles() { diff --git a/prototype/src/main/java/org/hso/ecommerce/db/ArticleRepository.java b/prototype/src/main/java/org/hso/ecommerce/db/ArticleRepository.java index 8a1919f..4cb6acf 100644 --- a/prototype/src/main/java/org/hso/ecommerce/db/ArticleRepository.java +++ b/prototype/src/main/java/org/hso/ecommerce/db/ArticleRepository.java @@ -2,8 +2,10 @@ package org.hso.ecommerce.db; import org.hso.ecommerce.entities.Article; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; import java.util.List; @@ -15,4 +17,13 @@ public interface ArticleRepository extends JpaRepository { @Query("SELECT a FROM Article a WHERE a.id = :id") List
findByID(int id); + + @Query("SELECT a FROM Article a WHERE a.refArticle = :refArticle") + List
findByRefArticle(int refArticle); + + // https://www.logicbig.com/tutorials/spring-framework/spring-data/modifying-queries.html + @Transactional + @Modifying + @Query("UPDATE Article a SET a.title = :title WHERE a.refArticle = :refArticle") + void updateByRefArticle(int refArticle, String title); } diff --git a/prototype/src/main/java/org/hso/ecommerce/entities/Article.java b/prototype/src/main/java/org/hso/ecommerce/entities/Article.java index dda935f..3ecbc5e 100644 --- a/prototype/src/main/java/org/hso/ecommerce/entities/Article.java +++ b/prototype/src/main/java/org/hso/ecommerce/entities/Article.java @@ -14,8 +14,8 @@ public class Article { public String category; public String description; public int refArticle; - public int sellingPrice; - public int maxPurchasePrice; + public float sellingPrice; + public float maxPurchasePrice; public int maxStock; public int currentStock; @@ -59,19 +59,19 @@ public class Article { this.refArticle = refArticle; } - public int getSellingPrice() { + public float getSellingPrice() { return sellingPrice; } - public void setSellingPrice(int sellingPrice) { + public void setSellingPrice(float sellingPrice) { this.sellingPrice = sellingPrice; } - public int getMaxPurchasePrice() { + public float getMaxPurchasePrice() { return maxPurchasePrice; } - public void setMaxPurchasePrice(int maxPurchasePrice) { + public void setMaxPurchasePrice(float maxPurchasePrice) { this.maxPurchasePrice = maxPurchasePrice; } diff --git a/prototype/src/main/resources/db/customers.sql b/prototype/src/main/resources/db/customers.sql index 46afd3f..5ae5f5b 100644 --- a/prototype/src/main/resources/db/customers.sql +++ b/prototype/src/main/resources/db/customers.sql @@ -12,8 +12,8 @@ CREATE TABLE "article" ( "category" TEXT, "description" TEXT, "refArticle" INTEGER, - "sellingPrice" INTEGER, - "maxPurchasePrice" INTEGER, + "sellingPrice" FLOAT, + "maxPurchasePrice" FLOAT, "maxStock" INTEGER, "currentStock" INTEGER ); diff --git a/prototype/src/main/resources/templates/intern/listedArticles/id.html b/prototype/src/main/resources/templates/intern/listedArticles/id.html index 0c33014..c864f01 100644 --- a/prototype/src/main/resources/templates/intern/listedArticles/id.html +++ b/prototype/src/main/resources/templates/intern/listedArticles/id.html @@ -22,14 +22,14 @@

Gelisteter Artikel 8450

-
+

- +

- + Details

@@ -45,14 +45,14 @@

-  EUR
+  EUR
(19% Mwst.)
= 105.98 EUR Brutto

-  EUR +  EUR

@@ -69,7 +69,7 @@

Bitte jede Kategorien in eine eigene Zeile

- @@ -77,8 +77,8 @@ Elektronik

- - + +

Lagerbestand: 12 @@ -96,7 +96,7 @@ Elektronik

-

- +