72 lines
1.7 KiB
Go
72 lines
1.7 KiB
Go
package turso
|
|
|
|
import (
|
|
"database/sql"
|
|
"io"
|
|
"log"
|
|
"time"
|
|
|
|
"gitea.trankilou.fr/fabien/lasebuche"
|
|
"trankilou.fr/lassistanoque/backend/internal/domain"
|
|
"trankilou.fr/lassistanoque/backend/internal/utility"
|
|
)
|
|
|
|
type TursoFileRepository struct {
|
|
db *sql.DB
|
|
FileTable lasebuche.Table[domain.File]
|
|
}
|
|
|
|
func NewTursoFileRepository(db *sql.DB) *TursoFileRepository {
|
|
dialect := lasebuche.NewSqliteDialect()
|
|
fileTable, err := lasebuche.NewTable[domain.File](db, dialect, "settings")
|
|
if err != nil {
|
|
log.Fatalf("error creating lasebuche team table")
|
|
}
|
|
return &TursoFileRepository{
|
|
db: db,
|
|
FileTable: fileTable,
|
|
}
|
|
}
|
|
|
|
func (fr *TursoFileRepository) FindByID(id string) (*domain.File, error) {
|
|
return fr.FileTable.Get(id)
|
|
}
|
|
|
|
func (fr *TursoFileRepository) Create(file *domain.File) (*domain.File, error) {
|
|
return fr.FileTable.Insert(file)
|
|
}
|
|
|
|
func (fr *TursoFileRepository) Update(file *domain.File) (*domain.File, error) {
|
|
return fr.FileTable.Update(file)
|
|
}
|
|
|
|
func (fr *TursoFileRepository) Delete(id string) error {
|
|
return fr.FileTable.Delete(id)
|
|
}
|
|
|
|
func (fr *TursoFileRepository) Upload(reader io.Reader, path string, name string, contentType string, replace bool) (*domain.File, error) {
|
|
|
|
bytes, err := io.ReadAll(reader)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
file := &domain.File{
|
|
ID: utility.GenID(),
|
|
Name: name,
|
|
StoragePath: path,
|
|
StorageFilename: name,
|
|
ContentType: contentType,
|
|
VersionId: utility.GenID(),
|
|
DateCreated: time.Now(),
|
|
DateUpdated: time.Now(),
|
|
Content: bytes,
|
|
}
|
|
|
|
return fr.FileTable.Insert(file)
|
|
}
|
|
|
|
func (fr *TursoFileRepository) Download(id string) (*domain.File, error) {
|
|
return fr.FileTable.Get(id)
|
|
}
|