From 39b29c0f871253eb81561118062e4e69869dec83 Mon Sep 17 00:00:00 2001 From: Seil0 Date: Mon, 24 Feb 2020 14:52:31 +0100 Subject: [PATCH 1/3] add Article as Entity * update spring-boot to version 2.2.4 --- .gitignore | 2 +- prototype/build.gradle | 2 +- .../hso/ecommerce/app/RequestController.java | 70 ++++++++++---- .../hso/ecommerce/db/ArticleRepository.java | 18 ++++ .../org/hso/ecommerce/db/CustomerConfig.java | 1 - .../org/hso/ecommerce/entities/Article.java | 93 +++++++++++++++++++ .../org/hso/ecommerce/entities/Customer.java | 2 +- prototype/src/main/resources/db/customers.sql | 12 +++ 8 files changed, 176 insertions(+), 24 deletions(-) create mode 100644 prototype/src/main/java/org/hso/ecommerce/db/ArticleRepository.java create mode 100644 prototype/src/main/java/org/hso/ecommerce/entities/Article.java diff --git a/.gitignore b/.gitignore index 178b075..3b718f8 100644 --- a/.gitignore +++ b/.gitignore @@ -88,4 +88,4 @@ local.properties *.launch # SQLite -prototype/src/main/resources/*.db +prototype/*.db diff --git a/prototype/build.gradle b/prototype/build.gradle index d035df8..02874df 100644 --- a/prototype/build.gradle +++ b/prototype/build.gradle @@ -3,7 +3,7 @@ buildscript { mavenCentral() } dependencies { - classpath("org.springframework.boot:spring-boot-gradle-plugin:2.2.2.RELEASE") + classpath("org.springframework.boot:spring-boot-gradle-plugin:2.2.4.RELEASE") } } 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 af1f387..cc903b8 100644 --- a/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java +++ b/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java @@ -1,7 +1,9 @@ package org.hso.ecommerce.app; import org.hso.ecommerce.contoller.Login; +import org.hso.ecommerce.db.ArticleRepository; import org.hso.ecommerce.db.CustomerRepository; +import org.hso.ecommerce.entities.Article; import org.hso.ecommerce.entities.Customer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -18,7 +20,16 @@ import java.util.UUID; @Controller public class RequestController { - static int notSoRandom = 0; + static int notSoRandom = 0; + + // TODO move this to a separate controller + private final ArticleRepository articleRepo; + + @Autowired + public RequestController(ArticleRepository articleRepo) { + this.articleRepo = articleRepo; + } + @GetMapping("/") public String home() { @@ -32,13 +43,13 @@ public class RequestController { @PostMapping("/login") public String loginPost(HttpServletResponse response, @RequestParam(value = "goto", required = false) String gto) { - response.addCookie(new Cookie("login", "true")); + response.addCookie(new Cookie("login", "true")); - if (gto != null && gto.startsWith("/")) { - return "redirect:" + gto; - } else { - return "redirect:/"; - } + if (gto != null && gto.startsWith("/")) { + return "redirect:" + gto; + } else { + return "redirect:/"; + } } @PostMapping("/logout") @@ -71,10 +82,11 @@ public class RequestController { public String shopCheckoutFinish() { return "shop/checkoutFinish"; } + @GetMapping("/shop/checkoutFinish") public String shopCheckoutFinishGET() { return "shop/checkoutFinish"; - } + } @GetMapping("/shop/articles/{id}") public String shopArticlesById() { @@ -82,13 +94,13 @@ public class RequestController { } @PostMapping("/shop/articles/{id}") - public String shopArticlesByIdBuy(@RequestAttribute("customer") Boolean isCustomer, @PathVariable("id") Integer id) { + public String shopArticlesByIdBuy(@RequestAttribute("customer") Boolean isCustomer, @PathVariable("id") Integer id) { if (isCustomer) { - return "redirect:/shop/checkout"; + return "redirect:/shop/checkout"; } else { - return "redirect:/login?goto=%2Fshop%2Farticles%2F"+id; + return "redirect:/login?goto=%2Fshop%2Farticles%2F" + id; } - } + } @GetMapping("/user/") public String user() { @@ -144,6 +156,19 @@ 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); + + articleRepo.save(article); + System.out.println("Article saved!"); + return "intern/listedArticles/id"; } @@ -235,18 +260,23 @@ public class RequestController { @PostMapping("/intern/warehouse/progress/{id}") public String accountingWarehouseProgressIdPost(HttpServletResponse response) { - if((notSoRandom++) % 2 == 1) { - return "redirect:/intern/warehouse/progress/450"; - } else { - response.setStatus(409); - return "intern/warehouse/error_progress_failed"; - } + if ((notSoRandom++) % 2 == 1) { + return "redirect:/intern/warehouse/progress/450"; + } else { + response.setStatus(409); + return "intern/warehouse/error_progress_failed"; + } } @GetMapping("/intern/warehouse/progress/{id}") public String accountingWarehouseProgressId() { - return "intern/warehouse/id_progress"; - } + return "intern/warehouse/id_progress"; + } + + @GetMapping("/intern/warehouse/error_progress_failed") + public String err() { + return "intern/warehouse/error_progress_failed"; + } @GetMapping("/intern/warehouse/slots/") public String accountingWarehouseSlots() { diff --git a/prototype/src/main/java/org/hso/ecommerce/db/ArticleRepository.java b/prototype/src/main/java/org/hso/ecommerce/db/ArticleRepository.java new file mode 100644 index 0000000..8a1919f --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/db/ArticleRepository.java @@ -0,0 +1,18 @@ +package org.hso.ecommerce.db; + +import org.hso.ecommerce.entities.Article; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface ArticleRepository extends JpaRepository { + + @Query("SELECT a FROM Article a WHERE a.title = :title") + List
findByTitle(String title); + + @Query("SELECT a FROM Article a WHERE a.id = :id") + List
findByID(int id); +} diff --git a/prototype/src/main/java/org/hso/ecommerce/db/CustomerConfig.java b/prototype/src/main/java/org/hso/ecommerce/db/CustomerConfig.java index 82d5d28..8b22efa 100644 --- a/prototype/src/main/java/org/hso/ecommerce/db/CustomerConfig.java +++ b/prototype/src/main/java/org/hso/ecommerce/db/CustomerConfig.java @@ -5,7 +5,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; -import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; import javax.sql.DataSource; diff --git a/prototype/src/main/java/org/hso/ecommerce/entities/Article.java b/prototype/src/main/java/org/hso/ecommerce/entities/Article.java new file mode 100644 index 0000000..dda935f --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/entities/Article.java @@ -0,0 +1,93 @@ +package org.hso.ecommerce.entities; + +import javax.persistence.*; + +@Entity +@Table(name = "article") +public class Article { + + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_Sequence") + @SequenceGenerator(name = "id_Sequence", sequenceName = "ID_SEQ") + public Long id; + public String title; + public String category; + public String description; + public int refArticle; + public int sellingPrice; + public int maxPurchasePrice; + public int maxStock; + public int currentStock; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public int getRefArticle() { + return refArticle; + } + + public void setRefArticle(int refArticle) { + this.refArticle = refArticle; + } + + public int getSellingPrice() { + return sellingPrice; + } + + public void setSellingPrice(int sellingPrice) { + this.sellingPrice = sellingPrice; + } + + public int getMaxPurchasePrice() { + return maxPurchasePrice; + } + + public void setMaxPurchasePrice(int maxPurchasePrice) { + this.maxPurchasePrice = maxPurchasePrice; + } + + public int getMaxStock() { + return maxStock; + } + + public void setMaxStock(int maxStock) { + this.maxStock = maxStock; + } + + public int getCurrentStock() { + return currentStock; + } + + public void setCurrentStock(int currentStock) { + this.currentStock = currentStock; + } +} diff --git a/prototype/src/main/java/org/hso/ecommerce/entities/Customer.java b/prototype/src/main/java/org/hso/ecommerce/entities/Customer.java index 609259a..dbaec6c 100644 --- a/prototype/src/main/java/org/hso/ecommerce/entities/Customer.java +++ b/prototype/src/main/java/org/hso/ecommerce/entities/Customer.java @@ -7,7 +7,7 @@ import javax.persistence.*; public class Customer { @Id - @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "id_Sequence") + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_Sequence") @SequenceGenerator(name = "id_Sequence", sequenceName = "ID_SEQ") public Long id; public String lastname; diff --git a/prototype/src/main/resources/db/customers.sql b/prototype/src/main/resources/db/customers.sql index b1a108f..46afd3f 100644 --- a/prototype/src/main/resources/db/customers.sql +++ b/prototype/src/main/resources/db/customers.sql @@ -5,3 +5,15 @@ CREATE TABLE "customers" ( "username" TEXT, "password" TEXT ); + +CREATE TABLE "article" ( + "id" INTEGER PRIMARY KEY AUTOINCREMENT, + "title" TEXT, + "category" TEXT, + "description" TEXT, + "refArticle" INTEGER, + "sellingPrice" INTEGER, + "maxPurchasePrice" INTEGER, + "maxStock" INTEGER, + "currentStock" INTEGER +); -- 2.50.1 From e99dfea609aa5d4ca6d1dafa6a2c1b2fef86d832 Mon Sep 17 00:00:00 2001 From: Seil0 Date: Wed, 26 Feb 2020 18:13:16 +0100 Subject: [PATCH 2/3] 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

