61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"kontor-api-gin/internal/models"
|
|
"log/slog"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type VendorRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func (repository *VendorRepository) Import(dtos models.VendorDTOList) {
|
|
slog.Info("VendorRepository#Import")
|
|
for index, dto := range dtos {
|
|
slog.Info("importing", "index", index, "profile", dto)
|
|
model := dto.ConvertDTOToModel()
|
|
existing_item := models.Vendor{ID: dto.ID}
|
|
result_existing := repository.db.First(&existing_item)
|
|
if result_existing.Error != nil {
|
|
slog.Info("check existing id: not found - inserting")
|
|
result := repository.db.Create(&model)
|
|
if result.Error != nil {
|
|
slog.Info("error during import", "error", result.Error.Error())
|
|
}
|
|
} else {
|
|
slog.Info("check existing id: found - updating")
|
|
result := repository.db.Save(&model)
|
|
if result.Error != nil {
|
|
slog.Info("error during updating", "error", result.Error.Error())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func NewVendorRepository(db *gorm.DB) *VendorRepository {
|
|
repo := &VendorRepository{db: db}
|
|
return repo
|
|
}
|
|
|
|
func (repository *VendorRepository) FindAll(context context.Context) ([]models.Vendor, error) {
|
|
var vendors []models.Vendor
|
|
|
|
result := repository.db.Find(&vendors)
|
|
slog.Info("VendorRepository#FindAll", "count", result.RowsAffected)
|
|
|
|
return vendors, nil
|
|
}
|
|
|
|
func (repository *VendorRepository) GetById(context context.Context, id string) (models.Vendor, error) {
|
|
var vendor = models.Vendor{ID: id}
|
|
slog.Info("VendorRepository#GetById", "id", id)
|
|
result := repository.db.First(&vendor)
|
|
if result.Error != nil {
|
|
slog.Info("VendorRepository#GetById", "error", result.Error.Error())
|
|
}
|
|
return vendor, result.Error
|
|
}
|