From 99865f415238449e0c7e91b3048b37e8d1e27eba Mon Sep 17 00:00:00 2001 From: Jan Eitzinger Date: Sun, 6 Feb 2022 09:48:31 +0100 Subject: [PATCH] Start to extract DB repositories --- api/rest.go | 17 +++++++++-------- repository/job.go | 40 ++++++++++++++++++++++++++++++++++++++++ repository/user.go | 1 + 3 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 repository/job.go create mode 100644 repository/user.go diff --git a/api/rest.go b/api/rest.go index 7b75fae..f5d571d 100644 --- a/api/rest.go +++ b/api/rest.go @@ -17,6 +17,7 @@ import ( "github.com/ClusterCockpit/cc-backend/graph/model" "github.com/ClusterCockpit/cc-backend/log" "github.com/ClusterCockpit/cc-backend/metricdata" + "github.com/ClusterCockpit/cc-backend/repository" "github.com/ClusterCockpit/cc-backend/schema" sq "github.com/Masterminds/squirrel" "github.com/gorilla/mux" @@ -24,6 +25,7 @@ import ( ) type RestApi struct { + r *repository.JobRepository DB *sqlx.DB Resolver *graph.Resolver AsyncArchiving bool @@ -51,7 +53,7 @@ func (api *RestApi) MountRoutes(r *mux.Router) { } } -type StartJobApiRespone struct { +type StartJobApiResponse struct { DBID int64 `json:"id"` } @@ -153,14 +155,14 @@ func (api *RestApi) tagJob(rw http.ResponseWriter, r *http.Request) { for _, tag := range req { var tagId int64 - if err := sq.Select("id").From("tag"). - Where("tag.tag_type = ?", tag.Type).Where("tag.tag_name = ?", tag.Name). - RunWith(api.DB).QueryRow().Scan(&tagId); err != nil { + exists := false + + if exists, tagId = api.r.TagExists(tag.Type, tag.Name); exists { http.Error(rw, fmt.Sprintf("the tag '%s:%s' does not exist", tag.Type, tag.Name), http.StatusNotFound) return } - if _, err := api.DB.Exec(`INSERT INTO jobtag (job_id, tag_id) VALUES (?, ?)`, job.ID, tagId); err != nil { + if err := api.r.AddTag(job.JobID, tagId); err != nil { http.Error(rw, err.Error(), http.StatusInternalServerError) return } @@ -204,8 +206,7 @@ func (api *RestApi) startJob(rw http.ResponseWriter, r *http.Request) { } // Check if combination of (job_id, cluster_id, start_time) already exists: - rows, err := api.DB.Query(`SELECT job.id FROM job WHERE job.job_id = ? AND job.cluster = ? AND job.start_time = ?`, - req.JobID, req.Cluster, req.StartTime) + rows, err := api.r.JobExists(req.JobID, req.Cluster, req.StartTime) if err != nil { http.Error(rw, err.Error(), http.StatusInternalServerError) return @@ -250,7 +251,7 @@ func (api *RestApi) startJob(rw http.ResponseWriter, r *http.Request) { log.Printf("new job (id: %d): cluster=%s, jobId=%d, user=%s, startTime=%d", id, req.Cluster, req.JobID, req.User, req.StartTime) rw.Header().Add("Content-Type", "application/json") rw.WriteHeader(http.StatusCreated) - json.NewEncoder(rw).Encode(StartJobApiRespone{ + json.NewEncoder(rw).Encode(StartJobApiResponse{ DBID: id, }) } diff --git a/repository/job.go b/repository/job.go new file mode 100644 index 0000000..e053bc0 --- /dev/null +++ b/repository/job.go @@ -0,0 +1,40 @@ +package repository + +import ( + "database/sql" + + sq "github.com/Masterminds/squirrel" + "github.com/jmoiron/sqlx" +) + +type JobRepository struct { + DB *sqlx.DB +} + +func (r *JobRepository) JobExists(jobId int64, cluster string, startTime int64) (rows *sql.Rows, err error) { + rows, err = r.DB.Query(`SELECT job.id FROM job WHERE job.job_id = ? AND job.cluster = ? AND job.start_time = ?`, + jobId, cluster, startTime) + return +} + +func (r *JobRepository) IdExists(jobId int64) bool { + + return true +} + +func (r *JobRepository) AddTag(jobId int64, tagId int64) error { + _, err := r.DB.Exec(`INSERT INTO jobtag (job_id, tag_id) VALUES (?, ?)`, jobId, tagId) + return err +} + +func (r *JobRepository) TagExists(tagType string, tagName string) (exists bool, tagId int64) { + exists = true + if err := sq.Select("id").From("tag"). + Where("tag.tag_type = ?", tagType).Where("tag.tag_name = ?", tagName). + RunWith(r.DB).QueryRow().Scan(&tagId); err != nil { + exists = false + return exists, tagId + } else { + return exists, tagId + } +} diff --git a/repository/user.go b/repository/user.go new file mode 100644 index 0000000..50a4378 --- /dev/null +++ b/repository/user.go @@ -0,0 +1 @@ +package repository