Skip to content

Commit

Permalink
add jobs list api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
sethetter committed Jul 29, 2024
1 parent d923ad9 commit 4535609
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
golang 1.22.5
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3'

services:
db:
image: postgres:14
Expand Down
27 changes: 19 additions & 8 deletions pkg/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
)
}

Expand All @@ -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
}

Expand Down
11 changes: 11 additions & 0 deletions pkg/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down

0 comments on commit 4535609

Please sign in to comment.