Change job repo interface

This commit is contained in:
Jan Eitzinger 2022-02-07 14:56:46 +01:00
parent 172c7173d3
commit d63130923c
2 changed files with 5 additions and 15 deletions

View File

@ -3,6 +3,7 @@ package api
import ( import (
"bufio" "bufio"
"context" "context"
"database/sql"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
@ -205,13 +206,12 @@ func (api *RestApi) startJob(rw http.ResponseWriter, r *http.Request) {
// Check if combination of (job_id, cluster_id, start_time) already exists: // Check if combination of (job_id, cluster_id, start_time) already exists:
job, err := api.JobRepository.Find(req.JobID, req.Cluster, req.StartTime) job, err := api.JobRepository.Find(req.JobID, req.Cluster, req.StartTime)
if err != nil { if err != nil && err != sql.ErrNoRows {
print("ERROR in Find\n")
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
} }
if job != nil { if err != sql.ErrNoRows {
http.Error(rw, fmt.Sprintf("a job with that job_id, cluster_id and start_time already exists (database id: %d)", job.ID), http.StatusUnprocessableEntity) http.Error(rw, fmt.Sprintf("a job with that job_id, cluster_id and start_time already exists (database id: %d)", job.ID), http.StatusUnprocessableEntity)
return return
} }

View File

@ -17,7 +17,7 @@ type JobRepository struct {
// The job is queried using the batch job id, the cluster name, // The job is queried using the batch job id, the cluster name,
// and the start time of the job in UNIX epoch time seconds. // and the start time of the job in UNIX epoch time seconds.
// It returns a pointer to a schema.Job data structure and an error variable. // It returns a pointer to a schema.Job data structure and an error variable.
// If the job was not found nil is returned for the job pointer. // To check if no job was found test err == sql.ErrNoRows
func (r *JobRepository) Find( func (r *JobRepository) Find(
jobId int64, jobId int64,
cluster string, cluster string,
@ -33,18 +33,13 @@ func (r *JobRepository) Find(
} }
job, err := schema.ScanJob(r.DB.QueryRowx(sqlQuery, args...)) job, err := schema.ScanJob(r.DB.QueryRowx(sqlQuery, args...))
// Reset error if no job is found as this is indicated by nil job ptr
if err == sql.ErrNoRows {
err = nil
}
return job, err return job, err
} }
// FindById executes a SQL query to find a specific batch job. // FindById executes a SQL query to find a specific batch job.
// The job is queried using the database id. // The job is queried using the database id.
// It returns a pointer to a schema.Job data structure and an error variable. // It returns a pointer to a schema.Job data structure and an error variable.
// If the job was not found nil is returned for the job pointer. // To check if no job was found test err == sql.ErrNoRows
func (r *JobRepository) FindById( func (r *JobRepository) FindById(
jobId int64) (*schema.Job, error) { jobId int64) (*schema.Job, error) {
sqlQuery, args, err := sq.Select(schema.JobColumns...). sqlQuery, args, err := sq.Select(schema.JobColumns...).
@ -54,11 +49,6 @@ func (r *JobRepository) FindById(
} }
job, err := schema.ScanJob(r.DB.QueryRowx(sqlQuery, args...)) job, err := schema.ScanJob(r.DB.QueryRowx(sqlQuery, args...))
// Reset error if no job is found as this is indicated by nil job ptr
if err == sql.ErrNoRows {
err = nil
}
return job, err return job, err
} }