61 lines
1.8 KiB
Go
61 lines
1.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"kontor-api-gin/internal/models"
|
|
"log/slog"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type MailAccountRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func (repository *MailAccountRepository) Import(dtos models.MailAccountDTOList) {
|
|
slog.Info("MailAccountRepository#Import")
|
|
for index, dto := range dtos {
|
|
slog.Info("importing", "index", index, "profile", dto)
|
|
model := dto.ConvertDTOToModel()
|
|
existing_item := models.MailAccount{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 NewMailAccountRepository(db *gorm.DB) *MailAccountRepository {
|
|
repository := &MailAccountRepository{db: db}
|
|
return repository
|
|
}
|
|
|
|
func (repository *MailAccountRepository) FindAll(context context.Context) ([]models.MailAccount, error) {
|
|
var mailaccounts []models.MailAccount
|
|
|
|
result := repository.db.Find(&mailaccounts)
|
|
slog.Info("MailAccountRepository#FindAll", "count", result.RowsAffected)
|
|
|
|
return mailaccounts, nil
|
|
}
|
|
|
|
func (repository *MailAccountRepository) GetById(context context.Context, id string) (models.MailAccount, error) {
|
|
var mailaccount = models.MailAccount{ID: id}
|
|
slog.Info("MailAccountRepository#GetById", "id", id)
|
|
result := repository.db.First(&mailaccount)
|
|
if result.Error != nil {
|
|
slog.Info("MailAccountRepository#GetById", "error", result.Error.Error())
|
|
}
|
|
return mailaccount, result.Error
|
|
}
|