2020-04-29 22:44:16 +02:00
package org.hso.ecommerce.controller.intern ;
2020-04-28 22:41:29 +02:00
2020-05-10 15:26:50 +02:00
import java.util.ArrayList ;
2020-05-09 23:01:25 +02:00
import java.util.List ;
2020-05-12 22:52:01 +02:00
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;
2020-05-09 23:01:25 +02:00
import org.hso.ecommerce.entities.shop.Article ;
import org.hso.ecommerce.repos.shop.ArticleRepository ;
2020-05-10 15:26:50 +02:00
import org.hso.ecommerce.repos.warehouse.WarehouseBookingPositionSlotEntryRepository ;
2020-05-09 23:01:25 +02:00
import org.springframework.beans.factory.annotation.Autowired ;
2020-04-28 22:41:29 +02:00
import org.springframework.stereotype.Controller ;
2020-05-09 23:01:25 +02:00
import org.springframework.ui.Model ;
import org.springframework.web.bind.annotation.* ;
2020-05-11 15:32:39 +02:00
import org.springframework.web.servlet.view.RedirectView ;
2020-05-09 23:01:25 +02:00
2020-05-10 15:26:50 +02:00
@Controller
2020-05-11 14:32:07 +02:00
@RequestMapping ( " intern/articles " )
2020-05-11 11:58:26 +02:00
public class InternArticleController {
2020-05-10 15:26:50 +02:00
@Autowired
private final ArticleRepository articleRepository = null ;
2020-05-09 23:01:25 +02:00
2020-05-10 15:26:50 +02:00
@Autowired
private final WarehouseBookingPositionSlotEntryRepository warehouseEntryRepository = null ;
2020-05-09 23:01:25 +02:00
2020-05-10 15:26:50 +02:00
@GetMapping ( " / " )
2020-05-11 11:58:26 +02:00
public String internListedArticles ( Model model ) {
2020-05-10 15:26:50 +02:00
List < ListedArticlesListTotals > totals = new ArrayList < ListedArticlesListTotals > ( ) ;
for ( Article article : articleRepository . findAll ( ) ) {
ListedArticlesListTotals tmp = new ListedArticlesListTotals ( ) ;
2020-05-11 11:58:26 +02:00
tmp . addListedArticle ( article , warehouseEntryRepository . getArticleStock ( article . id ) . orElse ( 0 ) ) ;
2020-05-10 15:26:50 +02:00
totals . add ( tmp ) ;
}
model . addAttribute ( " ListedArticles " , totals ) ;
return " intern/listedArticles/index " ;
}
@GetMapping ( " /{id} " )
2020-05-11 11:58:26 +02:00
public String internListedArticlesId ( Model model , @PathVariable String id ) {
int articleid = Integer . parseInt ( id ) ;
2020-05-12 22:52:01 +02:00
2020-05-11 11:58:26 +02:00
ListedArticlesListIdTotal total = new ListedArticlesListIdTotal ( ) ;
2020-05-12 22:52:01 +02:00
2020-05-11 11:58:26 +02:00
total . addArticle ( articleRepository . findArticleById ( articleid ) ,
warehouseEntryRepository . getArticleStock ( articleid ) . orElse ( 0 ) ) ;
2020-05-12 22:52:01 +02:00
// System.out.println("GETshouldReorder: " + articleRepository.findArticleById(articleid).shouldReorder);
2020-05-11 11:58:26 +02:00
model . addAttribute ( " ArticleID " , total ) ;
2020-05-10 15:26:50 +02:00
return " intern/listedArticles/id " ;
}
2020-05-12 22:52:01 +02:00
2020-05-11 15:32:39 +02:00
@PostMapping ( " /{id}/saveChanges " )
2020-05-12 22:52:01 +02:00
public RedirectView saveChanges ( @ModelAttribute ListedArticlesListIdTotal changedArticleTotal ) {
Article oldArticle = articleRepository . findArticleById ( changedArticleTotal . id ) ;
//TODO img
//TODO price
//TODO reorder price
//TODO categories
2020-05-11 14:32:07 +02:00
2020-05-12 22:52:01 +02:00
// oldArticle.shopPricePerUnitNetCent = changedArticleTotal.price_netto;
// System.out.println("netto: " + changedArticleTotal.price_netto);
2020-05-11 14:32:07 +02:00
2020-05-12 22:52:01 +02:00
//System.out.println("POSTshouldReorder: " + oldArticle.shouldReorder);
//oldArticle.shouldReorder = changedArticleTotal.shouldReorder;
System . out . println ( " POSTwarehouseUnitsPerSlot: " + oldArticle . warehouseUnitsPerSlot ) ;
2020-05-11 14:32:07 +02:00
2020-05-12 22:52:01 +02:00
//oldArticle.warehouseUnitsPerSlot = Integer.parseInt(changedArticleTotal.warehouseUnitsPerSlot);
oldArticle . title = changedArticleTotal . title ; //works
oldArticle . description = changedArticleTotal . description ; //works
articleRepository . save ( oldArticle ) ; // save updated article
2020-05-11 15:32:39 +02:00
return new RedirectView ( " ../ " ) ;
2020-05-12 22:52:01 +02:00
}
2020-05-10 15:26:50 +02:00
2020-05-11 11:58:26 +02:00
public static class ListedArticlesListTotals {
2020-05-10 15:26:50 +02:00
public String imgPath ;
public String title ;
public String price ;
public String price_netto ;
public String categorie ;
public int stock ;
public long offer_id ;
public long id ;
2020-05-11 11:58:26 +02:00
void addListedArticle ( Article article , int stock ) {
this . imgPath = article . image . path ;
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 ( ) ;
this . stock = stock ;
this . offer_id = article . related . id ;
this . id = article . id ;
}
}
public static class ListedArticlesListIdTotal {
2020-05-12 22:52:01 +02:00
public String getImgPath ( ) {
return imgPath ;
}
public void setImgPath ( String imgPath ) {
this . imgPath = imgPath ;
}
public String getTitle ( ) {
return title ;
}
public void setTitle ( String title ) {
this . title = title ;
}
public String getPrice ( ) {
return price ;
}
public void setPrice ( String price ) {
this . price = price ;
}
public String getPrice_netto ( ) {
return price_netto ;
}
public void setPrice_netto ( String price_netto ) {
this . price_netto = price_netto ;
}
public int getReorderMaxPrice ( ) {
return reorderMaxPrice ;
}
public void setReorderMaxPrice ( int reorderMaxPrice ) {
this . reorderMaxPrice = reorderMaxPrice ;
}
public String getCategorie ( ) {
return categorie ;
}
public void setCategorie ( String categorie ) {
this . categorie = categorie ;
}
public int getStock ( ) {
return stock ;
}
public void setStock ( int stock ) {
this . stock = stock ;
}
public long getOffer_id ( ) {
return offer_id ;
}
public void setOffer_id ( long offer_id ) {
this . offer_id = offer_id ;
}
public long getId ( ) {
return id ;
}
public void setId ( long id ) {
this . id = id ;
}
public boolean isShouldReorder ( ) {
return shouldReorder ;
}
public void setShouldReorder ( boolean shouldReorder ) {
this . shouldReorder = shouldReorder ;
}
public String getWarehouseUnitsPerSlot ( ) {
return warehouseUnitsPerSlot ;
}
public void setWarehouseUnitsPerSlot ( String warehouseUnitsPerSlot ) {
this . warehouseUnitsPerSlot = warehouseUnitsPerSlot ;
}
public String getDescription ( ) {
return description ;
}
public void setDescription ( String description ) {
this . description = description ;
}
2020-05-11 11:58:26 +02:00
public String imgPath ;
public String title ;
public String price ;
public String price_netto ;
public int reorderMaxPrice ;
public String categorie ;
public int stock ;
public long offer_id ;
public long id ;
public boolean shouldReorder ;
2020-05-12 22:52:01 +02:00
public String warehouseUnitsPerSlot ;
2020-05-11 11:58:26 +02:00
public String description ;
void addArticle ( Article article , int stock ) {
2020-05-10 15:26:50 +02:00
this . imgPath = article . image . path ;
this . title = article . title ;
2020-05-11 11:58:26 +02:00
this . price_netto = String . format ( " %.2f " , ( ( float ) article . shopPricePerUnitNetCent / 100 ) ) ;
this . price = String . format ( " %.2f " , ( ( float ) article . getPriceGross ( ) / 100 ) ) ;
2020-05-10 15:26:50 +02:00
this . categorie = article . getCategories ( ) ;
this . stock = stock ;
this . offer_id = article . related . id ;
this . id = article . id ;
2020-05-11 11:58:26 +02:00
this . reorderMaxPrice = article . reorderMaxPrice ;
this . shouldReorder = article . shouldReorder ;
2020-05-12 22:52:01 +02:00
this . warehouseUnitsPerSlot = String . valueOf ( article . warehouseUnitsPerSlot ) ;
2020-05-11 11:58:26 +02:00
this . description = article . description ;
2020-05-10 15:26:50 +02:00
}
}
2020-04-28 22:41:29 +02:00
}