BE-MiniERP/modules/inventory/repository/collection_repo.go

45 lines
1023 B
Go

package repository
import (
"BE-MiniERP/modules/inventory/models"
"gorm.io/gorm"
)
type CollectionRepository struct {
DB *gorm.DB
}
func NewCollectionRepository(db *gorm.DB) *CollectionRepository {
return &CollectionRepository{DB: db}
}
func (r *CollectionRepository) FindAll(limit, offset int, search string) ([]models.Collection, error) {
var collections []models.Collection
query := r.DB.Model(&models.Collection{})
if search != "" {
query = query.Where("name ILIKE ? OR code ILIKE ?", "%"+search+"%", "%"+search+"%")
}
err := query.
Limit(limit).
Offset(offset).
Find(&collections).Error
return collections, err
}
func (r *CollectionRepository) Create(col *models.Collection) error {
return r.DB.Create(col).Error
}
func (r *CollectionRepository) Update(id uint, data *models.Collection) error {
return r.DB.Model(&models.Collection{}).Where("id = ?", id).Updates(data).Error
}
func (r *CollectionRepository) Delete(id uint) error {
return r.DB.Delete(&models.Collection{}, id).Error
}