func (pg *PostgresDBManager) SaveFeed(feed *models.Feed) bool { var feedStmt, itemStmt *sql.Stmt var err error var randID uuid.UUID // save feed to db feedStmt, err = pg.db.Prepare("INSERT INTO feeds VALUES($1, $2, $3, $4, $5, $6, $7)") if err != nil { log.Fatal(err) return false } // add id randID, err = uuid.V4() if err != nil { log.Fatal(err) return false } feed.ID = randID.String() _, err = feedStmt.Exec(feed.ID, feed.Title, feed.Description, feed.URL, feed.FeedURL, feed.LastPubDate, feed.ImageURL) if err != nil { log.Fatal(err) return false } // save feed Items itemStmt, err = pg.db.Prepare("INSERT INTO feed_items VALUES($1, $2, $3, $4, $5, $6, $7, $8)") if err != nil { log.Fatal(err) return false } for _, item := range feed.Items { randID, err = uuid.V4() if err != nil { log.Fatal(err) return false } item.ID = randID.String() _, err = itemStmt.Exec(item.ID, item.Title, item.Description, item.URL, item.MediaURL, item.PubDate, item.ImageURL, feed.ID) if err != nil { log.Fatal(err) return false } } return true }
func generateFakeResponse(streamID uuid.UUID) []model.StreamItem { //fake data uuid1, _ := uuid.V4() uuid2, _ := uuid.V4() return []model.StreamItem{ { ID: uuid1.String(), Timestamp: time.Now(), Type: model.TypePost, StreamID: streamID.String(), }, { ID: uuid2.String(), Timestamp: time.Now(), Type: model.TypeRepost, StreamID: streamID.String(), }, } }
) var _ = Describe("StreamController", func() { var id uuid.UUID BeforeEach(func() { id, _ = uuid.V4() }) Context("when adding content via PUT /streams", func() { It("should return a status 201 when passed a correct body", func() { item1ID, _ := uuid.V4() item2ID, _ := uuid.V4() items := []model.StreamItem{{ StreamID: id.String(), Timestamp: time.Now(), Type: 0, ID: item1ID.String(), }, { StreamID: id.String(), Timestamp: time.Now(), Type: 1, ID: item2ID.String(), }} itemsJSON, _ := json.Marshal(items) Request("PUT", "/streams", string(itemsJSON)) logResponse(response) Expect(response.Code).To(Equal(http.StatusCreated))