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 06 additional endpoints gifts collections responses #31

Merged
merged 7 commits into from
Oct 17, 2023
Merged
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
Binary file added api/main
Binary file not shown.
21 changes: 17 additions & 4 deletions api/src/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,20 @@ func (pg *PgController) Serve() *gin.Engine {
}
c.JSON(http.StatusOK, gifts)
})

r.GET("/responses", func(c *gin.Context) {
responses, err := pg.AllGiftResponses()
if err != nil {
c.JSON(http.StatusInternalServerError, "Oops")
}
c.JSON(http.StatusOK, responses)
})
r.GET("/collections", func(c *gin.Context) {
collections, err := pg.AllCollections()
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 Expand Up @@ -137,9 +150,9 @@ func (pg *PgController) Serve() *gin.Engine {
if err != nil {
panic(err)
}

// Get Body Parameters and put in JSON Object
var input model.Gift;
var input model.Gift
if err := c.BindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, "Failed to unmarshal gift")
fmt.Print(err)
Expand All @@ -159,7 +172,7 @@ func (pg *PgController) Serve() *gin.Engine {

// Delete Gift Record based on Gift ID
r.DELETE("/gifts/:id", func(c *gin.Context) {

// Get Gift ID
id := c.Param("id")
intId, err := strconv.Atoi(id)
Expand Down
23 changes: 23 additions & 0 deletions api/src/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type Model interface {
AddGift(Gift) (Gift, error)
UpdateGift(int64, Gift) (Gift, error)
DeleteGift(int64) error
AllGiftResponses() ([]GiftResponse, error)
AllCollections() ([]GiftCollection, error)
}

func (m *PgModel) AddRequest(inputRequest GiftRequest) (GiftRequest, error) {
Expand Down Expand Up @@ -73,6 +75,7 @@ func (m *PgModel) GetGift(id int64) (Gift, error) {
return createdGift, nil
}


func (m *PgModel) GetAllGifts() ([]Gift, error) {

createdGifts, err := GetAllGiftsFromDB(m.Conn)
Expand All @@ -83,6 +86,7 @@ func (m *PgModel) GetAllGifts() ([]Gift, error) {

return createdGifts, nil
}

func (m *PgModel) UpdateGift(id int64, inputGift Gift) (Gift, error) {

updatedGift, err := UpdateGiftToDb(m.Conn, id, inputGift)
Expand All @@ -94,6 +98,15 @@ func (m *PgModel) UpdateGift(id int64, inputGift Gift) (Gift, error) {
return updatedGift, nil
}

func (m *PgModel) AllGiftResponses() ([]GiftResponse, error) {
responses, err := GetAllResponsesFromDB(m.Conn)

if err != nil {
return []GiftResponse{}, err
}
return responses, nil
}

func (m *PgModel) DeleteGift(id int64) error {

err := DeleteGiftFromDb(m.Conn, id)
Expand All @@ -105,6 +118,16 @@ func (m *PgModel) DeleteGift(id int64) error {
return nil
}


func (m *PgModel) AllCollections() ([]GiftCollection, error) {
collections, err := GetAllCollectionsFromDB(m.Conn)

if err != nil {
return []GiftCollection{}, err
}
return collections, nil
}

func (m *PgModel) IncompleteRequests() ([]GiftRequest, error) {
gifts, err := GetIncompleteGiftRequestsFromDB(m.Conn)

Expand Down
20 changes: 19 additions & 1 deletion api/src/model/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,36 @@ func GetGiftFromDB(db *gorm.DB, id int64) (Gift, error) {
}

// GetAllGiftsFromDB fetches all ExampleGift
// GetAllGiftsFromDB fetches all Gift
func GetAllGiftsFromDB(db *gorm.DB) ([]Gift, error) {
var gifts []Gift
if err := db.Find(&gifts).Error; err != nil {
if err := db.Preload("GiftCollections").Find(&gifts).Error; err != nil {
return nil, err
}
return gifts, nil
}

// GetAllResponsesFromDB fetches all GiftResponse
// WriteGiftToDb saves the Gift and returns it
func WriteGiftToDb(db *gorm.DB, inputGift Gift) (Gift, error) {
if err := db.Create(&inputGift).Error; err != nil {
return Gift{}, err
}
return inputGift, nil
}
func GetAllResponsesFromDB(db *gorm.DB) ([]GiftResponse, error) {
var response []GiftResponse
if err := db.Preload("GiftCollection").Find(&response).Error; err != nil {
return nil, err
}
return response, nil
}

// GetAllCollectionsFromDB fetches all GiftCollection
func GetAllCollectionsFromDB(db *gorm.DB) ([]GiftCollection, error) {
var collections []GiftCollection
if err := db.Preload("Gifts").Find(&collections).Error; err != nil {
return nil, err
}
return collections, nil
}
209 changes: 208 additions & 1 deletion api/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ func TestUpdateGift(t *testing.T) {
Demographic: "demogrpahic1",
GiftCollections: nil,
}
err = db.Create(&testGift).Error
err = tx.Create(&testGift).Error
assert.NoError(t, err)

// Test Inputted Gift Fields
Expand Down Expand Up @@ -715,3 +715,210 @@ func TestDeleteGift(t *testing.T) {
tx.Model(&model.Gift{}).Where("id = ?", testGift.ID).Count(&deletedCount)
assert.Equal(t, int64(0), deletedCount)
}


func TestGetAllGift(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.Gift{}, 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()
gift := model.Gift{
Name: "nice sweater",
Price: 50,
Link: "https://something",
Description: "sample description",
Demographic: "sample demographic",
GiftCollections: []*model.GiftCollection{},
}

gift_two := model.Gift{
Name: "nice sweater 2",
Price: 20,
Link: "https://something 2",
Description: "sample description 2",
Demographic: "sample demographic 2",
GiftCollections: []*model.GiftCollection{},
}
err = tx.Create(&gift).Error
assert.NoError(t, err)

err = tx.Create(&gift_two).Error
assert.NoError(t, err)
req1, err := http.NewRequest("GET", fmt.Sprintf("/gifts"), nil)
router.ServeHTTP(w, req1)

assert.Equal(t, 200, w.Code)

var giftRetrieved []model.Gift
if e := json.Unmarshal(w.Body.Bytes(), &giftRetrieved); e != nil {
t.Fatalf("Error unmarshaling JSON: %v", e)
}
t.Log(len(giftRetrieved))
assert.Equal(t, gift.Name, giftRetrieved[0].Name)
assert.Equal(t, gift.Price, giftRetrieved[0].Price)
assert.Equal(t, gift.Link, giftRetrieved[0].Link)
assert.Equal(t, gift.Description, giftRetrieved[0].Description)
assert.Equal(t, gift.Demographic, giftRetrieved[0].Demographic)
assert.Equal(t, gift.GiftCollections, giftRetrieved[0].GiftCollections)

assert.Equal(t, gift_two.Name, giftRetrieved[1].Name)
assert.Equal(t, gift_two.Price, giftRetrieved[1].Price)
assert.Equal(t, gift_two.Link, giftRetrieved[1].Link)
assert.Equal(t, gift_two.Description, giftRetrieved[1].Description)
assert.Equal(t, gift_two.Demographic, giftRetrieved[1].Demographic)
assert.Equal(t, gift_two.GiftCollections, giftRetrieved[1].GiftCollections)
}

func TestGetAllGiftCollection(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{},
}

collection_two := model.GiftCollection{
CustomerID: &uintValue,
CollectionName: "sample name 2",
Gifts: []*model.Gift{},
}

err = db.Create(&collection).Error
assert.NoError(t, err)

err = db.Create(&collection_two).Error
assert.NoError(t, err)

req1, err := http.NewRequest("GET", fmt.Sprintf("/collections"), 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.CustomerID, collectionRetrieved[0].CustomerID)
assert.Equal(t, collection.CollectionName, collectionRetrieved[0].CollectionName)
assert.Equal(t, collection.Gifts, collectionRetrieved[0].Gifts)

assert.Equal(t, collection_two.CustomerID, collectionRetrieved[1].CustomerID)
assert.Equal(t, collection_two.CollectionName, collectionRetrieved[1].CollectionName)
assert.Equal(t, collection_two.Gifts, collectionRetrieved[1].Gifts)
}

func TestGetAllGiftResponse(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.GiftResponse{}, 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()

collection := model.GiftCollection{}
err = tx.Create(&collection).Error

response := model.GiftResponse{
GiftCollection: collection,
CustomMessage: "sample custom message",
}


response_two := model.GiftResponse{
GiftCollection: collection,
CustomMessage: "sample custom message 2",
}

err = tx.Create(&response).Error
assert.NoError(t, err)

err = tx.Create(&response_two).Error
assert.NoError(t, err)
req1, err := http.NewRequest("GET", fmt.Sprintf("/responses"), nil)
router.ServeHTTP(w, req1)

assert.Equal(t, 200, w.Code)

var responseRetrieved []model.GiftResponse
if e := json.Unmarshal(w.Body.Bytes(), &responseRetrieved); e != nil {
t.Fatalf("Error unmarshaling JSON: %v", e)
}

assert.Equal(t, response.GiftCollection.ID, responseRetrieved[0].GiftCollection.ID)
assert.Equal(t, response.GiftCollectionID, responseRetrieved[0].GiftCollectionID)
assert.Equal(t, response.CustomMessage, responseRetrieved[0].CustomMessage)

assert.Equal(t, response_two.GiftCollection.ID, responseRetrieved[1].GiftCollection.ID)
assert.Equal(t, response_two.GiftCollectionID, responseRetrieved[1].GiftCollectionID)
assert.Equal(t, response_two.CustomMessage, responseRetrieved[1].CustomMessage)

}

Loading