option to set entry for binary file
This commit is contained in:
parent
9921b1f91e
commit
088d41e8a2
|
@ -14,7 +14,7 @@ func NewBinaryFileService(repo repository.BinaryRepository) *BinaryService {
|
|||
}
|
||||
|
||||
func (s *BinaryService) Create(name string, file []byte) (*model.BinaryFile, error) {
|
||||
return s.repo.Create(name, file)
|
||||
return s.repo.Create(name, file, nil)
|
||||
}
|
||||
|
||||
func (s *BinaryService) FindById(id string) (*model.BinaryFile, error) {
|
||||
|
|
|
@ -17,8 +17,9 @@ type BinaryRepository interface {
|
|||
// Create creates a new binary file
|
||||
// The name is the original file name, and is not unique
|
||||
// BinaryFile.Id is a unique identifier
|
||||
Create(name string, data []byte) (*model.BinaryFile, error)
|
||||
Create(name string, data []byte, entry model.Entry) (*model.BinaryFile, error)
|
||||
FindById(id string) (*model.BinaryFile, error)
|
||||
FindByNameForEntry(name string, entry model.Entry) (*model.BinaryFile, error)
|
||||
}
|
||||
|
||||
type AuthorRepository interface {
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(importCmd)
|
||||
|
||||
importCmd.Flags().StringVarP(&user, "path", "p", "", "Path to the user folder")
|
||||
importCmd.MarkFlagRequired("path")
|
||||
}
|
||||
|
||||
var importCmd = &cobra.Command{
|
||||
Use: "import",
|
||||
Short: "Import data from v1",
|
||||
Long: `Import data from v1`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// db := infra.NewSqliteDB(DbPath)
|
||||
// App(db).ImportV1()
|
||||
|
||||
// TODO: Implement this
|
||||
// For each folder in the user folder
|
||||
// Map to entry types
|
||||
// Convert and save
|
||||
// Import Binary files
|
||||
},
|
||||
}
|
|
@ -1,17 +1,18 @@
|
|||
package infra
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"owl-blogs/app/repository"
|
||||
"owl-blogs/domain/model"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type sqlBinaryFile struct {
|
||||
Id string `db:"id"`
|
||||
Name string `db:"name"`
|
||||
EntryId *string `db:"entry_id"`
|
||||
Data []byte `db:"data"`
|
||||
}
|
||||
|
||||
|
@ -29,6 +30,7 @@ func NewBinaryFileRepo(db Database) repository.BinaryRepository {
|
|||
CREATE TABLE IF NOT EXISTS binary_files (
|
||||
id VARCHAR(255) PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
entry_id VARCHAR(255),
|
||||
data BLOB NOT NULL
|
||||
);
|
||||
`)
|
||||
|
@ -37,15 +39,41 @@ func NewBinaryFileRepo(db Database) repository.BinaryRepository {
|
|||
}
|
||||
|
||||
// Create implements repository.BinaryRepository
|
||||
func (repo *DefaultBinaryFileRepo) Create(name string, data []byte) (*model.BinaryFile, error) {
|
||||
id := uuid.New().String()
|
||||
func (repo *DefaultBinaryFileRepo) Create(name string, data []byte, entry model.Entry) (*model.BinaryFile, error) {
|
||||
parts := strings.Split(name, ".")
|
||||
if len(parts) > 1 {
|
||||
ext := parts[len(parts)-1]
|
||||
id = id + "." + ext
|
||||
fileName := strings.Join(parts[:len(parts)-1], ".")
|
||||
fileExt := parts[len(parts)-1]
|
||||
id := fileName + "." + fileExt
|
||||
|
||||
// check if id exists
|
||||
var count int
|
||||
err := repo.db.Get(&count, "SELECT COUNT(*) FROM binary_files WHERE id = ?", id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err := repo.db.Exec("INSERT INTO binary_files (id, name, data) VALUES (?, ?, ?)", id, name, data)
|
||||
if count > 0 {
|
||||
counter := 1
|
||||
for {
|
||||
id = fmt.Sprintf("%s-%d.%s", fileName, counter, fileExt)
|
||||
err := repo.db.Get(&count, "SELECT COUNT(*) FROM binary_files WHERE id = ?", id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if count == 0 {
|
||||
break
|
||||
}
|
||||
counter++
|
||||
}
|
||||
}
|
||||
|
||||
var entryId *string
|
||||
if entry != nil {
|
||||
eId := entry.ID()
|
||||
entryId = &eId
|
||||
}
|
||||
|
||||
_, err = repo.db.Exec("INSERT INTO binary_files (id, name, entry_id, data) VALUES (?, ?, ?, ?)", id, name, entryId, data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -61,3 +89,13 @@ func (repo *DefaultBinaryFileRepo) FindById(id string) (*model.BinaryFile, error
|
|||
}
|
||||
return &model.BinaryFile{Id: sqlFile.Id, Name: sqlFile.Name, Data: sqlFile.Data}, nil
|
||||
}
|
||||
|
||||
// FindByNameForEntry implements repository.BinaryRepository
|
||||
func (repo *DefaultBinaryFileRepo) FindByNameForEntry(name string, entry model.Entry) (*model.BinaryFile, error) {
|
||||
var sqlFile sqlBinaryFile
|
||||
err := repo.db.Get(&sqlFile, "SELECT * FROM binary_files WHERE name = ? AND entry_id = ?", name, entry.ID())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &model.BinaryFile{Id: sqlFile.Id, Name: sqlFile.Name, Data: sqlFile.Data}, nil
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ func setupBinaryRepo() repository.BinaryRepository {
|
|||
func TestBinaryRepoCreate(t *testing.T) {
|
||||
repo := setupBinaryRepo()
|
||||
|
||||
file, err := repo.Create("name", []byte("😀 😃 😄 😁"))
|
||||
file, err := repo.Create("name", []byte("😀 😃 😄 😁"), nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
file, err = repo.FindById(file.Id)
|
||||
|
@ -30,10 +30,10 @@ func TestBinaryRepoCreate(t *testing.T) {
|
|||
func TestBinaryRepoNoSideEffect(t *testing.T) {
|
||||
repo := setupBinaryRepo()
|
||||
|
||||
file, err := repo.Create("name1", []byte("111"))
|
||||
file, err := repo.Create("name1", []byte("111"), nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
file2, err := repo.Create("name2", []byte("222"))
|
||||
file2, err := repo.Create("name2", []byte("222"), nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
file, err = repo.FindById(file.Id)
|
||||
|
|
Loading…
Reference in New Issue