Skip to content

Commit

Permalink
Merge pull request #34 from GenerateNU/cc-14-additional-admin-endpoints
Browse files Browse the repository at this point in the history
Cc 14 additional admin endpoints
  • Loading branch information
matherg authored Oct 18, 2023
2 parents 86d1df1 + 0bfa451 commit 3577aaf
Show file tree
Hide file tree
Showing 4 changed files with 314 additions and 31 deletions.
52 changes: 52 additions & 0 deletions api/src/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,57 @@ func (pg *PgController) Serve() *gin.Engine {
c.JSON(http.StatusNoContent, "Deleted Gift")
})

// Add Gift to Gift Collection
r.POST("/addGiftCollection/:id", func(c *gin.Context) {
var input model.Gift

// Get Gift Collection Id
id := c.Param("id")
intId, err := strconv.Atoi(id)
if err != nil {
panic(err)
}

if err := c.BindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, "Failed to unmarshal collection")
fmt.Print(err)
return
}

giftAddedCollection, err := pg.AddGiftToGiftCollection(input, int64(intId))

if err != nil {
c.JSON(http.StatusBadRequest, input)
panic(err)
}

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

// Delete Gift to Gift Collection
r.DELETE("/removeGiftFromGiftCollection/:giftID/:giftCollectionID", func(c *gin.Context) {
var input model.Gift

// Get Gift Collection Id
collectionID, err := strconv.Atoi(c.Param("giftCollectionID"))
if err != nil {
panic(err)
}

giftID, err := strconv.Atoi(c.Param("giftID"))
if err != nil {
panic(err)
}

giftRemovedCollection, err := pg.DeleteGiftFromGiftCollection(int64(giftID), int64(collectionID))

if err != nil {
c.JSON(http.StatusBadRequest, input)
panic(err)
}

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

return r
}
25 changes: 23 additions & 2 deletions api/src/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ type Model interface {
AddCollection(GiftCollection) (GiftCollection, error)
IncompleteRequests() ([]GiftRequest, error)
CompleteRequests() ([]GiftRequest, error)

GetGift(int64) (Gift, error)
GetAllGifts() ([]Gift, error)
AddGift(Gift) (Gift, error)
UpdateGift(int64, Gift) (Gift, error)
DeleteGift(int64) error
AllGiftResponses() ([]GiftResponse, error)
AllCollections() ([]GiftCollection, error)
AddGiftToGiftCollection(Gift, int64) (GiftCollection, error)
DeleteGiftFromGiftCollection(int64, int64) (GiftCollection, error)
}

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


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

createdGifts, err := GetAllGiftsFromDB(m.Conn)
Expand Down Expand Up @@ -118,7 +120,6 @@ func (m *PgModel) DeleteGift(id int64) error {
return nil
}


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

Expand Down Expand Up @@ -146,4 +147,24 @@ func (m *PgModel) CompleteRequests() ([]GiftRequest, error) {
return gifts, nil
}

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

giftAddedCollection, err := AddGiftToCollectionFromDB(m.Conn, inputGift, id)

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

return giftAddedCollection, nil
}

func (m *PgModel) DeleteGiftFromGiftCollection(giftID int64, giftCollectionID int64) (GiftCollection, error) {

giftDeletedCollection, err := DeleteGiftFromCollectionFromDB(m.Conn, giftID, giftCollectionID)

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

return giftDeletedCollection, nil
}
62 changes: 55 additions & 7 deletions api/src/model/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,26 @@ func UpdateGiftToDb(db *gorm.DB, id int64, inputGift Gift) (Gift, error) {
}

// Update Gift Record
updates := map[string]interface{}{
"Name": inputGift.Name,
"Price": inputGift.Price,
"Link": inputGift.Link,
"Description": inputGift.Description,
"Demographic": inputGift.Demographic,
"GiftCollections": inputGift.GiftCollections,
updates := make(map[string]interface{})

// Check each field in inputGift and add it to the updates map if it is non-zero
if inputGift.Name != "" {
updates["Name"] = inputGift.Name
}
if inputGift.Price != 0 {
updates["Price"] = inputGift.Price
}
if inputGift.Link != "" {
updates["Link"] = inputGift.Link
}
if inputGift.Description != "" {
updates["Description"] = inputGift.Description
}
if inputGift.Demographic != "" {
updates["Demographic"] = inputGift.Demographic
}
if inputGift.GiftCollections != nil && len(inputGift.GiftCollections) > 0 {
updates["GiftCollections"] = inputGift.GiftCollections
}

if err := db.Model(&updatedGift).Updates(updates).Error; err != nil {
Expand Down Expand Up @@ -122,3 +135,38 @@ func GetAllCollectionsFromDB(db *gorm.DB) ([]GiftCollection, error) {
}
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 {
return GiftCollection{}, err
}

collection.Gifts = append(collection.Gifts, &inputGift)

if err := db.Save(&collection).Error; 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 {
return GiftCollection{}, err
}

// Create a new GiftCollection array without the inputGift
var giftRemovedCollection []*Gift
for _, gift := range collection.Gifts {
if gift.ID != uint(giftID) {
giftRemovedCollection = append(giftRemovedCollection, gift)
}
}
if err := db.Model(&collection).Association("Gifts").Replace(giftRemovedCollection); err != nil {
return GiftCollection{}, err
}

return collection, nil
}
Loading

0 comments on commit 3577aaf

Please sign in to comment.