Refactoring: clean architecture project structure
This commit is contained in:
77
internal/repo/sqlite/transcript_job_sqlite.go
Normal file
77
internal/repo/sqlite/transcript_job_sqlite.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"git.vakhrushev.me/av/transcriber/internal/entity"
|
||||
"github.com/doug-martin/goqu/v9"
|
||||
)
|
||||
|
||||
type TranscriptJobRepository struct {
|
||||
db *sql.DB
|
||||
gq *goqu.Database
|
||||
}
|
||||
|
||||
func NewTranscriptJobRepository(db *sql.DB, gq *goqu.Database) *TranscriptJobRepository {
|
||||
return &TranscriptJobRepository{db, gq}
|
||||
}
|
||||
|
||||
func (repo *TranscriptJobRepository) Create(job *entity.TranscribeJob) error {
|
||||
record := goqu.Record{
|
||||
"id": job.Id,
|
||||
"state": job.State,
|
||||
"file_id": job.FileID,
|
||||
"is_error": job.IsError,
|
||||
"error_text": job.ErrorText,
|
||||
"worker": job.Worker,
|
||||
"acquired_at": job.AcquiredAt,
|
||||
"created_at": job.CreatedAt,
|
||||
}
|
||||
query := repo.gq.Insert("transcribe_jobs").Rows(record)
|
||||
sql, args, err := query.ToSQL()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to build query: %w", err)
|
||||
}
|
||||
|
||||
_, err = repo.db.Exec(sql, args...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to insert transcribe job: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repo *TranscriptJobRepository) GetByID(id string) (*entity.TranscribeJob, error) {
|
||||
query := repo.gq.From("transcribe_jobs").Select(
|
||||
"id",
|
||||
"state",
|
||||
"file_id",
|
||||
"is_error",
|
||||
"error_text",
|
||||
"worker",
|
||||
"acquired_at",
|
||||
"created_at",
|
||||
).Where(goqu.C("id").Eq(id))
|
||||
sql, args, err := query.ToSQL()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to build query: %w", err)
|
||||
}
|
||||
|
||||
var job entity.TranscribeJob
|
||||
err = repo.db.QueryRow(sql, args...).Scan(
|
||||
&job.Id,
|
||||
&job.State,
|
||||
&job.FileID,
|
||||
&job.IsError,
|
||||
&job.ErrorText,
|
||||
&job.Worker,
|
||||
&job.AcquiredAt,
|
||||
&job.CreatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get transcribe job: %w", err)
|
||||
}
|
||||
|
||||
return &job, nil
|
||||
}
|
Reference in New Issue
Block a user