WIP: add missing handlers, services nd repositories
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
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())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,11 +66,10 @@ func (repository *MediaActorRepository) Add(context context.Context, url string)
|
||||
}
|
||||
|
||||
func (repository *MediaActorRepository) Delete(context context.Context, id string) error {
|
||||
var actor = models.MediaActor{ID: id}
|
||||
slog.Info("MediaActorRepository#Delete", "id", id)
|
||||
result := repository.db.Delete(&actor)
|
||||
if result.Error != nil {
|
||||
slog.Info("SportRepository#Delete", "error", result.Error.Error())
|
||||
_, err := repository.db.NewDelete().Model((*models.MediaActor)(nil)).Where("id = ?", id).Exec(context)
|
||||
if err != nil {
|
||||
slog.Info("MediaArticleRepository#Delete", "error", err.Error())
|
||||
}
|
||||
return result.Error
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -67,11 +67,10 @@ func (repository *MediaActorFileRepository) Add(context context.Context, media_a
|
||||
}
|
||||
|
||||
func (repository *MediaActorFileRepository) Delete(context context.Context, id string) error {
|
||||
var file = models.MediaActorFile{ID: id}
|
||||
slog.Info("MediaActorFileRepository#Delete", "id", id)
|
||||
result := repository.db.Delete(&file)
|
||||
if result.Error != nil {
|
||||
slog.Info("MediaActorFileRepository#Delete", "error", result.Error.Error())
|
||||
_, err := repository.db.NewDelete().Model((*models.MediaActorFile)(nil)).Where("id = ?", id).Exec(context)
|
||||
if err != nil {
|
||||
slog.Info("MediaArticleRepository#Delete", "error", err.Error())
|
||||
}
|
||||
return result.Error
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -67,11 +67,10 @@ func (repository *MediaFileRepository) Add(context context.Context, url string)
|
||||
}
|
||||
|
||||
func (repository *MediaFileRepository) Delete(context context.Context, id string) error {
|
||||
var file = models.MediaFile{ID: id}
|
||||
slog.Info("MediaFileRepository#Delete", "id", id)
|
||||
result := repository.db.Delete(&file)
|
||||
if result.Error != nil {
|
||||
slog.Info("MediaFileRepository#Delete", "error", result.Error.Error())
|
||||
_, err := repository.db.NewDelete().Model((*models.MediaFile)(nil)).Where("id = ?", id).Exec(context)
|
||||
if err != nil {
|
||||
slog.Info("MediaFileRepository#Delete", "error", err.Error())
|
||||
}
|
||||
return result.Error
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
@@ -49,8 +50,12 @@ func NewMediaLoFiRepository(db *bun.DB) *MediaLoFiRepository {
|
||||
func (repository *MediaLoFiRepository) FindAll(context context.Context) ([]models.MediaLoFi, error) {
|
||||
var lofis []models.MediaLoFi
|
||||
|
||||
result := repository.db.Find(&lofis)
|
||||
slog.Info("MediaLoFiRepository#FindAll", "count", result.RowsAffected)
|
||||
err := repository.db.NewSelect().Model(&lofis).Scan(context)
|
||||
if err != nil {
|
||||
slog.Info(err.Error())
|
||||
return nil, echo.ErrInternalServerError
|
||||
}
|
||||
slog.Info("MediaLoFiRepository#FindAll", "count", len(lofis))
|
||||
|
||||
return lofis, nil
|
||||
}
|
||||
@@ -58,11 +63,12 @@ func (repository *MediaLoFiRepository) FindAll(context context.Context) ([]model
|
||||
func (repository *MediaLoFiRepository) GetById(context context.Context, id string) (*models.MediaLoFi, error) {
|
||||
var lofi = models.MediaLoFi{ID: id}
|
||||
slog.Info("MediaLoFiRepository#GetById", "id", id)
|
||||
result := repository.db.First(&lofi)
|
||||
if result.Error != nil {
|
||||
slog.Info("MediaLoFiRepository#GetById", "error", result.Error.Error())
|
||||
err := repository.db.NewSelect().Model(&lofi).Where("id = ?", id).Scan(context)
|
||||
if err != nil {
|
||||
slog.Info("MediaLoFiRepository#GetById", "error", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
return &lofi, result.Error
|
||||
return &lofi, err
|
||||
}
|
||||
|
||||
func (repository *MediaLoFiRepository) Add(context context.Context, url string) (*models.MediaLoFi, error) {
|
||||
@@ -117,11 +123,10 @@ func (repository *MediaLoFiRepository) Update(context context.Context, update mo
|
||||
}
|
||||
|
||||
func (repository *MediaLoFiRepository) Delete(context context.Context, id string) error {
|
||||
var actor = models.MediaLoFi{ID: id}
|
||||
slog.Info("MediaLoFiRepository#Delete", "id", id)
|
||||
result := repository.db.Delete(&actor)
|
||||
if result.Error != nil {
|
||||
slog.Info("MediaLoFiRepository#Delete", "error", result.Error.Error())
|
||||
_, err := repository.db.NewDelete().Model((*models.MediaLoFi)(nil)).Where("id = ?", id).Exec(context)
|
||||
if err != nil {
|
||||
slog.Info("MediaLoFiRepository#Delete", "error", err.Error())
|
||||
}
|
||||
return result.Error
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -117,11 +117,10 @@ func (repository *MediaMusicRepository) Update(context context.Context, update m
|
||||
}
|
||||
|
||||
func (repository *MediaMusicRepository) Delete(context context.Context, id string) error {
|
||||
var actor = models.MediaMusic{ID: id}
|
||||
slog.Info("MediaMusicRepository#Delete", "id", id)
|
||||
result := repository.db.Delete(&actor)
|
||||
if result.Error != nil {
|
||||
slog.Info("MediaMusicRepository#Delete", "error", result.Error.Error())
|
||||
_, err := repository.db.NewDelete().Model((*models.MediaMusic)(nil)).Where("id = ?", id).Exec(context)
|
||||
if err != nil {
|
||||
slog.Info("MediaMusicRepository#Delete", "error", err.Error())
|
||||
}
|
||||
return result.Error
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"kontor-api-gin/internal/models"
|
||||
"log/slog"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ProfileRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewProfileRepository(db *gorm.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)
|
||||
|
||||
return profiles, nil
|
||||
}
|
||||
|
||||
func (repository *ProfileRepository) FindProfileByEmail(email string) (*models.Profile, error) {
|
||||
var profile models.Profile
|
||||
|
||||
err := repository.db.Model(models.Profile{}).Where("email = ?", email).Take(&profile).Error
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &profile, nil
|
||||
}
|
||||
|
||||
func (repository *ProfileRepository) Import(profiles 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 {
|
||||
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