store invoices and printjobs
All checks were successful
Go / build (push) Successful in 12m19s

This commit is contained in:
2025-07-01 13:38:28 +02:00
parent 4b0649439c
commit 992b9c17c3
4 changed files with 117 additions and 15 deletions

View File

@@ -160,10 +160,28 @@ func (rc *printController) PrintHandler(c *gin.Context) {
return
}
printJob.CalculatePrintCosts()
printJob, err = repositories.PrintJobs.Create(printJob)
if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{"data": gin.H{"error": err}})
return
}
printJobs = append(printJobs, printJob)
}
invoice := models.Invoice{
PrintJobs: printJobs,
PricePerClick: 0.002604,
PartCosts: 0.0067,
}
invoice, err := repositories.Invoices.Create(invoice)
if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{"data": gin.H{"error": err}})
return
}
executeJobs := func() {
for _, printJob := range printJobs {
printJob.Execute()

View File

@@ -0,0 +1,82 @@
package repositories
import (
"strconv"
"gorm.io/gorm"
"git.dynamicdiscord.de/kalipso/zineshop/models"
)
type InvoiceRepository interface {
Create(models.Invoice) (models.Invoice, error)
GetAll() ([]models.Invoice, error)
GetById(string) (models.Invoice, error)
//GetByShopItemId(string) (models.Invoice, error)
Update(models.Invoice) (models.Invoice, error)
DeleteById(string) error
}
type GORMInvoiceRepository struct {
DB *gorm.DB
}
func NewGORMInvoiceRepository(db *gorm.DB) InvoiceRepository {
return &GORMInvoiceRepository{
DB: db,
}
}
func (t *GORMInvoiceRepository) Create(invoice models.Invoice) (models.Invoice, error) {
result := t.DB.Omit("PrintJobs").Create(&invoice)
if result.Error != nil {
return models.Invoice{}, result.Error
}
return invoice, nil
}
func (t *GORMInvoiceRepository) GetAll() ([]models.Invoice, error) {
var invoice []models.Invoice
result := t.DB.Preload("PrintJobs").Find(&invoice)
return invoice, result.Error
}
func (t *GORMInvoiceRepository) GetById(id string) (models.Invoice, error) {
invoiceId, err := strconv.Atoi(id)
if err != nil {
return models.Invoice{}, err
}
var invoice models.Invoice
result := t.DB.Preload("PrintJobs").First(&invoice, uint(invoiceId))
if result.Error != nil {
return models.Invoice{}, result.Error
}
return invoice, nil
}
func (t *GORMInvoiceRepository) Update(invoice models.Invoice) (models.Invoice, error) {
result := t.DB.Save(&invoice)
if result.Error != nil {
return models.Invoice{}, result.Error
}
return invoice, nil
}
func (t *GORMInvoiceRepository) DeleteById(id string) error {
invoiceId, err := strconv.Atoi(id)
if err != nil {
return err
}
result := t.DB.Delete(&models.Invoice{}, invoiceId)
return result.Error
}

View File

@@ -27,56 +27,56 @@ func NewGORMPrintJobRepository(db *gorm.DB) PrintJobRepository {
}
}
func (t *GORMPrintJobRepository) Create(tag models.PrintJob) (models.PrintJob, error) {
result := t.DB.Create(&tag)
func (t *GORMPrintJobRepository) Create(printJob models.PrintJob) (models.PrintJob, error) {
result := t.DB.Create(&printJob)
if result.Error != nil {
return models.PrintJob{}, result.Error
}
return tag, nil
return printJob, nil
}
func (t *GORMPrintJobRepository) GetAll() ([]models.PrintJob, error) {
var tags []models.PrintJob
result := t.DB.Find(&tags)
var printJobs []models.PrintJob
result := t.DB.Preload("ShopItem").Preload("Variant").Preload("PaperType").Preload("CoverPaperType").Find(&printJobs)
return tags, result.Error
return printJobs, result.Error
}
func (t *GORMPrintJobRepository) GetById(id string) (models.PrintJob, error) {
tagId, err := strconv.Atoi(id)
printJobId, err := strconv.Atoi(id)
if err != nil {
return models.PrintJob{}, err
}
var tag models.PrintJob
result := t.DB.First(&tag, uint(tagId))
var printJob models.PrintJob
result := t.DB.Preload("ShopItem").Preload("Variant").Preload("PaperType").Preload("CoverPaperType").First(&printJob, uint(printJobId))
if result.Error != nil {
return models.PrintJob{}, result.Error
}
return tag, nil
return printJob, nil
}
func (t *GORMPrintJobRepository) Update(tag models.PrintJob) (models.PrintJob, error) {
result := t.DB.Save(&tag)
func (t *GORMPrintJobRepository) Update(printJob models.PrintJob) (models.PrintJob, error) {
result := t.DB.Save(&printJob)
if result.Error != nil {
return models.PrintJob{}, result.Error
}
return tag, nil
return printJob, nil
}
func (t *GORMPrintJobRepository) DeleteById(id string) error {
tagId, err := strconv.Atoi(id)
printJobId, err := strconv.Atoi(id)
if err != nil {
return err
}
result := t.DB.Delete(&models.PrintJob{}, tagId)
result := t.DB.Delete(&models.PrintJob{}, printJobId)
return result.Error
}

View File

@@ -18,6 +18,7 @@ var (
ConfigOptions ConfigRepository
Papers PaperRepository
PrintJobs PrintJobRepository
Invoices InvoiceRepository
)
func InitRepositories() {
@@ -51,4 +52,5 @@ func InitRepositories() {
ConfigOptions = NewGORMConfigRepository(db)
Papers = NewGORMPaperRepository(db)
PrintJobs = NewGORMPrintJobRepository(db)
Invoices = NewGORMInvoiceRepository(db)
}