Skip to content

Commit

Permalink
Merge pull request #45 from GenerateNU/36-cc-15-update-gift-type-and-…
Browse files Browse the repository at this point in the history
…testing

cc 15 update gift type and testing
  • Loading branch information
matherg authored Nov 7, 2023
2 parents 5fab4ac + 1643b9d commit e20f0e4
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 49 deletions.
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

Binary file removed api/main
Binary file not shown.
8 changes: 6 additions & 2 deletions api/src/model/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ func UpdateGiftToDb(db *gorm.DB, id int64, inputGift Gift) (Gift, error) {
if inputGift.Description != "" {
updates["Description"] = inputGift.Description
}
if inputGift.Occasion != "" {
updates["Occasion"] = inputGift.Occasion
}
if len(inputGift.Category) != 0 {
updates["Category"] = inputGift.Category
}
if inputGift.Demographic != "" {
updates["Demographic"] = inputGift.Demographic
}
Expand Down Expand Up @@ -193,7 +199,6 @@ func GetGiftFromDB(db *gorm.DB, id int64) (Gift, error) {
return gift, nil
}

// GetAllGiftsFromDB fetches all ExampleGift
// GetAllGiftsFromDB fetches all Gift
func GetAllGiftsFromDB(db *gorm.DB) ([]Gift, error) {
var gifts []Gift
Expand All @@ -203,7 +208,6 @@ func GetAllGiftsFromDB(db *gorm.DB) ([]Gift, error) {
return gifts, nil
}

// GetAllResponsesFromDB fetches all GiftResponse
// WriteGiftToDb saves the Gift and returns it
func WriteGiftToDb(db *gorm.DB, inputGift Gift) (Gift, error) {
if err := db.Create(&inputGift).Error; err != nil {
Expand Down
5 changes: 4 additions & 1 deletion api/src/model/types.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package model

import (
"time"

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

type Gift struct {
Expand All @@ -13,6 +14,8 @@ type Gift struct {
Link string
Description string
Demographic string
Category pq.StringArray `gorm:"type:text[]"`
Occasion string
GiftCollections []*GiftCollection `gorm:"many2many:gift_request_gifts;"`
}

Expand Down
9 changes: 8 additions & 1 deletion api/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"sort"
"time"

"github.com/stretchr/testify/assert"

"github.com/lib/pq"

"os"
Expand Down Expand Up @@ -242,6 +243,8 @@ func TestAddRequest(t *testing.T) {
request := model.GiftRequest{
CustomerID: retrievedCustomer.ID,
RecipientName: "Friend",
Occasion: pq.StringArray{"Birthday", "Anniversary"},
RecipientInterests: pq.StringArray{"Reading", "Gaming"},
}

// Create the GiftRequest and call the endpoint
Expand All @@ -261,6 +264,8 @@ func TestAddRequest(t *testing.T) {
var retrievedRequest model.GiftRequest
err = tx.First(&retrievedRequest).Error
assert.Equal(t, addedRequest.RecipientName, retrievedRequest.RecipientName)
assert.Equal(t, addedRequest.Occasion, retrievedRequest.Occasion)
assert.Equal(t, addedRequest.RecipientInterests, retrievedRequest.RecipientInterests)

}

Expand Down Expand Up @@ -976,6 +981,7 @@ func TestAddGiftToCollection(t *testing.T) {
// Add Gift to Gift Collection
gift := model.Gift{
Name: "Gift1",
Category: pq.StringArray{"Best selling", "Gadgets"},
}
giftJSON, err := json.Marshal(gift)
if err != nil {
Expand All @@ -995,6 +1001,7 @@ func TestAddGiftToCollection(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, giftAddedRetrievedCollection.CollectionName, giftAddedCollection.CollectionName)
assert.Equal(t, giftAddedRetrievedCollection.Gifts[0].Name, giftAddedCollection.Gifts[0].Name)
assert.Equal(t, giftAddedRetrievedCollection.Gifts[0].Category, giftAddedCollection.Gifts[0].Category)
}

func TestGiftDeleteFromCollection(t *testing.T) {
Expand Down
13 changes: 10 additions & 3 deletions api/tests/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package tests

import (
"CaitsCurates/backend/src/model"
"github.com/stretchr/testify/assert"
"os"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/lib/pq"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -52,7 +54,7 @@ func TestGiftModel(t *testing.T) {
defer tx.Rollback()

// Create Gift
gift := model.Gift{Name: "Super cool new toy", Price: 500000.00, Link: "https://www.youtube.com/watch?v=dQw4w9WgXcQ", Description: "Really great content. Highly recommend", Demographic: "Unknown..."}
gift := model.Gift{Name: "Super cool new toy", Price: 500000.00, Link: "https://www.youtube.com/watch?v=dQw4w9WgXcQ", Description: "Really great content. Highly recommend", Demographic: "Unknown...", Category: pq.StringArray{"Best selling"}}

err = tx.Create(&gift).Error
assert.NoError(t, err)
Expand All @@ -68,6 +70,7 @@ func TestGiftModel(t *testing.T) {
assert.Equal(t, gift.Description, fetchedGift.Description)
assert.Equal(t, gift.Demographic, fetchedGift.Demographic)
assert.Equal(t, gift.Link, fetchedGift.Link)
assert.Equal(t, gift.Category, fetchedGift.Category)
assert.Equal(t, gift.CreatedAt.In(time.UTC).Round(time.Millisecond),
fetchedGift.CreatedAt.In(time.UTC).Round(time.Millisecond))

Expand Down Expand Up @@ -117,7 +120,7 @@ func TestGiftRequestModel(t *testing.T) {
assert.NoError(t, err)

// Create GiftRequest
giftRequest := model.GiftRequest{GiftResponse: &giftResponse}
giftRequest := model.GiftRequest{GiftResponse: &giftResponse, Occasion: pq.StringArray{"Birthday"}, RecipientInterests: pq.StringArray{"Soccer"}}
user := model.User{Email: "[email protected]", FirstName: "person1", LastName: "lastname1", Password: "dgeeg32"}
customer := model.Customer{GiftRequests: []*model.GiftRequest{&giftRequest}, User: user}
err = tx.Create(&customer).Error
Expand Down Expand Up @@ -146,6 +149,8 @@ func TestGiftRequestModel(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, giftRequest.ID, fetchedGiftRequest.ID)
assert.Equal(t, giftRequest.GiftResponseID, fetchedGiftRequest.GiftResponseID)
assert.Equal(t, giftRequest.Occasion, fetchedGiftRequest.Occasion)
assert.Equal(t, giftRequest.RecipientInterests, fetchedGiftRequest.RecipientInterests)
assert.Equal(t, giftRequest.CreatedAt.In(time.UTC).Round(time.Millisecond),
fetchedGiftRequest.CreatedAt.In(time.UTC).Round(time.Millisecond))

Expand Down Expand Up @@ -257,6 +262,7 @@ func TestGiftResponseModel(t *testing.T) {
Link: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
Description: "Really great content. Highly recommend",
Demographic: "Unknown...",
Category: pq.StringArray{"Best selling"},
GiftCollections: nil,
}

Expand All @@ -266,6 +272,7 @@ func TestGiftResponseModel(t *testing.T) {
Link: "https://www.youtube.com/Penguinz0",
Description: "Really great content. Highly recommend",
Demographic: "Unknown...",
Category: pq.StringArray{"Best selling"},
GiftCollections: nil,
}

Expand Down
16 changes: 14 additions & 2 deletions client/src/components/GiftForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {Gift} from "../types.tsx";


const defaultGift: Gift = {
Demographic: "", Description: "", GiftCollections: [], ID: 0, Link: "", Name: "", Price: 0
Demographic: "", Description: "", Occasion: "", Category: [], GiftCollections: [], ID: 0, Link: "", Name: "", Price: 0
}
type Props = {
initialGift?: Gift;
Expand Down Expand Up @@ -80,7 +80,19 @@ const GiftForm: React.FC<Props> = ({ initialGift = defaultGift, mode, onGiftChan
className="mt-1 p-2 w-full border-2 border-gray-300 rounded-md"
/>
</div>

<div className="mb-4">
<label htmlFor="occasion" className="block text-sm font-medium text-gray-700">
Description:
</label>
<input
type="text"
id="occasion"
name="Occasion"
value={gift.Occasion}
onChange={handleInputChange}
className="mt-1 p-2 w-full border-2 border-gray-300 rounded-md"
/>
</div>
<div className="mb-4">
<label htmlFor="price" className="block text-sm font-medium text-gray-700">
Price:
Expand Down
2 changes: 2 additions & 0 deletions client/src/components/GiftItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ const GiftItem = (props: GiftProps) => {
>Link</a>
</div>
<p>Demographic: {props.gift.Demographic}</p>
<p>Categories: {props.gift.Category}</p>
<p>Description: {props.gift.Description}</p>
<p>Description: {props.gift.Occasion}</p>
</div>

<div className='w-1/12 flex flex-row space-x-2'>
Expand Down
76 changes: 39 additions & 37 deletions client/src/types.tsx
Original file line number Diff line number Diff line change
@@ -1,56 +1,58 @@
export interface Gift {
ID : number
Name: string;
Price: number;
Link: string;
Description: string;
Demographic: string;
GiftCollections: GiftCollection[];
ID: number;
Name: string;
Price: number;
Link: string;
Occasion: string;
Description: string;
Demographic: string;
GiftCollections: GiftCollection[];
Category: string[];
}

export interface GiftRequest {
ID : number
CustomerId: number;
GiftResponseId: number | null;
RecipientName: string;
RecipientAge: number;
Occasion: string[];
RecipientInterests: string[];
BudgetMax: number;
BudgetMin: number;
GiftResponse: GiftResponse | null;
DateNeeded: string;
ID: number;
CustomerId: number;
GiftResponseId: number | null;
RecipientName: string;
RecipientAge: number;
Occasion: string[];
RecipientInterests: string[];
BudgetMax: number;
BudgetMin: number;
GiftResponse: GiftResponse | null;
DateNeeded: string;
}

export interface GiftCollection {
ID : number
CustomerId: number | null;
Customer: Customer;
CollectionName: string;
Gifts: Gift[];
ID: number;
CustomerId: number | null;
Customer: Customer;
CollectionName: string;
Gifts: Gift[];
}

export interface GiftResponse {
ID : number
GiftCollection: GiftCollection;
GiftCollectionId: number;
CustomMessage: string;
ID: number;
GiftCollection: GiftCollection;
GiftCollectionId: number;
CustomMessage: string;
}

export interface User {
ID : number
Email: string;
FirstName: string;
LastName: string;
Password: string;
ID: number;
Email: string;
FirstName: string;
LastName: string;
Password: string;
}

export interface Customer {
ID : number
UserId: number;
ID: number;
UserId: number;
}

export interface Admin {
ID : number
UserId: number;
}
ID: number;
UserId: number;
}

0 comments on commit e20f0e4

Please sign in to comment.