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<Article, Long> { @Query("SELECT a FROM Article a WHERE a.id = :id") List<Article> findByID(int id); + + @Query("SELECT a FROM Article a WHERE a.refArticle = :refArticle") + List<Article> 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 @@ <nav th:replace="fragments/intern :: sidebar"></nav> <div class="content-width"> <h2>Gelisteter Artikel 8450</h2> - <form class="detailgrid"> + <form class="detailgrid" th:action="@{/updateArticleAction}" th:object="${article}" method="post"> <p class="m"> <label for="title">Titel</label> - <input class=" full-width" type="text" name="title" value="Kamera" /> + <input class=" full-width" type="text" id="title" th:field="*{title}" value="Kamera" /> </p> <p class="s"> <label for="ref-article">Refernzierter Artikel</label> - <input class="" type="text" name="ref-article" value="8405" disabled /> + <input class="" type="text" id="ref-article" th:field="*{refArticle}" value="8405" readonly/> <td><a th:href="@{/intern/articles/#q=%2044048}">Details</a></td> </p> <div class="spacer"></div> @@ -45,14 +45,14 @@ <div class="s"> <p> <label for="price">Preis (Netto)</label> - <input class="" type="number" step="0.01" name="price" value="84.45" /> EUR <br /> + <input class="" type="number" step="0.01" id="price" th:field="*{sellingPrice}" value="84.45" /> EUR <br /> (19% Mwst.) <!-- Info von article ref--> <br /> = 105.98 EUR Brutto </p> <p> <label for="max-price-buy">Maximaler Einkaufspreis (Netto)</label> - <input class="" type="number" step="0.01" name="price" value="80.98" /> EUR + <input class="" type="number" step="0.01" id="max-price-buy" th:field="*{maxPurchasePrice}" value="80.98" /> EUR </p> <div> <fieldset> @@ -69,7 +69,7 @@ <p> Bitte jede Kategorien in eine eigene Zeile </p> - <textarea name="tags" class="full-width" rows="6"> + <textarea id="tags" class="full-width" rows="6" th:field="*{category}" > Überwachung Elektronik </textarea> @@ -77,8 +77,8 @@ Elektronik <div class="s"> <p> - <label for="price">Einheiten pro Lagerplatz</label> - <input class="" type="number" name="units-per-slot" value="20" /> + <label for="units-per-slot">Einheiten pro Lagerplatz</label> + <input class="" type="number" id="units-per-slot" th:field="*{maxStock}" value="20" /> </p> <p> <b>Lagerbestand: 12</b> @@ -96,7 +96,7 @@ Elektronik <p class="l"> <label for="description">Beschreibung</label> - <textarea name="description" class="full-width" rows="15"> + <textarea id="description" class="full-width" rows="15" th:field="*{description}"> Eine TOLLE Kamera Jaja du denkst jetzt bestimmt: "Bei dem Preis kann sie gar nich sooo TOLL sein". Aber glaub mir, sie is echt echt TOLL! @@ -104,7 +104,7 @@ Indianerehrenwort! </textarea> </p> <div class="l"> - <button type="submit">Änderungen speichern</button> + <button type="submit" name="action" value="updateArticleAction">Änderungen speichern</button> <button type="reset">Zurücksetzen</button> </div> </form>