diff --git a/modules/core/domain/entities/upload/upload.go b/modules/core/domain/entities/upload/upload.go index d4130029..f18b2f9d 100644 --- a/modules/core/domain/entities/upload/upload.go +++ b/modules/core/domain/entities/upload/upload.go @@ -15,11 +15,15 @@ import ( // ---- Value Objects ---- -type UploadType int +type UploadType string + +func (t UploadType) String() string { + return string(t) +} const ( - UploadTypeImage UploadType = iota - UploadTypeDocument + UploadTypeImage UploadType = "image" + UploadTypeDocument UploadType = "document" ) // ---- Interfaces ---- @@ -53,12 +57,19 @@ func New( size int, mimetype *mimetype.MIME, ) Upload { + var t UploadType + if strings.HasPrefix(mimetype.String(), "image") { + t = UploadTypeImage + } else { + t = UploadTypeDocument + } return &upload{ id: 0, hash: hash, path: path, size: NewSize(size), mimetype: mimetype, + _type: t, createdAt: time.Now(), updatedAt: time.Now(), } @@ -69,6 +80,7 @@ func NewWithID( hash, path string, size int, mimetype *mimetype.MIME, + _type UploadType, createdAt, updatedAt time.Time, ) Upload { return &upload{ @@ -77,6 +89,7 @@ func NewWithID( path: path, size: NewSize(size), mimetype: mimetype, + _type: _type, createdAt: createdAt, updatedAt: updatedAt, } @@ -87,6 +100,7 @@ type upload struct { hash string path string size Size + _type UploadType mimetype *mimetype.MIME createdAt time.Time updatedAt time.Time @@ -97,10 +111,7 @@ func (u *upload) ID() uint { } func (u *upload) Type() UploadType { - if strings.HasPrefix(u.mimetype.String(), "image") { - return UploadTypeImage - } - return UploadTypeDocument + return u._type } func (u *upload) Hash() string { diff --git a/modules/core/domain/entities/upload/upload_dto.go b/modules/core/domain/entities/upload/upload_dto.go index 94a48250..f46ce6cc 100644 --- a/modules/core/domain/entities/upload/upload_dto.go +++ b/modules/core/domain/entities/upload/upload_dto.go @@ -20,7 +20,6 @@ type CreateDTO struct { File io.ReadSeeker `validate:"required"` Name string `validate:"required"` Size int `validate:"required"` - Type string } func (d *CreateDTO) Ok(ctx context.Context) (map[string]string, bool) { diff --git a/modules/core/domain/entities/upload/upload_repository.go b/modules/core/domain/entities/upload/upload_repository.go index e684973f..18c7ecb1 100644 --- a/modules/core/domain/entities/upload/upload_repository.go +++ b/modules/core/domain/entities/upload/upload_repository.go @@ -2,16 +2,19 @@ package upload import ( "context" + + "github.com/gabriel-vasile/mimetype" ) type FindParams struct { - ID uint - Hash string - Limit int - Offset int - SortBy []string - Search string - Type string + ID uint + Hash string + Limit int + Offset int + SortBy []string + Search string + Type UploadType + Mimetype *mimetype.MIME } type Repository interface { diff --git a/modules/core/infrastructure/persistence/core_mappers.go b/modules/core/infrastructure/persistence/core_mappers.go index ffacfa81..4248c81e 100644 --- a/modules/core/infrastructure/persistence/core_mappers.go +++ b/modules/core/infrastructure/persistence/core_mappers.go @@ -144,6 +144,7 @@ func ToDBUpload(upload upload.Upload) *models.Upload { Path: upload.Path(), Hash: upload.Hash(), Size: upload.Size().Bytes(), + Type: upload.Type().String(), Mimetype: upload.Mimetype().String(), CreatedAt: upload.CreatedAt(), UpdatedAt: upload.UpdatedAt(), @@ -161,6 +162,7 @@ func ToDomainUpload(dbUpload *models.Upload) upload.Upload { dbUpload.Path, dbUpload.Size, mime, + upload.UploadType(dbUpload.Type), dbUpload.CreatedAt, dbUpload.UpdatedAt, ) diff --git a/modules/core/infrastructure/persistence/models/models.go b/modules/core/infrastructure/persistence/models/models.go index 45c86849..8f2bdb51 100644 --- a/modules/core/infrastructure/persistence/models/models.go +++ b/modules/core/infrastructure/persistence/models/models.go @@ -11,6 +11,7 @@ type Upload struct { Path string Size int Mimetype string + Type string CreatedAt time.Time UpdatedAt time.Time } @@ -88,7 +89,6 @@ type UploadedImage struct { Height int CreatedAt time.Time UpdatedAt time.Time - Upload Upload } type Session struct { diff --git a/modules/core/infrastructure/persistence/schema/core-schema.sql b/modules/core/infrastructure/persistence/schema/core-schema.sql index 9f7b9fb3..62f58eca 100644 --- a/modules/core/infrastructure/persistence/schema/core-schema.sql +++ b/modules/core/infrastructure/persistence/schema/core-schema.sql @@ -14,10 +14,11 @@ CREATE TABLE companies CREATE TABLE uploads ( id SERIAL PRIMARY KEY, - hash VARCHAR(255) NOT NULL UNIQUE, - path VARCHAR(1024) NOT NULL DEFAULT '', - size INT NOT NULL DEFAULT 0, - mimetype VARCHAR(255) NOT NULL, + hash VARCHAR(255) NOT NULL UNIQUE, -- md5 hash of the file + path VARCHAR(1024) NOT NULL DEFAULT '', -- relative path to the file + size INT NOT NULL DEFAULT 0, -- in bytes + mimetype VARCHAR(255) NOT NULL, -- image/jpeg, application/pdf, etc. + type VARCHAR(255) NOT NULL, -- image, document, etc. created_at TIMESTAMP WITH TIME ZONE DEFAULT current_timestamp, updated_at TIMESTAMP WITH TIME ZONE DEFAULT current_timestamp ); diff --git a/modules/core/infrastructure/persistence/upload_repository.go b/modules/core/infrastructure/persistence/upload_repository.go index e0ffceb4..74649b6e 100644 --- a/modules/core/infrastructure/persistence/upload_repository.go +++ b/modules/core/infrastructure/persistence/upload_repository.go @@ -44,7 +44,11 @@ func (g *GormUploadRepository) GetPaginated( } if params.Type != "" { - where, args = append(where, fmt.Sprintf("mimetype = $%d", len(args)+1)), append(args, params.Type) + where, args = append(where, fmt.Sprintf("type = $%d", len(args)+1)), append(args, params.Type.String()) + } + + if params.Mimetype != nil { + where, args = append(where, fmt.Sprintf("mimetype = $%d", len(args)+1)), append(args, params.Mimetype.String()) } rows, err := pool.Query(ctx, ` diff --git a/modules/core/interfaces/graph/uploads.resolvers.go b/modules/core/interfaces/graph/uploads.resolvers.go index a5c487b1..b751589f 100644 --- a/modules/core/interfaces/graph/uploads.resolvers.go +++ b/modules/core/interfaces/graph/uploads.resolvers.go @@ -10,9 +10,11 @@ import ( "fmt" "github.com/99designs/gqlgen/graphql" + "github.com/gabriel-vasile/mimetype" "github.com/iota-uz/iota-sdk/modules/core/domain/entities/upload" model "github.com/iota-uz/iota-sdk/modules/core/interfaces/graph/gqlmodels" "github.com/iota-uz/iota-sdk/modules/core/interfaces/graph/mappers" + "github.com/iota-uz/iota-sdk/pkg/mapping" ) // UploadFile is the resolver for the uploadFile field. @@ -36,5 +38,17 @@ func (r *mutationResolver) UploadFile(ctx context.Context, file *graphql.Upload) // Uploads is the resolver for the uploads field. func (r *queryResolver) Uploads(ctx context.Context, filter model.UploadFilter) ([]*model.Upload, error) { - panic(fmt.Errorf("not implemented: Uploads - uploads")) + params := &upload.FindParams{} + if filter.Type != nil { + params.Type = *filter.Type + } + if filter.MimeType != nil { + params.Mimetype = mimetype.Lookup(*filter.MimeType) + } + uploads, err := r.uploadService.GetPaginated(ctx, params) + if err != nil { + return nil, fmt.Errorf("failed to find uploads: %w", err) + } + + return mapping.MapViewModels(uploads, mappers.UploadToGraphModel), nil } diff --git a/modules/core/presentation/controllers/graphql_controller.go b/modules/core/presentation/controllers/graphql_controller.go index a679115e..4d89cdbd 100644 --- a/modules/core/presentation/controllers/graphql_controller.go +++ b/modules/core/presentation/controllers/graphql_controller.go @@ -39,6 +39,7 @@ func (g *GraphQLController) Register(r *mux.Router) { router.Use( middleware.Authorize(), middleware.ProvideUser(), + middleware.WithLocalizer(g.app.Bundle()), ) router.Handle("/query", srv) diff --git a/modules/core/services/upload_service.go b/modules/core/services/upload_service.go index 42727aed..c0ed94c2 100644 --- a/modules/core/services/upload_service.go +++ b/modules/core/services/upload_service.go @@ -39,6 +39,10 @@ func (s *UploadService) GetAll(ctx context.Context) ([]upload.Upload, error) { return s.repo.GetAll(ctx) } +func (s *UploadService) GetPaginated(ctx context.Context, params *upload.FindParams) ([]upload.Upload, error) { + return s.repo.GetPaginated(ctx, params) +} + func (s *UploadService) Create(ctx context.Context, data *upload.CreateDTO) (upload.Upload, error) { entity, bytes, err := data.ToEntity() if err != nil {