Skip to content

Commit

Permalink
feat: graphql resolver for uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
diyor28 committed Feb 25, 2025
1 parent 9645cd9 commit a6f595f
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 22 deletions.
25 changes: 18 additions & 7 deletions modules/core/domain/entities/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----
Expand Down Expand Up @@ -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(),
}
Expand All @@ -69,6 +80,7 @@ func NewWithID(
hash, path string,
size int,
mimetype *mimetype.MIME,
_type UploadType,
createdAt, updatedAt time.Time,
) Upload {
return &upload{
Expand All @@ -77,6 +89,7 @@ func NewWithID(
path: path,
size: NewSize(size),
mimetype: mimetype,
_type: _type,
createdAt: createdAt,
updatedAt: updatedAt,
}
Expand All @@ -87,6 +100,7 @@ type upload struct {
hash string
path string
size Size
_type UploadType
mimetype *mimetype.MIME
createdAt time.Time
updatedAt time.Time
Expand All @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion modules/core/domain/entities/upload/upload_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 10 additions & 7 deletions modules/core/domain/entities/upload/upload_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions modules/core/infrastructure/persistence/core_mappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -161,6 +162,7 @@ func ToDomainUpload(dbUpload *models.Upload) upload.Upload {
dbUpload.Path,
dbUpload.Size,
mime,
upload.UploadType(dbUpload.Type),
dbUpload.CreatedAt,
dbUpload.UpdatedAt,
)
Expand Down
2 changes: 1 addition & 1 deletion modules/core/infrastructure/persistence/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Upload struct {
Path string
Size int
Mimetype string
Type string
CreatedAt time.Time
UpdatedAt time.Time
}
Expand Down Expand Up @@ -88,7 +89,6 @@ type UploadedImage struct {
Height int
CreatedAt time.Time
UpdatedAt time.Time
Upload Upload
}

type Session struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
6 changes: 5 additions & 1 deletion modules/core/infrastructure/persistence/upload_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, `
Expand Down
16 changes: 15 additions & 1 deletion modules/core/interfaces/graph/uploads.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions modules/core/services/upload_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a6f595f

Please sign in to comment.