Files
kontor/kontor-echo/internal/repository/assignment.go
T
tpeetz 79653d2c43
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 5s
Gitea Actions Demo / build-and-test (push) Failing after 12s
WIP: add missing handlers, services nd repositories
2026-11-06 21:20:24 +01:00

61 lines
1.8 KiB
Go

package repository
import (
"context"
"kontor-api-echo/internal/models"
"log/slog"
"github.com/uptrace/bun"
)
type AssignmentRepository struct {
db *bun.DB
}
func NewAssignmentRepository(db *bun.DB) *AssignmentRepository {
repository := &AssignmentRepository{db: db}
return repository
}
func (repository *AssignmentRepository) FindAll(context context.Context) ([]models.Assignment, error) {
var assignments []models.Assignment
result := repository.db.Find(&assignments)
slog.Info("AssignmentRepository#FindAll", "count", result.RowsAffected)
return assignments, nil
}
func (repository *AssignmentRepository) GetById(context context.Context, id string) (models.Assignment, error) {
var assignment = models.Assignment{ID: id}
slog.Info("AssignmentRepository#GetById", "id", id)
result := repository.db.First(&assignment)
if result.Error != nil {
slog.Info("AssignmentRepository#GetById", "error", result.Error.Error())
}
return assignment, result.Error
}
func (repository *AssignmentRepository) Import(assignments models.AssignmentDTOList) {
slog.Info("AssignmentRepository#Import")
for index, assignment := range assignments {
slog.Info("importing", "index", index, "profile", assignment)
model := assignment.ConvertDTOToModel()
existing_item := models.Assignment{ID: assignment.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())
}
}
}
}