Skip to content

Commit

Permalink
Merge pull request #60 from GenerateNU/cc-21-giftrequestamountendpoints
Browse files Browse the repository at this point in the history
Cc 21 giftrequestamountendpoints
  • Loading branch information
matherg authored Nov 20, 2023
2 parents dd0f0d9 + 10a0000 commit ca31703
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 8 deletions.
26 changes: 25 additions & 1 deletion api/src/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ func (pg *PgController) Serve() *gin.Engine {
}
c.JSON(http.StatusOK, gift)
})

r.GET("/gifts", func(c *gin.Context) {
gifts, err := pg.GetAllGifts()
if err != nil {
Expand Down Expand Up @@ -395,5 +394,30 @@ func (pg *PgController) Serve() *gin.Engine {
c.JSON(http.StatusOK, giftRemovedCollection)
})

// Update AvailableRequests
r.PUT("customer/:id", func(c *gin.Context) {

// Get Customer ID
customerID, err := strconv.Atoi(c.Param("id"))
if err != nil {
panic(err)
}

// Get request amount
updatedRequests := c.Query("requests")
requests, err := strconv.Atoi(updatedRequests)
if err != nil {
panic(err)
}

updatedCustomerRequests, err := pg.UpdateCustomerAvailableRequests(int64(customerID), int64(requests))
if err != nil {
c.JSON(http.StatusBadRequest, "Failed to Update AvailableRequests")
panic(err)
}

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

return r
}
15 changes: 13 additions & 2 deletions api/src/model/model.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package model

import (
"gorm.io/gorm"
"regexp"
"strings"

"gorm.io/gorm"
)

type PgModel struct {
Expand Down Expand Up @@ -33,6 +32,7 @@ type Model interface {
AddGiftToCustomerCollection(Gift, string, int64) (GiftCollection, error)
DeleteGiftFromGiftCollection(int64, int64) (GiftCollection, error)
DeleteGiftFromCustomerCollection(Gift, string, int64) (GiftCollection, error)
UpdateCustomerAvailableRequests(int64, int64) (Customer, error)
}


Expand Down Expand Up @@ -264,3 +264,14 @@ func (m *PgModel) DeleteGiftFromGiftCollection(giftID int64, giftCollectionID in

return giftDeletedCollection, nil
}

func (m *PgModel) UpdateCustomerAvailableRequests(customerID int64, availableRequests int64) (Customer, error) {

updatedCustomer, err := UpdateCustomerAvailableRequestsFromDB(m.Conn, customerID, availableRequests)

if err != nil {
return Customer{}, err
}

return updatedCustomer, nil
}
22 changes: 22 additions & 0 deletions api/src/model/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model
import (
"github.com/lib/pq"
"gorm.io/gorm"
"errors"
)

func WriteRequestToDb(db *gorm.DB, inputRequest GiftRequest) (GiftRequest, error) {
Expand Down Expand Up @@ -337,3 +338,24 @@ func DeleteGiftFromCollectionFromDB(db *gorm.DB, giftID int64, giftCollectionID

return collection, nil
}

// Update Available Requests for Customers
func UpdateCustomerAvailableRequestsFromDB(db *gorm.DB, customerID int64, availableRequests int64) (Customer, error) {
var customer Customer
if err := db.First(&customer, customerID).Error; err != nil {
return Customer{}, err
}

updatedAvailableRequests := int64(customer.AvailableRequests) + availableRequests
if updatedAvailableRequests < 0 {
return Customer{}, errors.New("Customer doesn't have any AvailableRequests")
}

customer.AvailableRequests = uint(updatedAvailableRequests)

if err:= db.Save(&customer).Error; err!= nil {
return Customer{}, err
}

return customer, nil
}
9 changes: 5 additions & 4 deletions api/src/model/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ type User struct {

type Customer struct {
gorm.Model
UserID uint
User User
GiftCollections []*GiftCollection
GiftRequests []*GiftRequest
UserID uint
User User
AvailableRequests uint
GiftCollections []*GiftCollection
GiftRequests []*GiftRequest
}

type Admin struct {
Expand Down
76 changes: 75 additions & 1 deletion api/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,81 @@ func TestGetAllCustomerGiftCollection(t *testing.T) {
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)
}

func TestUpdateCustomerAvailableRequests(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.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
w1 := httptest.NewRecorder()
w2 := 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,
AvailableRequests: uint(10),
}
err = tx.Create(&customer).Error
assert.NoError(t, err)
var retrievedCustomer model.Customer
err = tx.First(&retrievedCustomer).Error
assert.NoError(t, err)

req1, err := http.NewRequest("PUT", fmt.Sprintf("/customer/%d?requests=5", retrievedCustomer.ID), nil)
router.ServeHTTP(w1, req1)
assert.Equal(t, 200, w1.Code)

var customerRetrieved model.Customer
if e := json.Unmarshal(w1.Body.Bytes(), &customerRetrieved); e != nil {
t.Fatalf("Error unmarshaling JSON: %v", e)
}

assert.Equal(t, retrievedCustomer.ID, customerRetrieved.ID)
assert.Equal(t, retrievedCustomer.UserID, customerRetrieved.UserID)
assert.Equal(t, retrievedCustomer.User, customerRetrieved.User)
assert.Equal(t, uint(15), customerRetrieved.AvailableRequests)

req2, err := http.NewRequest("PUT", fmt.Sprintf("/customer/%d?requests=-1", retrievedCustomer.ID), nil)
router.ServeHTTP(w2, req2)
assert.Equal(t, 200, w2.Code)

var customerRetrieved2 model.Customer
if e := json.Unmarshal(w2.Body.Bytes(), &customerRetrieved2); e != nil {
t.Fatalf("Error unmarshaling JSON: %v", e)
}

assert.Equal(t, retrievedCustomer.ID, customerRetrieved2.ID)
assert.Equal(t, retrievedCustomer.UserID, customerRetrieved2.UserID)
assert.Equal(t, retrievedCustomer.User, customerRetrieved2.User)
assert.Equal(t, uint(14), customerRetrieved2.AvailableRequests)
}

func TestAddGiftToCustomerGiftCollection(t *testing.T) {
Expand Down Expand Up @@ -1294,7 +1368,7 @@ func TestAddGiftToCustomerGiftCollection(t *testing.T) {

req, err := http.NewRequest(
"POST",
fmt.Sprintf("/addCustomerGiftCollection/%s/%d", retrievedCollection.CollectionName, retrievedCustomer.ID),
fmt.Sprintf("/addCustomerGiftCollection/%s/%d", retrievedCollection.CollectionName, retrievedCustomer.ID),
bytes.NewBuffer(giftJSON),
)
router.ServeHTTP(w, req)
Expand Down

0 comments on commit ca31703

Please sign in to comment.