62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"kontor-api-gin/internal/models"
|
|
"log/slog"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type ProfileRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewProfileRepository(db *gorm.DB) *ProfileRepository {
|
|
repository := &ProfileRepository{db: db}
|
|
return repository
|
|
}
|
|
|
|
func (repository *ProfileRepository) FindAll(context context.Context) ([]models.Profile, error) {
|
|
var profiles []models.Profile
|
|
|
|
result := repository.db.Find(&profiles)
|
|
slog.Info("ProfileRepository#FindAll", "count", result.RowsAffected)
|
|
|
|
return profiles, nil
|
|
}
|
|
|
|
func (repository *ProfileRepository) FindProfileByEmail(email string) (*models.Profile, error) {
|
|
var profile models.Profile
|
|
|
|
err := repository.db.Model(models.Profile{}).Where("email = ?", email).Take(&profile).Error
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &profile, nil
|
|
}
|
|
|
|
func (repository *ProfileRepository) Import(profiles models.ProfileDTOList) {
|
|
slog.Info("ProfileRepository#Import")
|
|
for index, profile := range profiles {
|
|
slog.Info("importing", "index", index, "profile", profile)
|
|
model := profile.ConvertDTOToModel()
|
|
existing_profile := models.Profile{ID: profile.ID}
|
|
result_existing := repository.db.First(&existing_profile)
|
|
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())
|
|
}
|
|
}
|
|
}
|
|
}
|