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