Skip to content

Commit

Permalink
delete button on edit job form
Browse files Browse the repository at this point in the history
  • Loading branch information
sethetter committed Feb 23, 2025
1 parent cf8e979 commit ba90728
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
15 changes: 15 additions & 0 deletions pkg/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,21 @@ func GetJob(id string, db *sqlx.DB) (Job, error) {
return job, nil
}

func DeleteJob(id string, db *sqlx.DB) (error) {
result, err := db.Exec("DELETE FROM jobs WHERE id = $1", id)
if err != nil {
return err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return err
}
if rowsAffected != 1 {
return fmt.Errorf("failed to delete job: %w", err)
}
return nil
}

type NewJob struct {
Position string `form:"position"`
Organization string `form:"organization"`
Expand Down
19 changes: 19 additions & 0 deletions pkg/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,25 @@ func (ctrl *Controller) ViewJob(ctx *gin.Context) {
ctx.HTML(200, "view", gin.H{"job": job, "description": template.HTML(description)})
}

func (ctrl *Controller) DeleteJob(ctx *gin.Context) {
id := ctx.Param("id")
err := data.DeleteJob(id, ctrl.DB)
if err != nil {
log.Println(fmt.Errorf("failed to delete job: %w", err))
ctx.AbortWithStatus(http.StatusInternalServerError)
return
}

session := sessions.Default(ctx)
session.AddFlash("Job deleted!")
if err := session.Save(); err != nil {
log.Println(fmt.Errorf("DeleteJob failed to session.Save: %w", err))
}

ctx.Redirect(http.StatusFound, "/")
}


func addFlash(ctx *gin.Context, base gin.H) gin.H {
session := sessions.Default(ctx)
base["flashes"] = session.Flashes()
Expand Down
1 change: 1 addition & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func NewServer(c *ServerConfig) (http.Server, error) {
{
authorized.GET("/jobs/:id/edit", ctrl.EditJob)
authorized.POST("/jobs/:id", ctrl.UpdateJob)
authorized.POST("/jobs/:id/delete", ctrl.DeleteJob)
}

return http.Server{
Expand Down
12 changes: 11 additions & 1 deletion templates/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
<span class="form-description">Please provide a description below if no URL is available.</span>
<textarea name="description" rows="4" class="form-textarea mb-3">{{ .job.Description.String }}</textarea>
</label>
<button class="btn btn-primary mt-6">Update</button>
<div class="flex gap-4 mt-6">
<button class="btn btn-primary">Update</button>
</div>
</form>

<form method="POST"
action="/jobs/{{ .job.ID }}/delete?token={{ .token }}"
onsubmit="return confirm('Are you sure you want to delete this job posting?')"
class="mt-4">
<button type="submit" class="btn btn-danger">Delete Job</button>
</form>

{{ end }}

0 comments on commit ba90728

Please sign in to comment.