-

- +
-- 2.50.1 From 4a095fac092c254eae81832345a86d145e877232 Mon Sep 17 00:00:00 2001 From: Seil0 Date: Mon, 2 Mar 2020 18:18:29 +0100 Subject: [PATCH 3/3] update article via id --- .../main/java/org/hso/ecommerce/app/RequestController.java | 2 +- .../main/java/org/hso/ecommerce/db/ArticleRepository.java | 6 ------ prototype/src/main/resources/application.properties | 1 + .../main/resources/templates/intern/listedArticles/id.html | 1 + 4 files changed, 3 insertions(+), 7 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 fd1af9d..6474635 100644 --- a/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java +++ b/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java @@ -182,7 +182,7 @@ public class RequestController { @RequestMapping(value="/updateArticleAction", method=RequestMethod.POST, params="action=updateArticleAction") public String updateArticleAction(@ModelAttribute Article article, HttpServletResponse response) { - articleRepo.updateByRefArticle(article.refArticle, article.title); + articleRepo.save(article); return "redirect:intern/listedArticles/id"; } 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 4cb6acf..914f5a7 100644 --- a/prototype/src/main/java/org/hso/ecommerce/db/ArticleRepository.java +++ b/prototype/src/main/java/org/hso/ecommerce/db/ArticleRepository.java @@ -20,10 +20,4 @@ public interface ArticleRepository extends JpaRepository { @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/resources/application.properties b/prototype/src/main/resources/application.properties index 5507957..89f099a 100644 --- a/prototype/src/main/resources/application.properties +++ b/prototype/src/main/resources/application.properties @@ -9,6 +9,7 @@ spring.datasource.url = jdbc:sqlite:./test.db spring.datasource.driverClassName = org.sqlite.JDBC spring.jpa.properties.hibernate.dialect = org.hso.ecommerce.db.SQLiteDialect spring.jpa.hibernate.ddl-auto=update +#spring.jpa.show-sql=true # ---------------------------------------- # WEB PROPERTIES diff --git a/prototype/src/main/resources/templates/intern/listedArticles/id.html b/prototype/src/main/resources/templates/intern/listedArticles/id.html index c864f01..ae88f7e 100644 --- a/prototype/src/main/resources/templates/intern/listedArticles/id.html +++ b/prototype/src/main/resources/templates/intern/listedArticles/id.html @@ -30,6 +30,7 @@

+ Details

-- 2.50.1