// hydrate loads a Gallery with form submissions from the MongoDB // database collection. func hydrate(context interface{}, db *db.DB, gallery *Gallery) error { // Load all the submission id's from the answers inside the gallery into // an array so that we can query by them all. var submissionIDs = make([]string, len(gallery.Answers)) for i, answer := range gallery.Answers { submissionIDs[i] = answer.SubmissionID.Hex() } submissions, err := submission.RetrieveMany(context, db, submissionIDs) if err != nil { return err } mergeSubmissionsIntoGalleryAnswers(gallery, submissions) return nil }
// hydrateMany loads an array of form galleries with form submissions // from the MongoDB database collection. func hydrateMany(context interface{}, db *db.DB, galleries []Gallery) error { // Load all the submission id's from the answers inside the gallery. var submissionIDs []string for _, gallery := range galleries { for _, answer := range gallery.Answers { submissionIDs = append(submissionIDs, answer.SubmissionID.Hex()) } } submissions, err := submission.RetrieveMany(context, db, submissionIDs) if err != nil { return err } for i := range galleries { mergeSubmissionsIntoGalleryAnswers(&galleries[i], submissions) } return nil }
func Test_RetrieveMany(t *testing.T) { subs, db := setup(t, "submission") defer teardown(t, db) t.Log("Given the need to retrieve many submissions.") { t.Log("\tWhen starting from an empty submissions collection") { //---------------------------------------------------------------------- // Create the submissions. ids := make([]string, 0, len(subs)) for _, sub := range subs { ids = append(ids, sub.ID.Hex()) if err := submission.Create(tests.Context, db, sub.FormID.Hex(), &sub); err != nil { t.Fatalf("\t%s\tShould be able to create a submission : %s", tests.Failed, err) } } t.Logf("\t%s\tShould be able to create submissions.", tests.Success) rsubs, err := submission.RetrieveMany(tests.Context, db, ids) if err != nil { t.Fatalf("\t%s\tShould be able to list submissions : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to list submissions", tests.Success) //---------------------------------------------------------------------- // Verify that the docs exist inside the results. matchSubmissions(t, subs, rsubs) } } }