/
listJobs.go
41 lines (32 loc) · 966 Bytes
/
listJobs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package handler
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/revogabe/go-jobsdev/schemas"
"go.mongodb.org/mongo-driver/bson"
)
func ListJobsHandler(ctx *gin.Context) {
jobs := []schemas.Jobs{}
cur, err := db.Collection("jobs").Find(ctx, bson.M{})
if err != nil {
logger.Errorf("Error getting jobs: %v", err.Error())
sendError(ctx, http.StatusInternalServerError, "Error getting jobs from database")
return
}
defer cur.Close(ctx)
for cur.Next(ctx) {
var job schemas.Jobs
if err := cur.Decode(&job); err != nil {
logger.Errorf("Error decoding job: %v", err.Error())
sendError(ctx, http.StatusInternalServerError, "Error getting jobs from database")
return
}
jobs = append(jobs, job)
}
if err := cur.Err(); err != nil {
logger.Errorf("Error getting jobs: %v", err.Error())
sendError(ctx, http.StatusInternalServerError, "Error getting jobs from database")
return
}
sendSuccess(ctx, "list-jobs", jobs)
}