code cleanup

This commit is contained in:
Hendrik Schutter 2020-05-15 18:13:12 +02:00
parent 197a774fb2
commit 56efbd7940
5 changed files with 105 additions and 76 deletions

View File

@ -3,11 +3,10 @@ package org.hso.ecommerce.controller.intern;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hso.ecommerce.entities.shop.Article;
import org.hso.ecommerce.entities.shop.Category;
import org.hso.ecommerce.repos.shop.ArticleRepository;
import org.hso.ecommerce.repos.shop.CategoryRepository;
import org.hso.ecommerce.repos.warehouse.WarehouseBookingPositionSlotEntryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@ -24,6 +23,9 @@ public class InternArticleController {
@Autowired
private final WarehouseBookingPositionSlotEntryRepository warehouseEntryRepository = null;
@Autowired
private final CategoryRepository categoryRepository = null;
@GetMapping("/")
public String internListedArticles(Model model) {
@ -56,40 +58,40 @@ public class InternArticleController {
@PostMapping("/{id}/saveChanges")
public RedirectView saveChanges(
@ModelAttribute UImodelArticle changedArticleTotal,
@RequestParam("units-per-slot") String sWarehouseUnitsPerSlot,
@RequestParam("price_netto") String sPrice_netto,
@RequestParam("reorderMaxPrice") String sReorderMaxPrice,
@RequestParam("autobuy") String sShouldReorder,
@RequestParam("categories") String sCategories) {
@PathVariable int id,
@RequestParam("title") String title,
@RequestParam("description") String description,
@RequestParam("units-per-slot") String warehouseUnitsPerSlot,
@RequestParam("price_netto") String pricenetto,
@RequestParam("reorderMaxPrice") String reorderMaxPrice,
@RequestParam("autobuy") Boolean shouldReorder,
@RequestParam("categories") String categories) {
Article oldArticle = articleRepository.findArticleById(changedArticleTotal.id);
Article oldArticle = articleRepository.findArticleById(id);
// TODO img
// TODO categories
/*
* String separatedCategories[] = sCategories.split("\\r?\\n");
*
*
* for (int i = 0; i < separatedCategories.length; i++) {
*
* oldArticle.categories.add(separatedCategories[i]);
*
* }
*/
oldArticle.shouldReorder = (sShouldReorder.equals("true")) ? true : false;
oldArticle.reorderMaxPrice = (int) (Float.parseFloat(sReorderMaxPrice) * 100);
oldArticle.shopPricePerUnitNetCent = (int) (Float.parseFloat(sPrice_netto) * 100);
oldArticle.warehouseUnitsPerSlot = Integer.parseInt(sWarehouseUnitsPerSlot);
oldArticle.title = changedArticleTotal.title;
oldArticle.description = changedArticleTotal.description;
String separatedCategories[] = categories.split("\n");
oldArticle.categories.clear();
// loop through all categories strings and create a new category if a new one; also adds the categorys to the article
for (String category : separatedCategories) {
oldArticle.categories.add(categoryRepository.findCategoryByName(category.trim())
.orElseGet(() -> new Category(category.trim())));
}
oldArticle.shouldReorder = shouldReorder;
oldArticle.reorderMaxPrice = (int) (Float.parseFloat(reorderMaxPrice) * 100);
oldArticle.shopPricePerUnitNetCent = (int) (Float.parseFloat(pricenetto) * 100);
oldArticle.warehouseUnitsPerSlot = Integer.parseInt(warehouseUnitsPerSlot);
oldArticle.title = title;
oldArticle.description = description;
articleRepository.save(oldArticle); // save updated article
return new RedirectView("../"); //return to overview page
return new RedirectView("../"); // return to overview page
}
public static class UImodelArticles {
public String imgPath;
@ -113,7 +115,14 @@ public class InternArticleController {
this.title = article.title;
this.price_netto = String.format("%.2f", ((float) article.shopPricePerUnitNetCent / 100));
this.price = String.format("%.2f", ((float) article.getPriceGross() / 100));
this.categorie = article.getCategories();
StringBuilder result = new StringBuilder();
for (Category temp : article.categories) {
result.append(temp.name + " ");
}
this.categorie = result.toString();
this.stock = stock;
this.offer_id = article.related.id;
this.id = article.id;
@ -236,7 +245,15 @@ public class InternArticleController {
this.title = article.title;
this.price_netto = String.format("%.2f", ((float) article.shopPricePerUnitNetCent / 100));
this.price = String.format("%.2f", ((float) article.getPriceGross() / 100));
this.categorie = article.getCategories();
StringBuilder result = new StringBuilder();
for (Category temp : article.categories) {
result.append(temp.name);
result.append("\n");
}
this.categorie = result.toString();
this.stock = stock;
this.offer_id = article.related.id;
this.id = article.id;

View File

@ -9,56 +9,41 @@ import java.util.Set;
@Entity
@Table(name = "articles")
public class Article
{
public class Article {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic
public long id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic
public long id;
@ManyToOne(optional = false)
public ArticleOffer related;
@ManyToOne(optional = false)
public ArticleOffer related;
public int shopPricePerUnitNetCent;
public int shopPricePerUnitNetCent;
public int warehouseUnitsPerSlot;
public int warehouseUnitsPerSlot;
public boolean shouldReorder;
public int reorderMaxPrice;
public boolean shouldReorder;
@NotNull
public String title;
public int reorderMaxPrice;
@NotNull
public String description;
@NotNull
public String title;
@OneToOne(optional = true)
@Basic(fetch = FetchType.LAZY)
public Image image;
@NotNull
public String description;
@ManyToMany
@JoinTable(name = "article_categories_bindings")
public Set<Category> categories = new HashSet<>();
@OneToOne(optional = true)
@Basic(fetch = FetchType.LAZY)
public Image image;
public int getVat() {
return (shopPricePerUnitNetCent * related.vatPercent) / 100;
}
@ManyToMany
@JoinTable(name = "article_categories_bindings")
public Set<Category> categories = new HashSet<>();
// returns all categories as string
public String getCategories()
{
StringBuilder result = new StringBuilder();
for (Category temp : categories) {
result.append(temp.name);
}
return result.toString();
}
public int getVat()
{
return (shopPricePerUnitNetCent * related.vatPercent) / 100;
}
public int getPriceGross()
{
return shopPricePerUnitNetCent + getVat();
}
}
public int getPriceGross() {
return shopPricePerUnitNetCent + getVat();
}
}

View File

@ -20,4 +20,15 @@ public class Category {
@ManyToMany(mappedBy = "categories")
public Set<Article> articles = new HashSet<>();
public Category() {
}
public Category (String name) {
this.name = name;
}
}

View File

@ -0,0 +1,17 @@
package org.hso.ecommerce.repos.shop;
import org.hso.ecommerce.entities.shop.Category;
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.Optional;
@Repository
public interface CategoryRepository extends JpaRepository<Category, Long> {
@Query("SELECT a FROM Category a WHERE a.name = :name")
Optional<Category> findCategoryByName(@Param("name") String name);
}

View File

@ -27,8 +27,7 @@
<h2>Gelisteter Artikel ID <span th:text="${ArticleID.id}"></span></h2>
<form class="detailgrid" action="#" th:action="@{/intern/articles/{id}/saveChanges(id = ${ArticleID.id})}" th:object="${ArticleID}" method="POST">
<form class="detailgrid" action="#" th:action="@{/intern/articles/{id}/saveChanges(id = ${ArticleID.id})}" th:object="${ArticleID}" method="POST" enctype="multipart/form-data">
<p class="m">
<label for="title">Titel</label>