2023-07-08 09:08:55 +00:00
|
|
|
package infra
|
|
|
|
|
|
|
|
import (
|
2023-07-09 19:27:59 +00:00
|
|
|
"fmt"
|
2023-07-08 10:03:10 +00:00
|
|
|
"owl-blogs/app/repository"
|
2023-07-08 09:08:55 +00:00
|
|
|
"owl-blogs/domain/model"
|
2023-07-09 17:09:54 +00:00
|
|
|
"strings"
|
2023-07-08 09:08:55 +00:00
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
)
|
|
|
|
|
|
|
|
type sqlBinaryFile struct {
|
2023-07-09 19:27:59 +00:00
|
|
|
Id string `db:"id"`
|
|
|
|
Name string `db:"name"`
|
|
|
|
EntryId *string `db:"entry_id"`
|
|
|
|
Data []byte `db:"data"`
|
2023-07-08 09:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultBinaryFileRepo struct {
|
|
|
|
db *sqlx.DB
|
|
|
|
}
|
|
|
|
|
2023-07-09 17:09:54 +00:00
|
|
|
// NewBinaryFileRepo creates a new binary file repository
|
|
|
|
// It creates the table if not exists
|
2023-07-08 10:03:10 +00:00
|
|
|
func NewBinaryFileRepo(db Database) repository.BinaryRepository {
|
2023-07-08 09:08:55 +00:00
|
|
|
sqlxdb := db.Get()
|
|
|
|
|
|
|
|
// Create table if not exists
|
|
|
|
sqlxdb.MustExec(`
|
|
|
|
CREATE TABLE IF NOT EXISTS binary_files (
|
|
|
|
id VARCHAR(255) PRIMARY KEY,
|
|
|
|
name VARCHAR(255) NOT NULL,
|
2023-07-09 19:27:59 +00:00
|
|
|
entry_id VARCHAR(255),
|
2023-07-08 09:08:55 +00:00
|
|
|
data BLOB NOT NULL
|
|
|
|
);
|
|
|
|
`)
|
|
|
|
|
|
|
|
return &DefaultBinaryFileRepo{db: sqlxdb}
|
|
|
|
}
|
|
|
|
|
2023-07-09 17:09:54 +00:00
|
|
|
// Create implements repository.BinaryRepository
|
2023-07-09 19:27:59 +00:00
|
|
|
func (repo *DefaultBinaryFileRepo) Create(name string, data []byte, entry model.Entry) (*model.BinaryFile, error) {
|
2023-07-09 17:09:54 +00:00
|
|
|
parts := strings.Split(name, ".")
|
2023-07-09 19:27:59 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2023-07-09 17:09:54 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 19:27:59 +00:00
|
|
|
_, err = repo.db.Exec("INSERT INTO binary_files (id, name, entry_id, data) VALUES (?, ?, ?, ?)", id, name, entryId, data)
|
2023-07-08 09:08:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &model.BinaryFile{Id: id, Name: name, Data: data}, nil
|
|
|
|
}
|
|
|
|
|
2023-07-09 17:09:54 +00:00
|
|
|
// FindById implements repository.BinaryRepository
|
2023-07-08 09:08:55 +00:00
|
|
|
func (repo *DefaultBinaryFileRepo) FindById(id string) (*model.BinaryFile, error) {
|
|
|
|
var sqlFile sqlBinaryFile
|
|
|
|
err := repo.db.Get(&sqlFile, "SELECT * FROM binary_files WHERE id = ?", id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &model.BinaryFile{Id: sqlFile.Id, Name: sqlFile.Name, Data: sqlFile.Data}, nil
|
|
|
|
}
|
2023-07-09 19:27:59 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2023-07-27 20:02:13 +00:00
|
|
|
|
|
|
|
// ListIds implements repository.BinaryRepository
|
|
|
|
func (repo *DefaultBinaryFileRepo) ListIds() ([]string, error) {
|
|
|
|
var ids []string
|
|
|
|
err := repo.db.Select(&ids, "SELECT id FROM binary_files")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ids, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete implements repository.BinaryRepository
|
|
|
|
func (repo *DefaultBinaryFileRepo) Delete(binary *model.BinaryFile) error {
|
|
|
|
id := binary.Id
|
|
|
|
println("Deleting binary file", id)
|
|
|
|
_, err := repo.db.Exec("DELETE FROM binary_files WHERE id = ?", id)
|
|
|
|
return err
|
|
|
|
}
|