From d63130923cfca29a780c72b37629e17f48ba5851 Mon Sep 17 00:00:00 2001 From: Jan Eitzinger Date: Mon, 7 Feb 2022 14:56:46 +0100 Subject: [PATCH] Change job repo interface --- api/rest.go | 6 +++--- repository/job.go | 14 ++------------ 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/api/rest.go b/api/rest.go index 7059928..1aa72ea 100644 --- a/api/rest.go +++ b/api/rest.go @@ -3,6 +3,7 @@ package api import ( "bufio" "context" + "database/sql" "encoding/json" "fmt" "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: job, err := api.JobRepository.Find(req.JobID, req.Cluster, req.StartTime) - if err != nil { - print("ERROR in Find\n") + if err != nil && err != sql.ErrNoRows { http.Error(rw, err.Error(), http.StatusInternalServerError) 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) return } diff --git a/repository/job.go b/repository/job.go index a936065..44389be 100644 --- a/repository/job.go +++ b/repository/job.go @@ -17,7 +17,7 @@ type JobRepository struct { // The job is queried using the batch job id, the cluster name, // 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. -// 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( jobId int64, cluster string, @@ -33,18 +33,13 @@ func (r *JobRepository) Find( } 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 } // FindById executes a SQL query to find a specific batch job. // The job is queried using the database id. // 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( jobId int64) (*schema.Job, error) { sqlQuery, args, err := sq.Select(schema.JobColumns...). @@ -54,11 +49,6 @@ func (r *JobRepository) FindById( } 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 }