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 31 add giftee to giftrequest #81

Merged
merged 8 commits into from
Dec 3, 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
18 changes: 15 additions & 3 deletions api/src/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (pg *PgController) Serve() *gin.Engine {
c.JSON(http.StatusOK, gifts)
})

// Add a Gift Response
r.POST("/addGiftResponse", func(c *gin.Context) {
var input model.GiftResponse
if err := c.BindJSON(&input); err != nil {
Expand All @@ -58,6 +59,7 @@ func (pg *PgController) Serve() *gin.Engine {

c.JSON(http.StatusOK, insertedResponse)
})
// Update the Gift Request
r.PUT("/requests", func(c *gin.Context) {
// Get Body Parameters and put in JSON Object
var input model.GiftRequest
Expand All @@ -77,6 +79,7 @@ func (pg *PgController) Serve() *gin.Engine {

c.JSON(http.StatusOK, updatedGiftRequest)
})
// Create a new Gift Request
r.POST("/addGiftRequest", func(c *gin.Context) {
var input model.GiftRequest
if err := c.BindJSON(&input); err != nil {
Expand All @@ -93,6 +96,7 @@ func (pg *PgController) Serve() *gin.Engine {

c.JSON(http.StatusOK, insertedRequest)
})
// Create a new Gift Collection
r.POST("/addGiftCollection", func(c *gin.Context) {
var input model.GiftCollection
if err := c.BindJSON(&input); err != nil {
Expand All @@ -111,6 +115,7 @@ func (pg *PgController) Serve() *gin.Engine {

c.JSON(http.StatusOK, insertedCollection)
})
// Update the Gift Collection
r.PUT("/updateGiftCollection", func(c *gin.Context) {
var input model.GiftCollection
if err := c.BindJSON(&input); err != nil {
Expand All @@ -129,6 +134,7 @@ func (pg *PgController) Serve() *gin.Engine {

c.JSON(http.StatusOK, updatedCollection)
})
// Get the Gift given the Gift ID
r.GET("/gifts/:id", func(c *gin.Context) {
id := c.Param("id")
intId, err := strconv.Atoi(id)
Expand All @@ -138,20 +144,23 @@ func (pg *PgController) Serve() *gin.Engine {
}
c.JSON(http.StatusOK, gift)
})
// Get all Gifts
r.GET("/gifts", func(c *gin.Context) {
gifts, err := pg.GetAllGifts()
if err != nil {
c.JSON(http.StatusInternalServerError, "Oops")
}
c.JSON(http.StatusOK, gifts)
})
// Get all Gift Responses
r.GET("/responses", func(c *gin.Context) {
responses, err := pg.AllGiftResponses()
if err != nil {
c.JSON(http.StatusInternalServerError, "Oops")
}
c.JSON(http.StatusOK, responses)
})
// Get all the Gifts in a Gift Collection given filter options
r.GET("/search/:giftCollectionId", func(c *gin.Context) {
searchTerm := c.Query("q")
minPriceStr := c.Query("minPrice")
Expand All @@ -175,6 +184,7 @@ func (pg *PgController) Serve() *gin.Engine {
}
c.JSON(http.StatusOK, gifts)
})
// Get all Gift Collections
r.GET("/collections", func(c *gin.Context) {
collections, err := pg.AllCollections()
if err != nil {
Expand All @@ -198,6 +208,7 @@ func (pg *PgController) Serve() *gin.Engine {
}
c.JSON(http.StatusOK, collections)
})
// Create a new Gift
r.POST("/addGift", func(c *gin.Context) {
var input model.Gift
fmt.Print(c)
Expand Down Expand Up @@ -266,6 +277,7 @@ func (pg *PgController) Serve() *gin.Engine {
}
c.JSON(http.StatusNoContent, "Deleted Gift")
})
// Delete Gift Collection based on Gift Collection ID
r.DELETE("/deleteGiftCollection/:id", func(c *gin.Context) {

// Get GiftCollection ID
Expand Down Expand Up @@ -340,7 +352,7 @@ func (pg *PgController) Serve() *gin.Engine {

c.JSON(http.StatusOK, giftAddedCollection)
})

// Remove a Customer from a Gift Collection given Gift Collection Name and Customer ID
r.POST("/removeCustomerGiftCollection/:collectionName/:customerId", func(c *gin.Context) {
var input model.Gift

Expand Down Expand Up @@ -458,7 +470,7 @@ func (pg *PgController) Serve() *gin.Engine {
c.JSON(http.StatusOK, updatedGiftee)
})

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

// Get Giftee ID
Expand All @@ -478,7 +490,7 @@ func (pg *PgController) Serve() *gin.Engine {
}
c.JSON(http.StatusNoContent, "Deleted Giftee")
})
// Update AvailableRequests
// Update AvailableRequests based on Customer ID
r.PUT("customer/:id", func(c *gin.Context) {

// Get Customer ID
Expand Down
7 changes: 5 additions & 2 deletions api/src/model/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func UpdateGiftRequestToDb(db *gorm.DB, inputRequest GiftRequest) (GiftRequest,
if inputRequest.GiftResponseID != nil {
updates["GiftResponseID"] = inputRequest.GiftResponseID
}
if inputRequest.GifteeID != 0 {
updates["GifteeID"] = inputRequest.GifteeID
}

if err := db.Model(&updatedGiftRequest).Updates(updates).Error; err != nil {
return GiftRequest{}, err
Expand Down Expand Up @@ -95,7 +98,7 @@ func UpdateCollectionToDb(db *gorm.DB, inputCollection GiftCollection) (GiftColl
}
func GetIncompleteGiftRequestsFromDB(db *gorm.DB) ([]GiftRequest, error) {
var requests []GiftRequest
if err := db.Where("gift_response_id IS NULL").Preload("GiftResponse").Find(&requests).Error; err != nil {
if err := db.Where("gift_response_id IS NULL").Preload("GiftResponse").Preload("Giftee").Find(&requests).Error; err != nil {
return nil, err
}
return requests, nil
Expand Down Expand Up @@ -212,7 +215,7 @@ func SearchGiftsDb(db *gorm.DB, id int64, searchTerm string, minPrice int, maxPr

func GetCompleteGiftRequestsFromDB(db *gorm.DB) ([]GiftRequest, error) {
var requests []GiftRequest
if err := db.Where("gift_response_id IS NOT NULL").Preload("GiftResponse").Preload("GiftResponse.GiftCollection").Find(&requests).Error; err != nil {
if err := db.Where("gift_response_id IS NOT NULL").Preload("GiftResponse").Preload("GiftResponse.GiftCollection").Preload("Giftee").Find(&requests).Error; err != nil {
return nil, err
}
return requests, nil
Expand Down
2 changes: 2 additions & 0 deletions api/src/model/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type GiftRequest struct {
gorm.Model
CustomerID uint
GiftResponseID *uint
GifteeID uint
RecipientName string
RecipientAge uint
Occasion pq.StringArray `gorm:"type:text[]"`
Expand Down Expand Up @@ -75,6 +76,7 @@ type Giftee struct {
Age uint
Colors pq.StringArray `gorm:"type:text[]"`
Interests pq.StringArray `gorm:"type:text[]"`
GiftRequests []*GiftRequest
}

type Admin struct {
Expand Down
57 changes: 48 additions & 9 deletions api/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1701,7 +1701,7 @@ func TestGetGiftee(t *testing.T) {
t.Fatalf("Unable to connect to database: %v", err)
}
// Put auto migrations here
err = db.AutoMigrate(&model.Giftee{})
err = db.AutoMigrate(&model.Giftee{}, &model.User{}, &model.Customer{})
if err != nil {
panic("failed to migrate test database schema")
}
Expand All @@ -1717,9 +1717,18 @@ func TestGetGiftee(t *testing.T) {
// Test code
w := httptest.NewRecorder()

// Create User
user := model.User{Email: "[email protected]", FirstName: "PersonFirstName", LastName: "PersonLastName", Password: "dgeeg32"}
err = tx.Create(&user).Error

// Create Customer
customer := model.Customer{User: user, UserID: user.ID}
err = tx.Create(&customer).Error
assert.NoError(t, err)

// Create Giftee
testGiftee := model.Giftee {
CustomerID: 1,
CustomerID: customer.ID,
GifteeName: "Maya",
Gender: "Female",
CustomerRelationship: "Sister",
Expand Down Expand Up @@ -1755,6 +1764,7 @@ func TestGetGiftee(t *testing.T) {
assert.Equal(t, retrievedGiftee.Age, fetchedGiftee.Age)
assert.Equal(t, retrievedGiftee.Colors, fetchedGiftee.Colors)
assert.Equal(t, retrievedGiftee.Interests, fetchedGiftee.Interests)
assert.Equal(t, retrievedGiftee.GiftRequests, fetchedGiftee.GiftRequests)
assert.Equal(t, retrievedGiftee.CreatedAt.In(time.UTC).Round(time.Millisecond),
fetchedGiftee.CreatedAt.In(time.UTC).Round(time.Millisecond))
}
Expand All @@ -1770,7 +1780,7 @@ func TestAddGiftee(t *testing.T) {
t.Fatalf("Unable to connect to database: %v", err)
}
// Put auto migrations here
err = db.AutoMigrate(&model.Giftee{})
err = db.AutoMigrate(&model.Giftee{}, &model.User{}, &model.Customer{})
if err != nil {
panic("failed to migrate test database schema")
}
Expand All @@ -1786,9 +1796,18 @@ func TestAddGiftee(t *testing.T) {
// Test code
w := httptest.NewRecorder()

// Create User
user := model.User{Email: "[email protected]", FirstName: "PersonFirstName", LastName: "PersonLastName", Password: "dgeeg32"}
err = tx.Create(&user).Error

// Create Customer
customer := model.Customer{User: user, UserID: user.ID}
err = tx.Create(&customer).Error
assert.NoError(t, err)

// Create Giftee
testGiftee := model.Giftee {
CustomerID: 1,
CustomerID: customer.ID,
GifteeName: "Maya",
Gender: "Female",
CustomerRelationship: "Sister",
Expand Down Expand Up @@ -1848,7 +1867,7 @@ func TestUpdateGiftee(t *testing.T) {
t.Fatalf("Unable to connect to database: %v", err)
}
// Put auto migrations here
err = db.AutoMigrate(&model.Giftee{})
err = db.AutoMigrate(&model.Giftee{}, &model.User{}, &model.Customer{})
if err != nil {
panic("failed to migrate test database schema")
}
Expand All @@ -1864,9 +1883,18 @@ func TestUpdateGiftee(t *testing.T) {
// Test code
w := httptest.NewRecorder()

// Create User
user := model.User{Email: "[email protected]", FirstName: "PersonFirstName", LastName: "PersonLastName", Password: "dgeeg32"}
err = tx.Create(&user).Error

// Create Customer
customer := model.Customer{User: user, UserID: user.ID}
err = tx.Create(&customer).Error
assert.NoError(t, err)

// Create Giftee
testGiftee := model.Giftee {
CustomerID: 1,
CustomerID: customer.ID,
GifteeName: "Maya",
Gender: "Female",
CustomerRelationship: "Sister",
Expand All @@ -1889,12 +1917,12 @@ func TestUpdateGiftee(t *testing.T) {
assert.Equal(t, testGiftee.Age, fetchedGiftee.Age)
assert.Equal(t, testGiftee.Colors, fetchedGiftee.Colors)
assert.Equal(t, testGiftee.Interests, fetchedGiftee.Interests)
assert.Equal(t, testGiftee.GiftRequests, fetchedGiftee.GiftRequests)
assert.Equal(t, testGiftee.CreatedAt.In(time.UTC).Round(time.Millisecond),
fetchedGiftee.CreatedAt.In(time.UTC).Round(time.Millisecond))

// Updated Giftee Fields
updatedTestGiftee := model.Giftee {
CustomerID: 1,
GifteeName: "Maya Updated",
Gender: "Female",
CustomerRelationship: "Sister",
Expand Down Expand Up @@ -1934,6 +1962,7 @@ func TestUpdateGiftee(t *testing.T) {
assert.Equal(t, fetchedUpdatedGiftee.Age, updatedGifteeRetrieved.Age)
assert.Equal(t, fetchedUpdatedGiftee.Colors, updatedGifteeRetrieved.Colors)
assert.Equal(t, fetchedUpdatedGiftee.Interests, updatedGifteeRetrieved.Interests)
assert.Equal(t, fetchedUpdatedGiftee.GiftRequests, updatedGifteeRetrieved.GiftRequests)
assert.Equal(t, fetchedUpdatedGiftee.CreatedAt.In(time.UTC).Round(time.Millisecond),
updatedGifteeRetrieved.CreatedAt.In(time.UTC).Round(time.Millisecond))
}
Expand All @@ -1949,7 +1978,7 @@ func TestDeleteGiftee(t *testing.T) {
t.Fatalf("Unable to connect to database: %v", err)
}
// Put auto migrations here
err = db.AutoMigrate(&model.Giftee{})
err = db.AutoMigrate(&model.Giftee{}, &model.User{}, &model.Customer{})
if err != nil {
panic("failed to migrate test database schema")
}
Expand All @@ -1965,9 +1994,18 @@ func TestDeleteGiftee(t *testing.T) {
// Test code
w := httptest.NewRecorder()

// Create User
user := model.User{Email: "[email protected]", FirstName: "PersonFirstName", LastName: "PersonLastName", Password: "dgeeg32"}
err = tx.Create(&user).Error

// Create Customer
customer := model.Customer{User: user, UserID: user.ID}
err = tx.Create(&customer).Error
assert.NoError(t, err)

// Create Giftee
testGiftee := model.Giftee {
CustomerID: 1,
CustomerID: customer.ID,
GifteeName: "Maya",
Gender: "Female",
CustomerRelationship: "Sister",
Expand All @@ -1990,6 +2028,7 @@ func TestDeleteGiftee(t *testing.T) {
assert.Equal(t, testGiftee.Age, fetchedGiftee.Age)
assert.Equal(t, testGiftee.Colors, fetchedGiftee.Colors)
assert.Equal(t, testGiftee.Interests, fetchedGiftee.Interests)
assert.Equal(t, testGiftee.GiftRequests, fetchedGiftee.GiftRequests)
assert.Equal(t, testGiftee.CreatedAt.In(time.UTC).Round(time.Millisecond),
fetchedGiftee.CreatedAt.In(time.UTC).Round(time.Millisecond))

Expand Down
Loading
Loading