From 97b1b5be61b11ad8a9af07181821b3593cbebcda Mon Sep 17 00:00:00 2001 From: joey-tsai Date: Mon, 6 Nov 2023 18:34:38 -0500 Subject: [PATCH 1/5] Finish endpoint --- api/src/controller/controller.go | 16 ++++++++++++++++ api/src/model/model.go | 10 ++++++++++ api/src/model/transactions.go | 9 +++++++++ 3 files changed, 35 insertions(+) diff --git a/api/src/controller/controller.go b/api/src/controller/controller.go index e79dea7..bce685c 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/:id", func(c * gin.Context) { + + // Get Customer ID + id := c.Param("id") + 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..6932c2c 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) + AllCustomerCollectionsid(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) AllCustomerCollectionsid(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..f382620 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("id = ? OR 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 { From 924456f3753246952e42f32be540849acc1689ba Mon Sep 17 00:00:00 2001 From: joey-tsai Date: Mon, 6 Nov 2023 19:22:01 -0500 Subject: [PATCH 2/5] Finish Test --- api/tests/api_test.go | 79 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/api/tests/api_test.go b/api/tests/api_test.go index 91de218..7cf4238 100644 --- a/api/tests/api_test.go +++ b/api/tests/api_test.go @@ -1091,3 +1091,82 @@ 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() + + uintValue := uint(5) + + collection := model.GiftCollection{ + CustomerID: &uintValue, + CollectionName: "sample name", + Gifts: []*model.Gift{}, + } + + uintValue = uint(6) + collection_two := model.GiftCollection{ + CustomerID: &uintValue, + CollectionName: "sample name 2", + Gifts: []*model.Gift{}, + } + + collection_three := model.GiftCollection{ + CustomerID: nil, + CollectionName: "sample name 3", + Gifts: []*model.Gift{}, + } + + err = db.Create(&collection).Error + assert.NoError(t, err) + + err = db.Create(&collection_two).Error + assert.NoError(t, err) + + err = db.Create(&collection_three).Error + assert.NoError(t, err) + + req1, err := http.NewRequest("GET", fmt.Sprintf("/collections/%d", uintValue), 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) + + var collectionCount int64 + collectionCount = int64(len(collectionRetrieved)) + assert.Equal(t, int64(2), collectionCount) +} \ No newline at end of file From c6c0bd5bbc1efbd06db431d247f8589b3b26d4fa Mon Sep 17 00:00:00 2001 From: joey-tsai Date: Mon, 6 Nov 2023 19:22:59 -0500 Subject: [PATCH 3/5] Small Fix --- api/src/model/model.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/src/model/model.go b/api/src/model/model.go index 6932c2c..80a3b1e 100644 --- a/api/src/model/model.go +++ b/api/src/model/model.go @@ -26,7 +26,7 @@ type Model interface { SearchGifts(string, int, int) ([]Gift, error) AllGiftResponses() ([]GiftResponse, error) AllCollections() ([]GiftCollection, error) - AllCustomerCollectionsid(id int64) ([]GiftCollection, error) + AllCustomerCollections(id int64) ([]GiftCollection, error) UpdateCollection(GiftCollection) (GiftCollection, error) AddGiftToGiftCollection(Gift, int64) (GiftCollection, error) DeleteGiftFromGiftCollection(int64, int64) (GiftCollection, error) @@ -189,7 +189,7 @@ func (m *PgModel) AllCollections() ([]GiftCollection, error) { return collections, nil } -func (m *PgModel) AllCustomerCollectionsid(id int64) ([]GiftCollection, error) { +func (m *PgModel) AllCustomerCollections(id int64) ([]GiftCollection, error) { collections, err := GetAllCustomerCollectionsFromDB(m.Conn, id) if err != nil { From 291580193f15b39d57bfeb31c7202b5b2fbb887d Mon Sep 17 00:00:00 2001 From: joey-tsai Date: Tue, 7 Nov 2023 09:49:14 -0500 Subject: [PATCH 4/5] Finish ticket --- api/tests/api_test.go | 95 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 75 insertions(+), 20 deletions(-) diff --git a/api/tests/api_test.go b/api/tests/api_test.go index 7cf4238..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) @@ -1119,37 +1148,66 @@ func TestGetAllCustomerGiftCollection(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{}, } collection_three := model.GiftCollection{ - CustomerID: nil, CollectionName: "sample name 3", 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) - err = db.Create(&collection_three).Error + err = tx.Create(&collection_three).Error assert.NoError(t, err) - req1, err := http.NewRequest("GET", fmt.Sprintf("/collections/%d", uintValue), nil) + req1, err := http.NewRequest("GET", fmt.Sprintf("/collections/%d", retrievedCustomer2.ID), nil) router.ServeHTTP(w, req1) assert.Equal(t, 200, w.Code) @@ -1166,7 +1224,4 @@ func TestGetAllCustomerGiftCollection(t *testing.T) { assert.Equal(t, collection_three.CollectionName, collectionRetrieved[1].CollectionName) assert.Equal(t, collection_three.Gifts, collectionRetrieved[1].Gifts) - var collectionCount int64 - collectionCount = int64(len(collectionRetrieved)) - assert.Equal(t, int64(2), collectionCount) } \ No newline at end of file From 5e908441a1ffde5b9f222244ca440107591ee3ac Mon Sep 17 00:00:00 2001 From: joey-tsai Date: Tue, 7 Nov 2023 09:50:39 -0500 Subject: [PATCH 5/5] Finish endpoint --- api/src/controller/controller.go | 4 ++-- api/src/model/transactions.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/api/src/controller/controller.go b/api/src/controller/controller.go index bce685c..6bc7239 100644 --- a/api/src/controller/controller.go +++ b/api/src/controller/controller.go @@ -160,10 +160,10 @@ 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/:id", func(c * gin.Context) { + r.GET("/collections/:customerId", func(c * gin.Context) { // Get Customer ID - id := c.Param("id") + id := c.Param("customerId") intId, err := strconv.Atoi(id) if err != nil { panic(err) diff --git a/api/src/model/transactions.go b/api/src/model/transactions.go index f382620..4c46573 100644 --- a/api/src/model/transactions.go +++ b/api/src/model/transactions.go @@ -235,7 +235,7 @@ func GetAllCollectionsFromDB(db *gorm.DB) ([]GiftCollection, error) { // 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("id = ? OR id IS NULL", id).Preload("Gifts").Find(&collections).Error; err != nil { + 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