This commit is contained in:
82
repositories/InvoiceRepository.go
Normal file
82
repositories/InvoiceRepository.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user