diff --git a/api/src/controller/controller.go b/api/src/controller/controller.go index e79dea7..6bc7239 100644 --- a/api/src/controller/controller.go +++ b/api/src/controller/controller.go @@ -159,6 +159,22 @@ func (pg *PgController) Serve() *gin.Engine { } c.JSON(http.StatusOK, collections) }) + // Create an endpoint that takes in a customerID and returns all collections with no customerID or a matching customerID. + r.GET("/collections/:customerId", func(c * gin.Context) { + + // Get Customer ID + id := c.Param("customerId") + intId, err := strconv.Atoi(id) + if err != nil { + panic(err) + } + + collections, err := pg.AllCustomerCollections(int64(intId)) + if err != nil { + c.JSON(http.StatusInternalServerError, "Oops") + } + c.JSON(http.StatusOK, collections) + }) r.POST("/addGift", func(c *gin.Context) { var input model.Gift fmt.Print(c) diff --git a/api/src/model/model.go b/api/src/model/model.go index f6c5c00..80a3b1e 100644 --- a/api/src/model/model.go +++ b/api/src/model/model.go @@ -26,6 +26,7 @@ type Model interface { SearchGifts(string, int, int) ([]Gift, error) AllGiftResponses() ([]GiftResponse, error) AllCollections() ([]GiftCollection, error) + AllCustomerCollections(id int64) ([]GiftCollection, error) UpdateCollection(GiftCollection) (GiftCollection, error) AddGiftToGiftCollection(Gift, int64) (GiftCollection, error) DeleteGiftFromGiftCollection(int64, int64) (GiftCollection, error) @@ -188,6 +189,15 @@ func (m *PgModel) AllCollections() ([]GiftCollection, error) { return collections, nil } +func (m *PgModel) AllCustomerCollections(id int64) ([]GiftCollection, error) { + collections, err := GetAllCustomerCollectionsFromDB(m.Conn, id) + + if err != nil { + return []GiftCollection{}, err + } + return collections, nil +} + func (m *PgModel) IncompleteRequests() ([]GiftRequest, error) { gifts, err := GetIncompleteGiftRequestsFromDB(m.Conn) diff --git a/api/src/model/transactions.go b/api/src/model/transactions.go index fdce1c5..4c46573 100644 --- a/api/src/model/transactions.go +++ b/api/src/model/transactions.go @@ -232,6 +232,15 @@ func GetAllCollectionsFromDB(db *gorm.DB) ([]GiftCollection, error) { return collections, nil } +// GetAllCustomerCollectionsFromDB fetches all GiftCollections that associated with the customer ID or none +func GetAllCustomerCollectionsFromDB(db *gorm.DB, id int64) ([]GiftCollection, error) { + var collections []GiftCollection + if err := db.Where("customer_id = ? OR customer_id IS NULL", id).Preload("Gifts").Find(&collections).Error; err != nil { + return nil, err + } + return collections, nil +} + func AddGiftToCollectionFromDB(db *gorm.DB, inputGift Gift, id int64) (GiftCollection, error) { var collection GiftCollection if err := db.Where("id = ?", id).First(&collection).Error; err != nil { diff --git a/api/tests/api_test.go b/api/tests/api_test.go index 91de218..4509527 100644 --- a/api/tests/api_test.go +++ b/api/tests/api_test.go @@ -404,7 +404,6 @@ func TestAddCollection(t *testing.T) { assert.Equal(t, retrievedCollection.Gifts[0].Name, addedCollection.Gifts[0].Name) } -//---------------CRUD GIFT ENDPOINT TESTS-------------------------------------- func TestGetGift(t *testing.T) { // Database setup @@ -806,7 +805,7 @@ func TestGetAllGiftCollection(t *testing.T) { t.Fatalf("Unable to connect to database: %v", err) } // Put auto migrations here - err = db.AutoMigrate(&model.GiftCollection{}) + err = db.AutoMigrate(&model.GiftCollection{}, &model.User{}, &model.Customer{}, &model.Gift{}) if err != nil { panic("failed to migrate test database schema") } @@ -822,24 +821,54 @@ func TestGetAllGiftCollection(t *testing.T) { // Test code w := httptest.NewRecorder() - uintValue := uint(5) + // Create a Customer + user := model.User{} + err = tx.Create(&user).Error + assert.NoError(t, err) + var retrievedUser model.User + err = tx.First(&retrievedUser).Error + assert.NoError(t, err) + customer := model.Customer{ + User: retrievedUser, + } + err = tx.Create(&customer).Error + assert.NoError(t, err) + var retrievedCustomer model.Customer + err = tx.First(&retrievedCustomer).Error + assert.NoError(t, err) + + // Second Customer + user2 := model.User{} + err = tx.Create(&user2).Error + assert.NoError(t, err) + var retrievedUser2 model.User + err = tx.Where("id = ?", user2.ID).First(&retrievedUser2).Error + assert.NoError(t, err) + customer2 := model.Customer{ + User: retrievedUser2, + } + err = tx.Create(&customer2).Error + assert.NoError(t, err) + var retrievedCustomer2 model.Customer + err = tx.Where("id = ?", customer2.ID).First(&retrievedCustomer2).Error + assert.NoError(t, err) collection := model.GiftCollection{ - CustomerID: &uintValue, + CustomerID: &retrievedCustomer.ID, CollectionName: "sample name", Gifts: []*model.Gift{}, } - uintValue = uint(6) + collection_two := model.GiftCollection{ - CustomerID: &uintValue, + CustomerID: &retrievedCustomer2.ID, CollectionName: "sample name 2", Gifts: []*model.Gift{}, } - err = db.Create(&collection).Error + err = tx.Create(&collection).Error assert.NoError(t, err) - err = db.Create(&collection_two).Error + err = tx.Create(&collection_two).Error assert.NoError(t, err) req1, err := http.NewRequest("GET", fmt.Sprintf("/collections"), nil) @@ -1091,3 +1120,108 @@ func TestGiftDeleteFromCollection(t *testing.T) { count2 = int64(len(giftDeletedRetrievedCollection.Gifts)) assert.Equal(t, int64(0), count2) } + +func TestGetAllCustomerGiftCollection(t *testing.T) { + // Database setup + dsn := "user=testuser password=testpwd host=localhost port=5433 dbname=testdb sslmode=disable" + if dbURL, exists := os.LookupEnv("TEST_DATABASE_URL"); exists { + dsn = dbURL + } + db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) + if err != nil { + t.Fatalf("Unable to connect to database: %v", err) + } + // Put auto migrations here + err = db.AutoMigrate(&model.GiftCollection{}) + if err != nil { + panic("failed to migrate test database schema") + } + // Wrap the DB connection in a transaction + tx := db.Begin() + defer tx.Rollback() + + // Create Model and Controller + m := &model.PgModel{Conn: tx} + c := &c.PgController{Model: m} + router := c.Serve() + + // Test code + w := httptest.NewRecorder() + + + // Create a Customer + user := model.User{} + err = tx.Create(&user).Error + assert.NoError(t, err) + var retrievedUser model.User + err = tx.First(&retrievedUser).Error + assert.NoError(t, err) + customer := model.Customer{ + User: retrievedUser, + } + err = tx.Create(&customer).Error + assert.NoError(t, err) + var retrievedCustomer model.Customer + err = tx.First(&retrievedCustomer).Error + assert.NoError(t, err) + + // Second Customer + user2 := model.User{} + err = tx.Create(&user2).Error + assert.NoError(t, err) + var retrievedUser2 model.User + err = tx.Where("id = ?", user2.ID).First(&retrievedUser2).Error + assert.NoError(t, err) + customer2 := model.Customer{ + User: retrievedUser2, + } + err = tx.Create(&customer2).Error + assert.NoError(t, err) + var retrievedCustomer2 model.Customer + err = tx.Where("id = ?", customer2.ID).First(&retrievedCustomer2).Error + assert.NoError(t, err) + + collection := model.GiftCollection{ + CustomerID: &retrievedCustomer.ID, + CollectionName: "sample name", + Gifts: []*model.Gift{}, + } + + collection_two := model.GiftCollection{ + CustomerID: &retrievedCustomer2.ID, + CollectionName: "sample name 2", + Gifts: []*model.Gift{}, + } + + collection_three := model.GiftCollection{ + CollectionName: "sample name 3", + Gifts: []*model.Gift{}, + } + + err = tx.Create(&collection).Error + assert.NoError(t, err) + + err = tx.Create(&collection_two).Error + assert.NoError(t, err) + + err = tx.Create(&collection_three).Error + assert.NoError(t, err) + + req1, err := http.NewRequest("GET", fmt.Sprintf("/collections/%d", retrievedCustomer2.ID), nil) + router.ServeHTTP(w, req1) + assert.Equal(t, 200, w.Code) + + var collectionRetrieved []model.GiftCollection + if e := json.Unmarshal(w.Body.Bytes(), &collectionRetrieved); e != nil { + t.Fatalf("Error unmarshaling JSON: %v", e) + } + + assert.Equal(t, collection_two.CustomerID, collectionRetrieved[0].CustomerID) + assert.Equal(t, collection_two.CollectionName, collectionRetrieved[0].CollectionName) + assert.Equal(t, collection_two.Gifts, collectionRetrieved[0].Gifts) + + assert.Equal(t, collection_three.CustomerID, collectionRetrieved[1].CustomerID) + assert.Equal(t, collection_three.CollectionName, collectionRetrieved[1].CollectionName) + assert.Equal(t, collection_three.Gifts, collectionRetrieved[1].Gifts) + +} \ No newline at end of file