WIP: add missing handlers, services nd repositories
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"kontor-api-echo/internal/models"
|
||||
"log/slog"
|
||||
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type PermissionRepository struct {
|
||||
db *bun.DB
|
||||
}
|
||||
|
||||
func NewPermissionRepository(db *bun.DB) *PermissionRepository {
|
||||
repository := &PermissionRepository{db: db}
|
||||
return repository
|
||||
}
|
||||
|
||||
func (repository *PermissionRepository) FindAll(context context.Context) ([]models.Permission, error) {
|
||||
|
||||
var permissions []models.Permission
|
||||
|
||||
result := repository.db.Find(&permissions)
|
||||
slog.Info("ComicRepository#List", "count", result.RowsAffected)
|
||||
|
||||
return permissions, nil
|
||||
}
|
||||
|
||||
func (repository *PermissionRepository) Import(permissions models.PermissionDTOList) {
|
||||
slog.Info("PermissionRepository#Import")
|
||||
for index, permission := range permissions {
|
||||
slog.Info("importing", "index", index, "profile", permission)
|
||||
model := permission.ConvertDTOToModel()
|
||||
existing_item := models.Permission{ID: permission.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())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user