mirror of
https://github.com/serty2005/rmser.git
synced 2026-02-05 03:12:34 -06:00
71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
package photos
|
|
|
|
import (
|
|
"rmser/internal/domain/photos"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type pgRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewRepository(db *gorm.DB) photos.Repository {
|
|
return &pgRepository{db: db}
|
|
}
|
|
|
|
func (r *pgRepository) Create(photo *photos.ReceiptPhoto) error {
|
|
return r.db.Create(photo).Error
|
|
}
|
|
|
|
func (r *pgRepository) GetByID(id uuid.UUID) (*photos.ReceiptPhoto, error) {
|
|
var photo photos.ReceiptPhoto
|
|
err := r.db.First(&photo, id).Error
|
|
return &photo, err
|
|
}
|
|
|
|
func (r *pgRepository) GetByServerID(serverID uuid.UUID, page, limit int) ([]photos.ReceiptPhoto, int64, error) {
|
|
var items []photos.ReceiptPhoto
|
|
var total int64
|
|
|
|
offset := (page - 1) * limit
|
|
|
|
err := r.db.Model(&photos.ReceiptPhoto{}).
|
|
Where("rms_server_id = ?", serverID).
|
|
Count(&total).Error
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
err = r.db.Where("rms_server_id = ?", serverID).
|
|
Order("created_at DESC").
|
|
Offset(offset).
|
|
Limit(limit).
|
|
Find(&items).Error
|
|
|
|
return items, total, err
|
|
}
|
|
|
|
func (r *pgRepository) UpdateDraftLink(photoID uuid.UUID, draftID *uuid.UUID) error {
|
|
return r.db.Model(&photos.ReceiptPhoto{}).
|
|
Where("id = ?", photoID).
|
|
Update("draft_id", draftID).Error
|
|
}
|
|
|
|
func (r *pgRepository) UpdateInvoiceLink(photoID uuid.UUID, invoiceID *uuid.UUID) error {
|
|
return r.db.Model(&photos.ReceiptPhoto{}).
|
|
Where("id = ?", photoID).
|
|
Update("invoice_id", invoiceID).Error
|
|
}
|
|
|
|
func (r *pgRepository) ClearDraftLinkByDraftID(draftID uuid.UUID) error {
|
|
return r.db.Model(&photos.ReceiptPhoto{}).
|
|
Where("draft_id = ?", draftID).
|
|
Update("draft_id", nil).Error
|
|
}
|
|
|
|
func (r *pgRepository) Delete(id uuid.UUID) error {
|
|
return r.db.Delete(&photos.ReceiptPhoto{}, id).Error
|
|
}
|