From 6466883832fa68963f4debb227f0e645b00e0d4e Mon Sep 17 00:00:00 2001 From: Seth Etter Date: Mon, 29 Jul 2024 12:32:50 -0500 Subject: [PATCH] fix tests, add json fields to getJob --- pkg/data/data.go | 13 ++++++++++--- pkg/server/routes.go | 1 + pkg/server/routes_test.go | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pkg/data/data.go b/pkg/data/data.go index 1f74f37..b193518 100644 --- a/pkg/data/data.go +++ b/pkg/data/data.go @@ -22,9 +22,9 @@ type Job struct { Position string `db:"position" json:"position"` Organization string `db:"organization" json:"organization"` Url sql.NullString `db:"url" json:"-"` - JSONUrl *string `json:"url"` + JSONUrl *string `db:"-" json:"url"` Description sql.NullString `db:"description" json:"-"` - DescriptionJSON *string `json:"description"` + DescriptionJSON *string `db:"-" json:"description"` Email string `db:"email" json:"email"` PublishedAt time.Time `db:"published_at" json:"published_at"` } @@ -76,7 +76,7 @@ func (job *Job) RenderDescription() (string, error) { func (job *Job) Save(db *sqlx.DB) (sql.Result, error) { return db.Exec( "UPDATE jobs SET position = $1, organization = $2, url = $3, description = $4 WHERE id = $5", - job.Position, job.Organization, job.JSONUrl, job.DescriptionJSON, job.ID, + job.Position, job.Organization, job.Url, job.Description, job.ID, ) } @@ -122,6 +122,13 @@ func GetJob(id string, db *sqlx.DB) (Job, error) { return job, err } + if !job.Url.Valid { + job.JSONUrl = &job.Url.String + } + if !job.Description.Valid { + job.DescriptionJSON = &job.Description.String + } + return job, nil } diff --git a/pkg/server/routes.go b/pkg/server/routes.go index a7fd795..d9b57a9 100644 --- a/pkg/server/routes.go +++ b/pkg/server/routes.go @@ -44,6 +44,7 @@ func (ctrl *Controller) JobsJSON(ctx *gin.Context) { return } + ctx.Header("Content-Type", "application/json") ctx.JSON(200, gin.H{"items": jobs}) } diff --git a/pkg/server/routes_test.go b/pkg/server/routes_test.go index 3808d52..f8b4dda 100644 --- a/pkg/server/routes_test.go +++ b/pkg/server/routes_test.go @@ -510,7 +510,7 @@ func getDbFields(thing interface{}) []string { for i := 0; i < t.NumField(); i++ { dbTag := t.Field(i).Tag.Get("db") - if dbTag != "" { + if dbTag != "-" { dbFields = append(dbFields, dbTag) } }