WIP: add missing handlers, services amd repositories
This commit is contained in:
@@ -2,59 +2,67 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"kontor-api-gin/internal/models"
|
||||
"kontor-api-echo/internal/models"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"github.com/labstack/echo/v5"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type ProfileRepository struct {
|
||||
db *gorm.DB
|
||||
db *bun.DB
|
||||
}
|
||||
|
||||
func NewProfileRepository(db *gorm.DB) *ProfileRepository {
|
||||
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
|
||||
|
||||
result := repository.db.Find(&profiles)
|
||||
slog.Info("ProfileRepository#FindAll", "count", result.RowsAffected)
|
||||
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(email string) (*models.Profile, error) {
|
||||
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.Model(models.Profile{}).Where("email = ?", email).Take(&profile).Error
|
||||
|
||||
err := repository.db.NewSelect().Model(&profile).Where("email = ?", email).Scan(context)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &profile, nil
|
||||
}
|
||||
|
||||
func (repository *ProfileRepository) Import(profiles models.ProfileDTOList) {
|
||||
func (repository *ProfileRepository) Import(dtos 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 {
|
||||
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")
|
||||
result := repository.db.Create(&model)
|
||||
if result.Error != nil {
|
||||
slog.Info("error during import", "error", result.Error.Error())
|
||||
_, 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")
|
||||
result := repository.db.Save(&model)
|
||||
if result.Error != nil {
|
||||
slog.Info("error during updating", "error", result.Error.Error())
|
||||
_, 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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user