70 lines
2.0 KiB
Go
70 lines
2.0 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"kontor-api-echo/internal/models"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"github.com/labstack/echo/v5"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
type ProfileRepository struct {
|
|
db *bun.DB
|
|
}
|
|
|
|
func NewProfileRepository(db *bun.DB) *ProfileRepository {
|
|
repository := &ProfileRepository{db: db}
|
|
return repository
|
|
}
|
|
|
|
func (repository *ProfileRepository) FindAll(context context.Context) ([]models.Profile, error) {
|
|
var profiles []models.Profile
|
|
err := repository.db.NewSelect().Model(&profiles).Scan(context)
|
|
if err != nil {
|
|
slog.Info(err.Error())
|
|
return nil, echo.ErrInternalServerError
|
|
}
|
|
slog.Info("MediaArticleRepository#FindAll", "count", len(profiles))
|
|
|
|
return profiles, nil
|
|
}
|
|
|
|
func (repository *ProfileRepository) FindProfileByEmail(context context.Context, email string) (*models.Profile, error) {
|
|
var profile models.Profile
|
|
slog.Info("MediaArticleRepository#GetById", "email", email)
|
|
|
|
err := repository.db.NewSelect().Model(&profile).Where("email = ?", email).Scan(context)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &profile, nil
|
|
}
|
|
|
|
func (repository *ProfileRepository) Import(dtos models.ProfileDTOList) {
|
|
slog.Info("ProfileRepository#Import")
|
|
for index, dto := range dtos {
|
|
slog.Info("importing", "index", index, "profile", dto)
|
|
model := dto.ConvertDTOToModel()
|
|
existing_item := models.Profile{ID: dto.ID}
|
|
err := repository.db.NewSelect().Model(&existing_item).Where("id = ?", dto.ID).Scan(context.Background())
|
|
if err != nil {
|
|
slog.Info("check existing id: not found - inserting")
|
|
_, err := repository.db.NewInsert().Model(&model).Exec(context.Background())
|
|
if err != nil {
|
|
slog.Info("error during import", "error", err.Error())
|
|
}
|
|
} else {
|
|
slog.Info("check existing id: found - updating")
|
|
_, err := repository.db.NewUpdate().Model(&model).
|
|
Set("updated_at = ? ", time.Now).
|
|
Where("id = ?", model.ID).
|
|
Exec(context.Background())
|
|
if err != nil {
|
|
slog.Info("error during updating", "error", err.Error())
|
|
}
|
|
}
|
|
}
|
|
}
|