51 lines
1.5 KiB
Go
51 lines
1.5 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"kontor-api-gin/internal/models"
|
|
"log/slog"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type BookshelfPublisherRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewBookshelfPublisherRepository(db *gorm.DB) *BookshelfPublisherRepository {
|
|
repo := &BookshelfPublisherRepository{db: db}
|
|
return repo
|
|
}
|
|
|
|
func (repository *BookshelfPublisherRepository) FindAll(context context.Context) ([]models.BookshelfPublisher, error) {
|
|
var bookshelfPublishers []models.BookshelfPublisher
|
|
|
|
result := repository.db.Find(&bookshelfPublishers)
|
|
slog.Info("BookshelfPublisherRepository#FindAll", "count", result.RowsAffected)
|
|
|
|
return bookshelfPublishers, nil
|
|
}
|
|
|
|
func (repository *BookshelfPublisherRepository) Import(dtos models.BookshelfPublisherDTOList) {
|
|
slog.Info("BookshelfPublisherRepository#Import")
|
|
for index, dto := range dtos {
|
|
slog.Info("importing", "index", index, "profile", dto)
|
|
model := dto.ConvertDTOToModel()
|
|
existing_item := models.BookshelfPublisher{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())
|
|
}
|
|
}
|
|
}
|
|
}
|