Skip to content

Commit

Permalink
Merge pull request #21 from GenerateNU/cc-01-gift-models
Browse files Browse the repository at this point in the history
Cc 01 gift models
  • Loading branch information
matherg authored Oct 6, 2023
2 parents 3db323f + cfb58c2 commit f8ca960
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 96 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cSpell.words": ["gorm", "timestamppb"]
}
2 changes: 1 addition & 1 deletion api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21.1
require (
github.com/gin-contrib/cors v1.4.0
github.com/gin-gonic/gin v1.9.1
github.com/go-playground/assert/v2 v2.2.0
github.com/lib/pq v1.10.9
github.com/stretchr/testify v1.8.3
gorm.io/driver/postgres v1.5.2
gorm.io/gorm v1.25.4
Expand Down
2 changes: 1 addition & 1 deletion api/src/model/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ func GetAllExampleGiftsFromDB(db *gorm.DB) ([]ExampleGift, error) {
return nil, err
}
return gifts, nil
}
}
71 changes: 47 additions & 24 deletions api/src/model/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package model

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

type ExampleGift struct {
gorm.Model
Expand All @@ -13,15 +17,46 @@ type ExampleGiftInput struct {
Price int
}

type User struct {
type Gift struct {
gorm.Model
Email string
FirstName string
LastName string
Password string
Name string
Price float64
Link string
Description string
Demographic string
GiftCollections []*GiftCollection `gorm:"many2many:gift_request_gifts;"`
}

type GiftRequest struct {
gorm.Model
CustomerID uint
GiftResponseID *uint
RecipientName string
RecipientAge uint
Occasion pq.StringArray `gorm:"type:text[]"`
RecipientInterests pq.StringArray `gorm:"type:text[]"`
BudgetMax uint
BudgetMin uint
GiftResponse GiftResponse
DateNeeded time.Time
}

type UserInput struct {
type GiftCollection struct {
gorm.Model
CustomerID *uint
CollectionName string
Gifts []*Gift `gorm:"many2many:gift_request_gifts;"`
}

type GiftResponse struct {
gorm.Model
GiftCollection GiftCollection
GiftCollectionID uint
CustomMessage string
}

type User struct {
gorm.Model
Email string
FirstName string
LastName string
Expand All @@ -30,26 +65,14 @@ type UserInput struct {

type Customer struct {
gorm.Model
UserID uint
User User
//GiftCollections []ExampleGiftCollection
//GiftRequests []ExampleGiftRequests
}

type CustomerInput struct {
UserID uint
User User
//GiftCollections []ExampleGiftCollection
//GiftRequests []ExampleGiftRequests
UserID uint
User User
GiftCollections []*GiftCollection
GiftRequests []*GiftRequest
}

type Admin struct {
gorm.Model
UserID uint
User User
User User
}

type AdminInput struct {
UserID uint
User User
}
6 changes: 4 additions & 2 deletions api/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,25 @@ func TestAddExampleGift(t *testing.T) {
t.Fatalf("Error unmarshaling JSON: %v", e)
}
assert.Equal(t, testGift.Price, giftAdded.Price)
assert.Equal(t, uint(1), giftAdded.ID)
req1, err := http.NewRequest("GET", "/gifts/1", nil)
req1, err := http.NewRequest("GET", fmt.Sprintf("/gifts/%d", giftAdded.ID), nil)
router.ServeHTTP(w1, req1)

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

var giftRetrieved model.ExampleGift
var giftRetrievedDB model.ExampleGift
if e := json.Unmarshal(w1.Body.Bytes(), &giftRetrieved); e != nil {
t.Fatalf("Error unmarshaling JSON: %v", e)
}
err = tx.First(&giftRetrievedDB, giftRetrievedDB.ID).Error

assert.Equal(t, giftAdded.ID, giftRetrieved.ID)
assert.Equal(t, giftAdded.Name, giftRetrieved.Name)
assert.Equal(t, giftAdded.Price, giftRetrieved.Price)
assert.Equal(t, giftAdded.CreatedAt.In(time.UTC).Round(time.Millisecond),
giftRetrieved.CreatedAt.In(time.UTC).Round(time.Millisecond))
}

func TestGetExampleGift(t *testing.T) {
// Database setup
dsn := "user=testuser password=testpwd host=localhost port=5433 dbname=testdb sslmode=disable"
Expand Down
Loading

0 comments on commit f8ca960

Please sign in to comment.