test/orm #3

Closed
Seil0 wants to merge 3 commits from test/orm into master
10 changed files with 208 additions and 35 deletions

2
.gitignore vendored
View File

@ -88,4 +88,4 @@ local.properties
*.launch *.launch
# SQLite # SQLite
prototype/src/main/resources/*.db prototype/*.db

View File

@ -3,7 +3,7 @@ buildscript {
mavenCentral() mavenCentral()
} }
dependencies { 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")
} }
} }

View File

@ -1,7 +1,9 @@
package org.hso.ecommerce.app; package org.hso.ecommerce.app;
import org.hso.ecommerce.contoller.Login; import org.hso.ecommerce.contoller.Login;
import org.hso.ecommerce.db.ArticleRepository;
import org.hso.ecommerce.db.CustomerRepository; import org.hso.ecommerce.db.CustomerRepository;
import org.hso.ecommerce.entities.Article;
import org.hso.ecommerce.entities.Customer; import org.hso.ecommerce.entities.Customer;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
@ -18,7 +20,16 @@ import java.util.UUID;
@Controller @Controller
public class RequestController { 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("/") @GetMapping("/")
public String home() { public String home() {
@ -32,13 +43,13 @@ public class RequestController {
@PostMapping("/login") @PostMapping("/login")
public String loginPost(HttpServletResponse response, @RequestParam(value = "goto", required = false) String gto) { 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("/")) { if (gto != null && gto.startsWith("/")) {
return "redirect:" + gto; return "redirect:" + gto;
} else { } else {
return "redirect:/"; return "redirect:/";
} }
} }
@PostMapping("/logout") @PostMapping("/logout")
@ -71,10 +82,11 @@ public class RequestController {
public String shopCheckoutFinish() { public String shopCheckoutFinish() {
return "shop/checkoutFinish"; return "shop/checkoutFinish";
} }
@GetMapping("/shop/checkoutFinish") @GetMapping("/shop/checkoutFinish")
public String shopCheckoutFinishGET() { public String shopCheckoutFinishGET() {
return "shop/checkoutFinish"; return "shop/checkoutFinish";
} }
@GetMapping("/shop/articles/{id}") @GetMapping("/shop/articles/{id}")
public String shopArticlesById() { public String shopArticlesById() {
@ -82,13 +94,13 @@ public class RequestController {
} }
@PostMapping("/shop/articles/{id}") @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) { if (isCustomer) {
return "redirect:/shop/checkout"; return "redirect:/shop/checkout";
} else { } else {
return "redirect:/login?goto=%2Fshop%2Farticles%2F"+id; return "redirect:/login?goto=%2Fshop%2Farticles%2F" + id;
} }
} }
@GetMapping("/user/") @GetMapping("/user/")
public String user() { public String user() {
@ -143,10 +155,37 @@ public class RequestController {
} }
@GetMapping("/intern/listedArticles/{id}") @GetMapping("/intern/listedArticles/{id}")
public String internListedArticlesId() { public String internListedArticlesId(Model model) {
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"; return "intern/listedArticles/id";
} }
@RequestMapping(value="/updateArticleAction", method=RequestMethod.POST, params="action=updateArticleAction")
public String updateArticleAction(@ModelAttribute Article article, HttpServletResponse response) {
articleRepo.save(article);
return "redirect:intern/listedArticles/id";
}
@GetMapping("/intern/articles/") @GetMapping("/intern/articles/")
public String internArticles() { public String internArticles() {
@ -235,18 +274,23 @@ public class RequestController {
@PostMapping("/intern/warehouse/progress/{id}") @PostMapping("/intern/warehouse/progress/{id}")
public String accountingWarehouseProgressIdPost(HttpServletResponse response) { public String accountingWarehouseProgressIdPost(HttpServletResponse response) {
if((notSoRandom++) % 2 == 1) { if ((notSoRandom++) % 2 == 1) {
return "redirect:/intern/warehouse/progress/450"; return "redirect:/intern/warehouse/progress/450";
} else { } else {
response.setStatus(409); response.setStatus(409);
return "intern/warehouse/error_progress_failed"; return "intern/warehouse/error_progress_failed";
} }
} }
@GetMapping("/intern/warehouse/progress/{id}") @GetMapping("/intern/warehouse/progress/{id}")
public String accountingWarehouseProgressId() { 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/") @GetMapping("/intern/warehouse/slots/")
public String accountingWarehouseSlots() { public String accountingWarehouseSlots() {

View File

@ -0,0 +1,23 @@
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;
@Repository
public interface ArticleRepository extends JpaRepository<Article, Long> {
@Query("SELECT a FROM Article a WHERE a.title = :title")
List<Article> findByTitle(String title);
@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);
}

View File

@ -5,7 +5,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource; import javax.sql.DataSource;

View File

@ -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 float sellingPrice;
public float 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 float getSellingPrice() {
return sellingPrice;
}
public void setSellingPrice(float sellingPrice) {
this.sellingPrice = sellingPrice;
}
public float getMaxPurchasePrice() {
return maxPurchasePrice;
}
public void setMaxPurchasePrice(float 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;
}
}

View File

@ -7,7 +7,7 @@ import javax.persistence.*;
public class Customer { public class Customer {
@Id @Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "id_Sequence") @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_Sequence")
@SequenceGenerator(name = "id_Sequence", sequenceName = "ID_SEQ") @SequenceGenerator(name = "id_Sequence", sequenceName = "ID_SEQ")
public Long id; public Long id;
public String lastname; public String lastname;

View File

@ -9,6 +9,7 @@ spring.datasource.url = jdbc:sqlite:./test.db
spring.datasource.driverClassName = org.sqlite.JDBC spring.datasource.driverClassName = org.sqlite.JDBC
spring.jpa.properties.hibernate.dialect = org.hso.ecommerce.db.SQLiteDialect spring.jpa.properties.hibernate.dialect = org.hso.ecommerce.db.SQLiteDialect
spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.ddl-auto=update
#spring.jpa.show-sql=true
# ---------------------------------------- # ----------------------------------------
# WEB PROPERTIES # WEB PROPERTIES

View File

@ -5,3 +5,15 @@ CREATE TABLE "customers" (
"username" TEXT, "username" TEXT,
"password" TEXT "password" TEXT
); );
CREATE TABLE "article" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"title" TEXT,
"category" TEXT,
"description" TEXT,
"refArticle" INTEGER,
"sellingPrice" FLOAT,
"maxPurchasePrice" FLOAT,
"maxStock" INTEGER,
"currentStock" INTEGER
);

View File

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