From 453560948191c7eaaefa4a54feec939c0fa05c41 Mon Sep 17 00:00:00 2001 From: Seth Etter Date: Sun, 28 Jul 2024 20:43:45 -0500 Subject: [PATCH] add jobs list api endpoint --- .tool-versions | 1 + docker-compose.yml | 2 -- pkg/data/data.go | 27 +++++++++++++++++++-------- pkg/server/routes.go | 11 +++++++++++ pkg/server/server.go | 2 ++ 5 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 .tool-versions diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..009efa3 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +golang 1.22.5 diff --git a/docker-compose.yml b/docker-compose.yml index 39fd24c..3b68b58 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,3 @@ -version: '3' - services: db: image: postgres:14 diff --git a/pkg/data/data.go b/pkg/data/data.go index 1c1bd1c..1f74f37 100644 --- a/pkg/data/data.go +++ b/pkg/data/data.go @@ -18,13 +18,15 @@ import ( ) type Job struct { - ID string `db:"id"` - Position string `db:"position"` - Organization string `db:"organization"` - Url sql.NullString `db:"url"` - Description sql.NullString `db:"description"` - Email string `db:"email"` - PublishedAt time.Time `db:"published_at"` + ID string `db:"id" json:"id"` + Position string `db:"position" json:"position"` + Organization string `db:"organization" json:"organization"` + Url sql.NullString `db:"url" json:"-"` + JSONUrl *string `json:"url"` + Description sql.NullString `db:"description" json:"-"` + DescriptionJSON *string `json:"description"` + Email string `db:"email" json:"email"` + PublishedAt time.Time `db:"published_at" json:"published_at"` } const ( @@ -74,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.Url, job.Description, job.ID, + job.Position, job.Organization, job.JSONUrl, job.DescriptionJSON, job.ID, ) } @@ -100,6 +102,15 @@ func GetAllJobs(db *sqlx.DB) ([]Job, error) { return jobs, err } + for i := range jobs { + if !jobs[i].Url.Valid { + jobs[i].JSONUrl = &jobs[i].Url.String + } + if !jobs[i].Description.Valid { + jobs[i].DescriptionJSON = &jobs[i].Description.String + } + } + return jobs, nil } diff --git a/pkg/server/routes.go b/pkg/server/routes.go index c1e401c..a7fd795 100644 --- a/pkg/server/routes.go +++ b/pkg/server/routes.go @@ -36,6 +36,17 @@ func (ctrl *Controller) Index(ctx *gin.Context) { })) } +func (ctrl *Controller) JobsJSON(ctx *gin.Context) { + jobs, err := data.GetAllJobs(ctrl.DB) + if err != nil { + log.Println(fmt.Errorf("JobsJSON failed to getAllJobs: %w", err)) + ctx.AbortWithStatus(http.StatusInternalServerError) + return + } + + ctx.JSON(200, gin.H{"items": jobs}) +} + func (ctrl *Controller) About(ctx *gin.Context) { ctx.HTML(200, "about", gin.H{}) } diff --git a/pkg/server/server.go b/pkg/server/server.go index 334b1a7..b54e0c0 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -70,6 +70,8 @@ func NewServer(c *ServerConfig) (http.Server, error) { router.POST("/jobs", ctrl.CreateJob) router.GET("/jobs/:id", ctrl.ViewJob) + router.GET("/api/jobs", ctrl.JobsJSON) + authorized := router.Group("/") authorized.Use(requireTokenAuth(sqlxDb, c.Config.AppSecret, JobRoute)) {