Skip to content

Commit

Permalink
Merge pull request #43 from GenerateNU/cc-07-searchbar-backend
Browse files Browse the repository at this point in the history
Cc 07 searchbar backend
  • Loading branch information
matherg authored Nov 17, 2023
2 parents 110ee34 + 596f409 commit d2cdf6e
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 25 deletions.
36 changes: 23 additions & 13 deletions api/src/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,29 @@ func (pg *PgController) Serve() *gin.Engine {
}
c.JSON(http.StatusOK, responses)
})
r.GET("/search/:giftCollectionId", func(c *gin.Context) {
searchTerm := c.Query("q")
minPriceStr := c.Query("minPrice")
maxPriceStr := c.Query("maxPrice")
occasion := c.Query("occasion")
demographic := c.Query("demographic")
category := c.QueryArray("category")

id := c.Param("giftCollectionId")
intId, err := strconv.Atoi(id)
if err != nil {
c.JSON(http.StatusBadRequest, "Invalid giftCollectionId")
return
}

minPrice, _ := strconv.Atoi(minPriceStr)
maxPrice, _ := strconv.Atoi(maxPriceStr)
gifts, err := pg.SearchGifts(int64(intId), searchTerm, minPrice, maxPrice, occasion, demographic, category)
if err != nil {
c.JSON(http.StatusInternalServerError, "Oops")
}
c.JSON(http.StatusOK, gifts)
})
r.GET("/collections", func(c *gin.Context) {
collections, err := pg.AllCollections()
if err != nil {
Expand Down Expand Up @@ -195,19 +218,6 @@ func (pg *PgController) Serve() *gin.Engine {

c.JSON(http.StatusOK, insertedGift)
})
r.GET("/search", func(c *gin.Context) {
searchTerm := c.Query("q")
minPriceStr := c.Query("minPrice")
maxPriceStr := c.Query("maxPrice")

minPrice, _ := strconv.Atoi(minPriceStr)
maxPrice, _ := strconv.Atoi(maxPriceStr)
gifts, err := pg.SearchGifts(searchTerm, minPrice, maxPrice)
if err != nil {
c.JSON(http.StatusInternalServerError, "Oops")
}
c.JSON(http.StatusOK, gifts)
})
// Update Gift Record Given Gift ID
r.PUT("/gifts/:id", func(c *gin.Context) {

Expand Down
1 change: 1 addition & 0 deletions api/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func main() {
err = db.Create(&randomGift2).Error

// Check for errors

if err != nil {
fmt.Println("Error auto-migrating:", err)
return
Expand Down
8 changes: 5 additions & 3 deletions api/src/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Model interface {
UpdateGift(int64, Gift) (Gift, error)
DeleteGift(int64) error
DeleteGiftCollection(int64) error
SearchGifts(string, int, int) ([]Gift, error)
SearchGifts(int64, string, int, int, string, string, []string) ([]Gift, error)
AllGiftResponses() ([]GiftResponse, error)
AllCollections() ([]GiftCollection, error)
AllCustomerCollections(id int64) ([]GiftCollection, error)
Expand All @@ -34,6 +34,7 @@ type Model interface {
DeleteGiftFromGiftCollection(int64, int64) (GiftCollection, error)
}


func (m *PgModel) AddRequest(inputRequest GiftRequest) (GiftRequest, error) {

createdRequest, err := WriteRequestToDb(m.Conn, inputRequest)
Expand Down Expand Up @@ -148,10 +149,11 @@ func (m *PgModel) DeleteGift(id int64) error {

return nil
}
func (m *PgModel) SearchGifts(searchTerm string, minPrice int, maxPrice int) ([]Gift, error) {
func (m *PgModel) SearchGifts(id int64, searchTerm string, minPrice int, maxPrice int, occasion string, demographic string, category []string) ([]Gift, error) {
var gifts []Gift
searchTerm = strings.TrimSpace(searchTerm)


// Convert to lowercase
searchTerm = strings.ToLower(searchTerm)

Expand All @@ -164,7 +166,7 @@ func (m *PgModel) SearchGifts(searchTerm string, minPrice int, maxPrice int) ([]
searchTerms[i] = term + ":*"
}
formattedSearchTerm := strings.Join(searchTerms, " | ")
gifts, err := SearchGiftsDb(m.Conn, formattedSearchTerm, minPrice, maxPrice)
gifts, err := SearchGiftsDb(m.Conn, id, formattedSearchTerm, minPrice, maxPrice, occasion, demographic, category)

if err != nil {
return nil, err
Expand Down
36 changes: 32 additions & 4 deletions api/src/model/transactions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

import (
"github.com/lib/pq"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -171,17 +172,44 @@ func DeleteGiftCollectionFromDb(db *gorm.DB, id int64) error {

return nil
}
func SearchGiftsDb(db *gorm.DB, searchTerm string, minPrice int, maxPrice int) ([]Gift, error) {
func SearchGiftsDb(db *gorm.DB, id int64, searchTerm string, minPrice int, maxPrice int, occasion string, demographic string, category []string) ([]Gift, error) {
var gifts []Gift

if err := db.Where("to_tsvector('english', name || ' ' || description) @@ to_tsquery('english', ?)", searchTerm).
Where("price >= ? AND price <= ?", minPrice, maxPrice).
Find(&gifts).Error; err != nil {
query := db.Joins("JOIN gift_collection_gifts ON gifts.id = gift_collection_gifts.gift_id").
Where("gift_collection_gifts.gift_collection_id = ?", id)

if minPrice >= 0 {
query = query.Where("price >= ?", minPrice)
}

if searchTerm != "" {
query = query.Where("to_tsvector('english', name || ' ' || description) @@ to_tsquery('english', ?)", searchTerm)
}

if maxPrice > 0 {
query = query.Where("price <= ?", maxPrice)
}

if occasion != "" {
query = query.Where("occasion = ?", occasion)
}

if demographic != "" {
query = query.Where("demographic = ?", demographic)
}

if len(category) > 0 {
query = query.Where("category && ?", pq.StringArray(category))
}
query = query.Preload("GiftCollections")

if err := query.Find(&gifts).Error; err != nil {
return nil, err
}

return gifts, nil
}

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 {
Expand Down
4 changes: 2 additions & 2 deletions api/src/model/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Gift struct {
Demographic string
Category pq.StringArray `gorm:"type:text[]"`
Occasion string
GiftCollections []*GiftCollection `gorm:"many2many:gift_request_gifts;"`
GiftCollections []*GiftCollection `gorm:"many2many:gift_collection_gifts;"`
}

type GiftRequest struct {
Expand All @@ -38,7 +38,7 @@ type GiftCollection struct {
CustomerID *uint
Customer *Customer
CollectionName string
Gifts []*Gift `gorm:"many2many:gift_request_gifts;"`
Gifts []*Gift `gorm:"many2many:gift_collection_gifts;"`
}

type GiftResponse struct {
Expand Down
Loading

0 comments on commit d2cdf6e

Please sign in to comment.