Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CC-27: add favorite remove endpoint #69

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions api/src/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,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) {
Expand Down
12 changes: 12 additions & 0 deletions api/src/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}


Expand Down Expand Up @@ -242,6 +243,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)
Expand Down
19 changes: 19 additions & 0 deletions api/src/model/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,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 {
Expand Down
92 changes: 92 additions & 0 deletions api/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,98 @@ func TestAddGiftToCustomerGiftCollection(t *testing.T) {
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)
}

func TestSearchGift(t *testing.T) {
// Database setup
dsn := "user=testuser password=testpwd host=localhost port=5433 dbname=testdb sslmode=disable"
Expand Down
Loading