From c5b31076185c9652a8ef4cdde800bebd8decad1f Mon Sep 17 00:00:00 2001 From: Derrick Kim Date: Fri, 17 Nov 2023 19:00:20 -0500 Subject: [PATCH] add favorite remove endpoint --- api/src/controller/controller.go | 27 ++++++++++ api/src/model/model.go | 12 +++++ api/src/model/transactions.go | 19 +++++++ api/tests/api_test.go | 91 ++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+) diff --git a/api/src/controller/controller.go b/api/src/controller/controller.go index d3c4999..e9ae055 100644 --- a/api/src/controller/controller.go +++ b/api/src/controller/controller.go @@ -332,6 +332,33 @@ func (pg *PgController) Serve() *gin.Engine { c.JSON(http.StatusOK, giftAddedCollection) }) + r.POST("/removeCustomerGiftCollection/:collectionName/:customerId", func(c *gin.Context) { + var input model.Gift + + collectionName := c.Param("collectionName") + customerId := c.Param("customerId") + + intId, err := strconv.Atoi(customerId) + if err != nil { + panic(err) + } + + if err := c.BindJSON(&input); err != nil { + c.JSON(http.StatusBadRequest, "Failed to unmarshal gift") + fmt.Print(err) + return + } + + giftRemovedCollection, err := pg.DeleteGiftFromCustomerCollection(input, collectionName, int64(intId)) + + if err != nil { + c.JSON(http.StatusBadRequest, input) + panic(err) + } + + c.JSON(http.StatusOK, giftRemovedCollection) + }) + // Delete Gift to Gift Collection r.DELETE("/removeGiftFromGiftCollection/:giftID/:giftCollectionID", func(c *gin.Context) { diff --git a/api/src/model/model.go b/api/src/model/model.go index 29f9edd..480575f 100644 --- a/api/src/model/model.go +++ b/api/src/model/model.go @@ -32,6 +32,7 @@ type Model interface { AddGiftToGiftCollection(Gift, int64) (GiftCollection, error) AddGiftToCustomerCollection(Gift, string, int64) (GiftCollection, error) DeleteGiftFromGiftCollection(int64, int64) (GiftCollection, error) + DeleteGiftFromCustomerCollection(Gift, string, int64) (GiftCollection, error) } func (m *PgModel) AddRequest(inputRequest GiftRequest) (GiftRequest, error) { @@ -240,6 +241,17 @@ func (m *PgModel) AddGiftToCustomerCollection(gift Gift, collectionName string, return giftAddedCollection, nil } +func (m *PgModel) DeleteGiftFromCustomerCollection(gift Gift, collectionName string, customerId int64) (GiftCollection, error) { + + giftDeletedCollection, err := DeleteGiftFromCustomerCollectionFromDB(m.Conn, gift, collectionName, customerId); + + if err != nil { + return GiftCollection{}, err + } + + return giftDeletedCollection, nil +} + func (m *PgModel) DeleteGiftFromGiftCollection(giftID int64, giftCollectionID int64) (GiftCollection, error) { giftDeletedCollection, err := DeleteGiftFromCollectionFromDB(m.Conn, giftID, giftCollectionID) diff --git a/api/src/model/transactions.go b/api/src/model/transactions.go index d848320..838adf1 100644 --- a/api/src/model/transactions.go +++ b/api/src/model/transactions.go @@ -271,6 +271,25 @@ func AddGiftToCustomerCollectionFromDB(db *gorm.DB, gift Gift, collectionName st return collection, nil } +func DeleteGiftFromCustomerCollectionFromDB(db *gorm.DB, gift Gift, collectionName string, customerId int64) (GiftCollection, error) { + var collection GiftCollection + if err := db.Preload("Gifts").Where("collection_name = ? AND customer_id = ?", collectionName, customerId).First(&collection).Error; err != nil { + return GiftCollection{}, err + } + + var giftRemovedCollection []*Gift + for _, collectionGift := range collection.Gifts { + if collectionGift.Name != gift.Name { + giftRemovedCollection = append(giftRemovedCollection, collectionGift) + } + } + if err := db.Model(&collection).Association("Gifts").Replace(giftRemovedCollection); err != nil { + return GiftCollection{}, err + } + + return collection, nil +} + func DeleteGiftFromCollectionFromDB(db *gorm.DB, giftID int64, giftCollectionID int64) (GiftCollection, error) { var collection GiftCollection if err := db.Preload("Gifts").First(&collection, giftCollectionID).Error; err != nil { diff --git a/api/tests/api_test.go b/api/tests/api_test.go index 869db08..8300c79 100644 --- a/api/tests/api_test.go +++ b/api/tests/api_test.go @@ -1309,4 +1309,95 @@ func TestAddGiftToCustomerGiftCollection(t *testing.T) { assert.Equal(t, retrievedCollection.CollectionName, collectionResponse.CollectionName) assert.Equal(t, gift.Name, collectionResponse.Gifts[0].Name) assert.Equal(t, gift.Price, collectionResponse.Gifts[0].Price) +} + +func TestDeleteGiftFromCustomerGiftCollection(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{}, &model.Gift{}, &model.Customer{}, &model.User{}) + 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) + + // Create gifts + giftToRemove := model.Gift{ + Name: "gift to remove", + Price: 25, + } + giftToStay := model.Gift{ + Name: "gift to stay", + Price: 25, + } + giftJSON, err := json.Marshal(giftToRemove) + if err != nil { + t.Fatalf("Error marshaling JSON: %v", err) + } + assert.NoError(t, err) + + // Create a collection + collection := model.GiftCollection{ + CustomerID: &retrievedCustomer.ID, + CollectionName: "test name", + Gifts: []*model.Gift{&giftToRemove, &giftToStay}, + } + + err = tx.Create(&collection).Error + assert.NoError(t, err) + var retrievedCollection model.GiftCollection + err = tx.Preload("Gifts").First(&retrievedCollection).Error; + assert.NoError(t, err) + + req, err := http.NewRequest( + "POST", + fmt.Sprintf("/removeCustomerGiftCollection/%s/%d", retrievedCollection.CollectionName, retrievedCustomer.ID), + bytes.NewBuffer(giftJSON), + ) + router.ServeHTTP(w, req) + assert.Equal(t, 200, w.Code) + + var collectionResponse model.GiftCollection + if e := json.Unmarshal(w.Body.Bytes(), &collectionResponse); e != nil { + t.Fatalf("Error unmarshaling JSON: %v", e) + } + + assert.Equal(t, &retrievedCustomer.ID, collectionResponse.CustomerID) + assert.Equal(t, collection.CollectionName, collectionResponse.CollectionName) + assert.Equal(t, 1, len(collectionResponse.Gifts)) + assert.Equal(t, giftToStay.Name, collectionResponse.Gifts[0].Name) + assert.Equal(t, giftToStay.Price, collectionResponse.Gifts[0].Price) } \ No newline at end of file