Skip to content

Commit

Permalink
Merge pull request #50 from GenerateNU/cc-20-default-and-user-collect…
Browse files Browse the repository at this point in the history
…ion-endpoint

Cc 20 default and user collection endpoint
  • Loading branch information
matherg authored Nov 7, 2023
2 parents e20f0e4 + 5e90844 commit 9452099
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 8 deletions.
16 changes: 16 additions & 0 deletions api/src/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions api/src/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
9 changes: 9 additions & 0 deletions api/src/model/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
150 changes: 142 additions & 8 deletions api/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)

}

0 comments on commit 9452099

Please sign in to comment.