add batchupload

This commit is contained in:
2025-04-10 15:44:59 +02:00
parent f87a6352dd
commit fdb11bc57c
7 changed files with 159 additions and 24 deletions

View File

@@ -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()