feat(backend/repositories): add get tags by file method
refactor(backend/domain): start switching from pgx types
This commit is contained in:
parent
c39d82fafd
commit
00ab98072b
@ -20,9 +20,9 @@ type MIME struct {
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
CategoryCore struct {
|
CategoryCore struct {
|
||||||
ID string `json:"id"`
|
ID *string `json:"id"`
|
||||||
Name string `json:"name"`
|
Name *string `json:"name"`
|
||||||
Color pgtype.Text `json:"color"`
|
Color *string `json:"color"`
|
||||||
}
|
}
|
||||||
CategoryItem struct {
|
CategoryItem struct {
|
||||||
CategoryCore
|
CategoryCore
|
||||||
|
|||||||
@ -13,4 +13,5 @@ type FileRepository interface {
|
|||||||
Add(ctx context.Context, user_id int, name, mime string, datetime time.Time, notes string, metadata json.RawMessage) (file FileCore, domainErr *DomainError)
|
Add(ctx context.Context, user_id int, name, mime string, datetime time.Time, notes string, metadata json.RawMessage) (file FileCore, domainErr *DomainError)
|
||||||
Update(ctx context.Context, user_id int, file_id string, updates map[string]interface{}) (domainErr *DomainError)
|
Update(ctx context.Context, user_id int, file_id string, updates map[string]interface{}) (domainErr *DomainError)
|
||||||
Delete(ctx context.Context, user_id int, file_id string) (domainErr *DomainError)
|
Delete(ctx context.Context, user_id int, file_id string) (domainErr *DomainError)
|
||||||
|
GetTags(ctx context.Context, user_id int, file_id string) (tags []TagItem, domainErr *DomainError)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -270,3 +270,44 @@ func (s *FileRepository) Delete(ctx context.Context, user_id int, file_id string
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *FileRepository) GetTags(ctx context.Context, user_id int, file_id string) (tags []domain.TagItem, domainErr *domain.DomainError) {
|
||||||
|
rows, err := s.db.Query(ctx, `
|
||||||
|
SELECT
|
||||||
|
t.id,
|
||||||
|
t.name,
|
||||||
|
t.color,
|
||||||
|
c.id,
|
||||||
|
c.name,
|
||||||
|
c.color
|
||||||
|
FROM data.tags t
|
||||||
|
LEFT JOIN data.categories c ON c.id=t.category_id
|
||||||
|
JOIN data.file_tag ft ON ft.tag_id=t.id AND ft.file_id=$2
|
||||||
|
JOIN data.files f ON f.id=$2
|
||||||
|
WHERE NOT f.is_deleted AND (f.creator_id=$1 OR (SELECT view FROM acl.files WHERE file_id=$2 AND user_id=$1) OR (SELECT is_admin FROM system.users WHERE id=$1))
|
||||||
|
`, user_id, file_id)
|
||||||
|
if err != nil {
|
||||||
|
var pgErr *pgconn.PgError
|
||||||
|
if errors.As(err, &pgErr) && (pgErr.Code == "22P02" || pgErr.Code == "22007") {
|
||||||
|
domainErr = domain.NewDomainError(err, domain.ErrValidation, "format", pgErr.Message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
domainErr = domain.NewUnexpectedError(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
var tag domain.TagItem
|
||||||
|
err = rows.Scan(&tag.ID, &tag.Name, &tag.Color, &tag.Category.ID, &tag.Category.Name, &tag.Category.Color)
|
||||||
|
if err != nil {
|
||||||
|
domainErr = domain.NewUnexpectedError(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tags = append(tags, tag)
|
||||||
|
}
|
||||||
|
err = rows.Err()
|
||||||
|
if err != nil {
|
||||||
|
domainErr = domain.NewUnexpectedError(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user