apskel-pos-backend/internal/repository/payment_order_item_repository.go
2025-08-07 22:45:02 +07:00

63 lines
2.0 KiB
Go

package repository
import (
"context"
"apskel-pos-be/internal/entities"
"github.com/google/uuid"
"gorm.io/gorm"
)
type PaymentOrderItemRepository interface {
Create(ctx context.Context, paymentOrderItem *entities.PaymentOrderItem) error
GetByID(ctx context.Context, id uuid.UUID) (*entities.PaymentOrderItem, error)
GetByPaymentID(ctx context.Context, paymentID uuid.UUID) ([]*entities.PaymentOrderItem, error)
Update(ctx context.Context, paymentOrderItem *entities.PaymentOrderItem) error
Delete(ctx context.Context, id uuid.UUID) error
}
type PaymentOrderItemRepositoryImpl struct {
db *gorm.DB
}
func NewPaymentOrderItemRepositoryImpl(db *gorm.DB) *PaymentOrderItemRepositoryImpl {
return &PaymentOrderItemRepositoryImpl{
db: db,
}
}
func (r *PaymentOrderItemRepositoryImpl) Create(ctx context.Context, paymentOrderItem *entities.PaymentOrderItem) error {
return r.db.WithContext(ctx).Create(paymentOrderItem).Error
}
func (r *PaymentOrderItemRepositoryImpl) GetByID(ctx context.Context, id uuid.UUID) (*entities.PaymentOrderItem, error) {
var paymentOrderItem entities.PaymentOrderItem
err := r.db.WithContext(ctx).
Preload("Payment").
Preload("OrderItem").
First(&paymentOrderItem, "id = ?", id).Error
if err != nil {
return nil, err
}
return &paymentOrderItem, nil
}
func (r *PaymentOrderItemRepositoryImpl) GetByPaymentID(ctx context.Context, paymentID uuid.UUID) ([]*entities.PaymentOrderItem, error) {
var paymentOrderItems []*entities.PaymentOrderItem
err := r.db.WithContext(ctx).
Preload("Payment").
Preload("OrderItem").
Where("payment_id = ?", paymentID).
Find(&paymentOrderItems).Error
return paymentOrderItems, err
}
func (r *PaymentOrderItemRepositoryImpl) Update(ctx context.Context, paymentOrderItem *entities.PaymentOrderItem) error {
return r.db.WithContext(ctx).Save(paymentOrderItem).Error
}
func (r *PaymentOrderItemRepositoryImpl) Delete(ctx context.Context, id uuid.UUID) error {
return r.db.WithContext(ctx).Delete(&entities.PaymentOrderItem{}, "id = ?", id).Error
}