96 lines
2.9 KiB
Go
96 lines
2.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/warehouse/query"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type WarehouseRepository interface {
|
|
Create(ctx context.Context, tx *gorm.DB, warehouse entities.MWarehouseEntity) (entities.MWarehouseEntity, error)
|
|
GetById(ctx context.Context, tx *gorm.DB, warehouseId string) (entities.MWarehouseEntity, error)
|
|
GetAll(ctx context.Context, filter query.WarehouseFilter) ([]entities.MWarehouseEntity, int64, error)
|
|
Update(ctx context.Context, tx *gorm.DB, warehouse entities.MWarehouseEntity) (entities.MWarehouseEntity, error)
|
|
Delete(ctx context.Context, tx *gorm.DB, warehouseId string) error
|
|
}
|
|
|
|
type warehouseRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewWarehouseRepository(db *gorm.DB) WarehouseRepository {
|
|
return &warehouseRepository{db: db}
|
|
}
|
|
|
|
func (r *warehouseRepository) Create(ctx context.Context, tx *gorm.DB, warehouse entities.MWarehouseEntity) (entities.MWarehouseEntity, error) {
|
|
if tx == nil {
|
|
tx = r.db
|
|
}
|
|
if err := tx.WithContext(ctx).Create(&warehouse).Error; err != nil {
|
|
return warehouse, err
|
|
}
|
|
return warehouse, nil
|
|
}
|
|
|
|
func (r *warehouseRepository) GetById(ctx context.Context, tx *gorm.DB, warehouseId string) (entities.MWarehouseEntity, error) {
|
|
if tx == nil {
|
|
tx = r.db
|
|
}
|
|
var warehouse entities.MWarehouseEntity
|
|
if err := tx.WithContext(ctx).
|
|
Preload("Client").
|
|
Preload("PIC").
|
|
Preload("Zonas").
|
|
Preload("Zonas.Client").
|
|
Preload("Zonas.Aisles").
|
|
Preload("Zonas.Aisles.Client").
|
|
Preload("Zonas.Aisles.Zona").
|
|
Preload("Zonas.Aisles.DimUom").
|
|
Preload("Zonas.Aisles.WeightUom").
|
|
First(&warehouse, "id = ?", warehouseId).Error; err != nil {
|
|
return warehouse, err
|
|
}
|
|
return warehouse, nil
|
|
}
|
|
|
|
func (r *warehouseRepository) GetAll(ctx context.Context, filter query.WarehouseFilter) ([]entities.MWarehouseEntity, int64, error) {
|
|
var warehouses []entities.MWarehouseEntity
|
|
var total int64
|
|
db := query.ApplyWarehouseFilters(r.db, filter)
|
|
db.Model(&entities.MWarehouseEntity{}).Count(&total)
|
|
if filter.PerPage > 0 && filter.Page > 0 {
|
|
db = db.Offset((filter.Page - 1) * filter.PerPage).Limit(filter.PerPage)
|
|
}
|
|
if err :=
|
|
db.Preload("Client").Preload("PIC").
|
|
Find(&warehouses).Error; err != nil {
|
|
return warehouses, total, err
|
|
}
|
|
return warehouses, total, nil
|
|
}
|
|
|
|
func (r *warehouseRepository) Update(ctx context.Context, tx *gorm.DB, warehouse entities.MWarehouseEntity) (entities.MWarehouseEntity, error) {
|
|
if tx == nil {
|
|
tx = r.db
|
|
}
|
|
if err := tx.WithContext(ctx).Model(&entities.MWarehouseEntity{}).
|
|
Where("id = ?", warehouse.ID).
|
|
Select("*").
|
|
Updates(&warehouse).Error; err != nil {
|
|
return warehouse, err
|
|
}
|
|
return warehouse, nil
|
|
}
|
|
|
|
func (r *warehouseRepository) Delete(ctx context.Context, tx *gorm.DB, warehouseId string) error {
|
|
if tx == nil {
|
|
tx = r.db
|
|
}
|
|
if err := tx.WithContext(ctx).Delete(&entities.MWarehouseEntity{}, "id = ?", warehouseId).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|