diff --git a/controllers/shopItemController.go b/controllers/shopItemController.go index 8db096f..a26d511 100644 --- a/controllers/shopItemController.go +++ b/controllers/shopItemController.go @@ -27,6 +27,8 @@ type ShopItemController interface { ShopItemView(*gin.Context) AddItemView(*gin.Context) AddItemHandler(*gin.Context) + AddItemsView(*gin.Context) + AddItemsHandler(*gin.Context) CreateTag(*gin.Context) GetAllTags(*gin.Context) EditItemView(*gin.Context) @@ -316,6 +318,94 @@ func (rc *shopItemController) AddItemHandler(c *gin.Context) { c.HTML(http.StatusOK, "additem.html", data) } +func (rc *shopItemController) AddItemsView(c *gin.Context) { + data := CreateSessionData(c, gin.H{}) + + c.HTML(http.StatusOK, "batchupload.html", data) +} + +func (rc *shopItemController) AddItemsHandler(c *gin.Context) { + errorHandler := func(err error) { + data := CreateSessionData(c, gin.H{ + "error": err, + }) + c.HTML(http.StatusBadRequest, "batchupload.html", data) + } + + form, err := c.MultipartForm() + + if err != nil { + errorHandler(err) + return + } + + files := form.File["pdf"] + + var shopItems []models.ShopItem + for _, file := range files { + dstPdf := filepath.Join("static/uploads", file.Filename) + + if err := c.SaveUploadedFile(file, dstPdf); err != nil { + errorHandler(err) + return + } + + dstImage := dstPdf + ".preview.png" + cmd := exec.Command("pdftoppm", "-png", "-singlefile", dstPdf, dstPdf+".preview") + _, err := cmd.Output() + + if err != nil { + fmt.Println("Error during pdftoppm: ", err.Error()) + } + + category, err := models.ParseCategory("Zine") + if err != nil { + errorHandler(err) + return + } + + variants := []models.ItemVariant{ + { + Name: "B/W", + Price: 1.0, + }, + } + + shopItem := models.ShopItem{ + Name: file.Filename, + Abstract: file.Filename, + Description: file.Filename, + Category: category, + IsPublic: true, + BasePrice: rc.GetBasePrice(variants), + Image: dstImage, + Pdf: dstPdf, + Variants: variants, + } + + _, err = repositories.ShopItems.Create(shopItem) + if err != nil { + errorHandler(err) + return + } + + shopItems = append(shopItems, shopItem) + } + + msg := "The Following items were registered:\n" + + for _, item := range shopItems { + msg += fmt.Sprintf("%s\n", item.Name) + } + + data := CreateSessionData(c, gin.H{ + "error": "", + "success": msg, + }) + + c.HTML(http.StatusOK, "batchupload.html", data) +} + func (rc *shopItemController) EditItemView(c *gin.Context) { shopItem, err := repositories.ShopItems.GetById(c.Param("id")) tags, err := repositories.Tags.GetAll() diff --git a/main.go b/main.go index b8e7b8e..141a577 100644 --- a/main.go +++ b/main.go @@ -104,9 +104,11 @@ func main() { viewRoutes.GET("/register", userController.RegisterView) viewRoutes.GET("/passwordreset", userController.ResetView) viewRoutes.GET("/additem", authValidator.RequireAuth, shopItemController.AddItemView) + viewRoutes.GET("/batchupload", authValidator.RequireAuth, shopItemController.AddItemsView) viewRoutes.POST("/login", userController.LoginHandler) viewRoutes.POST("/register", userController.RegisterHandler) viewRoutes.POST("/additem", authValidator.RequireAuth, shopItemController.AddItemHandler) + viewRoutes.POST("/batchupload", authValidator.RequireAuth, shopItemController.AddItemsHandler) viewRoutes.POST("/passwordreset", userController.ResetHandler) } diff --git a/models/printer.go b/models/printer.go index d307cb5..be8e6bb 100644 --- a/models/printer.go +++ b/models/printer.go @@ -50,6 +50,8 @@ func (p *PrintJob) Execute() error { baseCommand += fmt.Sprintf(" %v ", option) } + baseCommand += fmt.Sprintf(" -- %s", p.Pdf) + fmt.Println(baseCommand) return nil } diff --git a/views/batchupload.html b/views/batchupload.html new file mode 100644 index 0000000..1ddff0d --- /dev/null +++ b/views/batchupload.html @@ -0,0 +1,53 @@ +{{ template "header.html" . }} + +
+
+ Pressing Print will automatically print the given Zines for you.
+ Add Zines for printing simply by adding them to the Cart
+
- Welcome to the Zineshop Printservice.
- Pressing Print will automatically print the given Zines for you.
- All you have to do is set an Amount for each Zine you want to print.
diff --git a/views/shopitem.html b/views/shopitem.html
index fe9def4..3f90f00 100644
--- a/views/shopitem.html
+++ b/views/shopitem.html
@@ -77,25 +77,4 @@