Files
Thomas Peetz 0750621b96
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 6s
Gitea Actions Demo / build-and-test (push) Failing after 19m24s
add methods in Repository and Model for importing data
2026-10-29 17:40:54 +01:00

69 lines
1.8 KiB
Go

package repository
import (
"context"
"kontor-api-gin/internal/models"
"log/slog"
"gorm.io/gorm"
)
type SportRepository struct {
db *gorm.DB
}
func (repository *SportRepository) Import(sports models.SportDTOList) {
slog.Info("SportRepository#Import")
for index, sport := range sports {
slog.Info("importing", "index", index, "profile", sport)
model := sport.ConvertDTOToModel()
existing_item := models.Sport{ID: sport.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 NewSportRepository(db *gorm.DB) *SportRepository {
repo := &SportRepository{db: db}
return repo
}
func (repository *SportRepository) FindAll(page int, limit int, context context.Context) (sports []models.Sport, total int64, err error) {
offset := (page - 1) * limit
err = repository.db.
Model(&models.Sport{}).
Count(&total).
Limit(limit).
Offset(offset).
Order("id asc").
Find(&sports).Error
if err != nil {
return
}
slog.Info("SportRepository#FindAll", "total", total)
return
}
func (repository *SportRepository) GetById(context context.Context, id string) (models.Sport, error) {
var sport = models.Sport{ID: id}
slog.Info("SportRepository#GetById", "id", id)
result := repository.db.First(&sport)
if result.Error != nil {
slog.Info("SportRepository#GetById", "error", result.Error.Error())
}
return sport, result.Error
}