Created
June 1, 2020 06:31
-
-
Save hadoan/44435f850fabab3960f4a618fdc6f14b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func getJobPostsEndpoint(response http.ResponseWriter, request *http.Request) { | |
response.Header().Add("content-type", "application/json") | |
var jobs []Job | |
collection := client.Database("Default").Collection("AppJobs") | |
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second) | |
cursor, err := collection.Find(ctx, bson.M{}) | |
if err != nil { | |
response.WriteHeader(http.StatusInternalServerError) | |
response.Write([]byte(`{"message": ` + err.Error() + `"}`)) | |
return | |
} | |
defer cursor.Close(ctx) | |
for cursor.Next(ctx) { | |
var job Job | |
cursor.Decode(&job) | |
fmt.Printf("%+v\n", job) | |
jobs = append(jobs, job) | |
} | |
if err := cursor.Err(); err != nil { | |
response.WriteHeader(http.StatusInternalServerError) | |
response.Write([]byte(`{"message": ` + err.Error() + `"}`)) | |
return | |
} | |
json.NewEncoder(response).Encode(jobs) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment