Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove gorm #12

Merged
merged 4 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 55 additions & 9 deletions application/repositories/job_repository.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package repositories

import (
"fmt"
"github.com/jinzhu/gorm"
"database/sql"
"github.com/olukkas/go-encoder/domain"
)

Expand All @@ -13,37 +12,84 @@ type JobRepository interface {
}

type JobRepositoryDb struct {
DB *gorm.DB
DB *sql.DB
}

func NewJobRepositoryDb(DB *gorm.DB) *JobRepositoryDb {
func NewJobRepositoryDb(DB *sql.DB) *JobRepositoryDb {
return &JobRepositoryDb{DB: DB}
}

//goland:noinspection SqlResolve,SqlNoDataSourceInspection
func (j *JobRepositoryDb) Insert(job *domain.Job) (*domain.Job, error) {
if err := j.DB.Create(job).Error; err != nil {
query := `
insert into jobs
(id, output_bucket_path, status, video_id, error, created_at, updated_at)
values
($1, $2, $3, $4, $5, $6, $7)
`
stmt, err := j.DB.Prepare(query)
if err != nil {
return nil, err
}
defer stmt.Close()

_, err = stmt.Exec(job.ID, job.OutputBucketPath, job.Status, job.VideoId, job.Error, job.CreatedAt, job.UpdatedAt)
if err != nil {
return nil, err
}

return job, nil
}

//goland:noinspection SqlResolve,SqlNoDataSourceInspection
func (j *JobRepositoryDb) Find(id string) (*domain.Job, error) {
job := new(domain.Job)
j.DB.Preload("Video").First(job, "id = ?", id)

if job.ID == "" {
return nil, fmt.Errorf("job with id %s does not exists \n", id)
query := `select id, output_bucket_path, status, video_id, error, created_at, updated_at
from jobs
where id = ?
`
err := j.DB.QueryRow(query, id).
Scan(&job.ID, &job.OutputBucketPath, &job.Status, &job.VideoId, &job.Error, &job.CreatedAt, &job.UpdatedAt)
if err != nil {
return nil, err
}

job.Video, err = j.getJobVideo(job.VideoId)
if err != nil {
return nil, err
}

return job, nil
}

//goland:noinspection SqlResolve,SqlNoDataSourceInspection
func (j *JobRepositoryDb) Update(job *domain.Job) (*domain.Job, error) {
err := j.DB.Save(&job).Error
query := `update jobs set status = $1, updated_at = $2, error = $3 where id = $4 `

stmt, err := j.DB.Prepare(query)
if err != nil {
return nil, err
}
defer stmt.Close()

_, err = stmt.Exec(job.Status, job.UpdatedAt, job.Error, job.ID)
if err != nil {
return nil, err
}

return job, nil
}

//goland:noinspection SqlResolve,SqlNoDataSourceInspection
func (j *JobRepositoryDb) getJobVideo(videoId string) (*domain.Video, error) {
var video domain.Video

videoQuery := `select id, resource_id, file_path, created_at from videos where id = ?`
err := j.DB.QueryRow(videoQuery, videoId).Scan(&video.ID, &video.ResourceID, &video.FilePath, &video.CreatedAt)
if err != nil {
return nil, err
}

return &video, nil
}
20 changes: 16 additions & 4 deletions application/repositories/job_repository_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package repositories_test

import (
"github.com/jinzhu/gorm"
"database/sql"
"github.com/olukkas/go-encoder/application/repositories"
"github.com/olukkas/go-encoder/domain"
"github.com/olukkas/go-encoder/framework/database"
Expand All @@ -13,11 +13,17 @@ func TestJobRepositoryDb_Insert(t *testing.T) {
db := database.NewDataBaseTest()
defer db.Close()

err := createVideosTable(db)
require.Nil(t, err)

err = createJobsTable(db)
require.Nil(t, err)

jobRepo := repositories.NewJobRepositoryDb(db)

job := prepareJobHelper(t, db)

_, err := jobRepo.Insert(job)
_, err = jobRepo.Insert(job)
require.Nil(t, err)

j, err := jobRepo.Find(job.ID)
Expand All @@ -31,10 +37,16 @@ func TestJobRepositoryDb_Update(t *testing.T) {
db := database.NewDataBaseTest()
defer db.Close()

err := createVideosTable(db)
require.Nil(t, err)

err = createJobsTable(db)
require.Nil(t, err)

jobRepo := repositories.NewJobRepositoryDb(db)
job := prepareJobHelper(t, db)

_, err := jobRepo.Insert(job)
_, err = jobRepo.Insert(job)
require.Nil(t, err)

job.Status = domain.JobDownloading
Expand All @@ -43,7 +55,7 @@ func TestJobRepositoryDb_Update(t *testing.T) {
require.Equal(t, domain.JobDownloading, updated.Status)
}

func prepareJobHelper(t *testing.T, db *gorm.DB) *domain.Job {
func prepareJobHelper(t *testing.T, db *sql.DB) *domain.Job {
video, err := domain.NewVideo("resource", "path")
require.Nil(t, err)
require.NotNil(t, video)
Expand Down
59 changes: 51 additions & 8 deletions application/repositories/video_repository.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package repositories

import (
"fmt"
"github.com/jinzhu/gorm"
"database/sql"
"github.com/olukkas/go-encoder/domain"
uuid "github.com/satori/go.uuid"
)
Expand All @@ -13,33 +12,77 @@ type VideoRepository interface {
}

type VideoRepositoryDb struct {
DB *gorm.DB
DB *sql.DB
}

func NewVideoRepositoryDb(DB *gorm.DB) *VideoRepositoryDb {
func NewVideoRepositoryDb(DB *sql.DB) *VideoRepositoryDb {
return &VideoRepositoryDb{DB: DB}
}

//goland:noinspection SqlNoDataSourceInspection,SqlResolve
func (v *VideoRepositoryDb) Insert(video *domain.Video) (*domain.Video, error) {
if video.ID == "" {
video.ID = uuid.NewV4().String()
}

err := v.DB.Create(video).Error
query := "insert into videos (id, resource_id, file_path, created_at) values ($1, $2, $3, $4);"
stmt, err := v.DB.Prepare(query)
if err != nil {
return nil, err
}
defer stmt.Close()

_, err = stmt.Exec(video.ID, video.ResourceID, video.FilePath, video.CreatedAt)
if err != nil {
return nil, err
}

return video, nil
}

//goland:noinspection SqlResolve,SqlNoDataSourceInspection
func (v *VideoRepositoryDb) Find(id string) (*domain.Video, error) {
var video domain.Video
v.DB.Preloads("Jobs").First(&video, "id = ?", id)

if video.ID == "" {
return nil, fmt.Errorf("video with id %s does not exists \n", id)
videoQuery := `select id, resource_id, file_path, created_at from videos where id = ?`
err := v.DB.QueryRow(videoQuery, id).Scan(&video.ID, &video.ResourceID, &video.FilePath, &video.CreatedAt)
if err != nil {
return nil, err
}

video.Jobs, err = v.getVideoJobs(&video)
if err != nil {
return nil, err
}

return &video, nil
}

//goland:noinspection SqlNoDataSourceInspection,GoConvertStringLiterals,SqlResolve
func (v *VideoRepositoryDb) getVideoJobs(video *domain.Video) ([]*domain.Job, error) {
var jobs []*domain.Job

jobsQuery := `
select id, output_bucket_path, status, video_id, error, created_at, updated_at
from jobs where video_id = ?
`

rows, err := v.DB.Query(jobsQuery, video.ID)
if err != nil {
return nil, err
}
defer rows.Close()

for rows.Next() {
var job domain.Job
err = rows.Scan(&job.ID, &job.OutputBucketPath, &job.Status, &job.Error, &job.CreatedAt, &job.UpdatedAt)
if err != nil {
return nil, err
}

job.Video = video
jobs = append(jobs, &job)
}

return jobs, err
}
46 changes: 46 additions & 0 deletions application/repositories/video_repository_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package repositories_test

import (
"database/sql"
"github.com/olukkas/go-encoder/application/repositories"
"github.com/olukkas/go-encoder/domain"
"github.com/olukkas/go-encoder/framework/database"
Expand All @@ -12,6 +13,12 @@ func TestVideoRepositoryDB_Insert(t *testing.T) {
db := database.NewDataBaseTest()
defer db.Close()

err := createVideosTable(db)
require.Nil(t, err)

err = createJobsTable(db)
require.Nil(t, err)

video, err := domain.NewVideo("resource", "path")
require.Nil(t, err)
require.NotNil(t, video)
Expand All @@ -25,3 +32,42 @@ func TestVideoRepositoryDB_Insert(t *testing.T) {
require.NotEmpty(t, v.ID)
require.Equal(t, v.ID, video.ID)
}

//goland:noinspection SqlNoDataSourceInspection
func createVideosTable(db *sql.DB) error {
ddl := `
create table videos (
id text,
resource_id text,
file_path text,
created_at date
)
`
_, err := db.Exec(ddl)
if err != nil {
return err
}

return nil
}

//goland:noinspection SqlNoDataSourceInspection
func createJobsTable(db *sql.DB) error {
ddl := `
create table jobs (
id text,
output_bucket_path text,
status text,
video_id text,
error text,
created_at date,
updated_at date
)
`
_, err := db.Exec(ddl)
if err != nil {
return err
}

return nil
}
9 changes: 6 additions & 3 deletions application/services/job_manager.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package services

import (
"database/sql"
"encoding/json"
"github.com/jinzhu/gorm"
"github.com/olukkas/go-encoder/application/repositories"
"github.com/olukkas/go-encoder/framework/queue"
"github.com/streadway/amqp"
Expand All @@ -12,7 +12,7 @@ import (
)

type JobManager struct {
DB *gorm.DB
DB *sql.DB
MessageChannel chan amqp.Delivery
ReturnChannel chan JobWorkerResult
RabbitMQ *queue.RabbitMQ
Expand All @@ -24,7 +24,7 @@ type JobNotificationError struct {
}

func NewJobManager(
DB *gorm.DB,
DB *sql.DB,
rabbitMq *queue.RabbitMQ,
returnChannel chan JobWorkerResult,
messageChannel chan amqp.Delivery,
Expand Down Expand Up @@ -70,7 +70,10 @@ func (j *JobManager) Start() {
}

func (j *JobManager) notifySuccess(result JobWorkerResult) error {
Mutex.Lock()
jobJson, err := json.Marshal(result.Job)
Mutex.Unlock()

if err != nil {
return err
}
Expand Down
Loading