question - add a new type 'repository' which includes all helpers, remove helpers
This commit is contained in:
parent
2ee047a801
commit
7ae94f16a0
|
@ -17,7 +17,7 @@ import (
|
|||
|
||||
type pgRepository struct {
|
||||
*pg.DB
|
||||
fileStorage filestorage.FileStorage
|
||||
*repository
|
||||
}
|
||||
|
||||
type PGRepositoryConfig struct {
|
||||
|
@ -31,7 +31,9 @@ func NewPGRepository(cfg *PGRepositoryConfig) (question.Repository, error) {
|
|||
}
|
||||
return &pgRepository{
|
||||
cfg.DB,
|
||||
cfg.FileStorage,
|
||||
&repository{
|
||||
fileStorage: cfg.FileStorage,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -48,7 +50,7 @@ func (repo *pgRepository) Store(ctx context.Context, input *models.QuestionInput
|
|||
return nil, errorutils.Wrap(err, messageFailedToSaveModel)
|
||||
}
|
||||
|
||||
saveImages(repo.fileStorage, item, input)
|
||||
repo.saveImages(item, input)
|
||||
|
||||
return item, nil
|
||||
}
|
||||
|
@ -72,9 +74,7 @@ func (repo *pgRepository) UpdateOneByID(ctx context.Context, id int, input *mode
|
|||
return nil, errorutils.Wrap(err, messageFailedToSaveModel)
|
||||
}
|
||||
|
||||
saveImages(repo.fileStorage, item, input)
|
||||
deleteImagesBasedOnInput(repo.fileStorage, item, input)
|
||||
|
||||
repo.saveImages(item, input)
|
||||
if _, err := baseQuery.
|
||||
Clone().
|
||||
Set("image = ?", item.Image).
|
||||
|
@ -83,9 +83,9 @@ func (repo *pgRepository) UpdateOneByID(ctx context.Context, id int, input *mode
|
|||
Set("answer_c_image = ?", item.AnswerCImage).
|
||||
Set("answer_d_image = ?", item.AnswerDImage).
|
||||
Update(); err != nil && err != pg.ErrNoRows {
|
||||
|
||||
return nil, errorutils.Wrap(err, messageFailedToSaveModel)
|
||||
}
|
||||
repo.deleteImagesBasedOnInput(item, input)
|
||||
|
||||
return item, nil
|
||||
}
|
||||
|
@ -101,7 +101,7 @@ func (repo *pgRepository) Delete(ctx context.Context, f *models.QuestionFilter)
|
|||
return nil, errorutils.Wrap(err, messageFailedToDeleteModel)
|
||||
}
|
||||
|
||||
go getAllFilenamesAndDeleteImages(repo.fileStorage, items)
|
||||
go repo.getAllFilenamesAndDeleteImages(items)
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
|
|
@ -9,7 +9,11 @@ import (
|
|||
"github.com/zdam-egzamin-zawodowy/backend/pkg/utils"
|
||||
)
|
||||
|
||||
func saveImages(storage filestorage.FileStorage, destination *models.Question, input *models.QuestionInput) {
|
||||
type repository struct {
|
||||
fileStorage filestorage.FileStorage
|
||||
}
|
||||
|
||||
func (repo *repository) saveImages(destination *models.Question, input *models.QuestionInput) {
|
||||
images := [...]*graphql.Upload{
|
||||
input.Image,
|
||||
input.AnswerAImage,
|
||||
|
@ -32,7 +36,7 @@ func saveImages(storage filestorage.FileStorage, destination *models.Question, i
|
|||
generated = true
|
||||
filenames[index] = utils.GenerateFilename(filepath.Ext(file.Filename))
|
||||
}
|
||||
err := storage.Put(file.File, filenames[index])
|
||||
err := repo.fileStorage.Put(file.File, filenames[index])
|
||||
if err != nil && generated {
|
||||
filenames[index] = ""
|
||||
}
|
||||
|
@ -46,13 +50,13 @@ func saveImages(storage filestorage.FileStorage, destination *models.Question, i
|
|||
destination.AnswerDImage = filenames[4]
|
||||
}
|
||||
|
||||
func deleteImages(storage filestorage.FileStorage, filenames []string) {
|
||||
func (repo *repository) deleteImages(filenames []string) {
|
||||
for _, filename := range filenames {
|
||||
storage.Remove(filename)
|
||||
repo.fileStorage.Remove(filename)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteImagesBasedOnInput(storage filestorage.FileStorage, question *models.Question, input *models.QuestionInput) {
|
||||
func (repo *repository) deleteImagesBasedOnInput(question *models.Question, input *models.QuestionInput) {
|
||||
filenames := []string{}
|
||||
|
||||
if input.DeleteImage != nil &&
|
||||
|
@ -95,10 +99,10 @@ func deleteImagesBasedOnInput(storage filestorage.FileStorage, question *models.
|
|||
question.AnswerDImage = ""
|
||||
}
|
||||
|
||||
deleteImages(storage, filenames)
|
||||
repo.deleteImages(filenames)
|
||||
}
|
||||
|
||||
func getAllFilenamesAndDeleteImages(storage filestorage.FileStorage, questions []*models.Question) {
|
||||
func (repo *repository) getAllFilenamesAndDeleteImages(questions []*models.Question) {
|
||||
filenames := []string{}
|
||||
|
||||
for _, question := range questions {
|
||||
|
@ -119,5 +123,5 @@ func getAllFilenamesAndDeleteImages(storage filestorage.FileStorage, questions [
|
|||
}
|
||||
}
|
||||
|
||||
deleteImages(storage, filenames)
|
||||
repo.deleteImages(filenames)
|
||||
}
|
Reference in New Issue