Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors #71

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 0 additions & 99 deletions internal/pkg/db/migration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package migration
import (
"fmt"
"log"
"time"

"github.com/go-gormigrate/gormigrate/v2"
"github.com/gofrs/uuid"
"gorm.io/datatypes"
"gorm.io/gorm"

"github.com/bradpurchase/grocerytime-backend/internal/pkg/db/models"
Expand Down Expand Up @@ -67,102 +64,6 @@ func AutoMigrateService(db *gorm.DB) error {
return tx.Exec("TRUNCATE TABLE api_clients").Error
},
},
{
// Add index idx_items_grocery_trip_id_category_id
ID: "202006021110_add_idx_items_grocery_trip_id_category_id",
Migrate: func(tx *gorm.DB) error {
return tx.Exec("CREATE INDEX IF NOT EXISTS idx_grocery_trip_id_category_id ON items (grocery_trip_id, category_id)").Error
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec("DROP INDEX idx_grocery_trip_id_category_id").Error
},
},
{
// Add index idx_store_users_store_id
ID: "202006021117_add_idx_store_users_store_id_user_id",
Migrate: func(tx *gorm.DB) error {
return tx.Exec("CREATE INDEX IF NOT EXISTS idx_store_users_store_id_user_id ON store_users (store_id, user_id)").Error
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec("DROP INDEX idx_store_users_store_id_user_id").Error
},
},
{
// Create store_item_category_settings
ID: "202105020850_create_store_item_category_settings",
Migrate: func(tx *gorm.DB) error {
type StoreItemCategorySettings struct {
ID uuid.UUID `gorm:"primaryKey;type:uuid;default:gen_random_uuid()"`
StoreID uuid.UUID `gorm:"type:uuid;not null;index:idx_store_category_items_store_id"`
Items datatypes.JSON

CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt
}
return tx.AutoMigrate(&StoreItemCategorySettings{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable("store_item_category_settings")
},
},
{
ID: "202105050742_create_store_staple_items",
Migrate: func(tx *gorm.DB) error {
type StoreStapleItem struct {
ID uuid.UUID `gorm:"primaryKey;type:uuid;default:gen_random_uuid()"`
StoreID uuid.UUID `gorm:"type:uuid;not null;index:idx_store_staple_items_store_id"`
Name string `gorm:"type:varchar(100);not null"`

CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt
}
return tx.AutoMigrate(&StoreStapleItem{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable("store_staple_items")
},
},
{
// Add index idx_store_staple_items_store_id_name
ID: "202105050820_add_idx_store_staple_items_store_id_name",
Migrate: func(tx *gorm.DB) error {
return tx.Exec("CREATE INDEX IF NOT EXISTS idx_store_staple_items_store_id_name ON store_staple_items (store_id, name)").Error
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec("DROP INDEX idx_store_staple_items_store_id_name").Error
},
},
{
// Add column staple_id to items
ID: "202105060735_add_staple_id_to_items",
Migrate: func(tx *gorm.DB) error {
type Item struct {
StapleItemID *uuid.UUID `gorm:"type:uuid;index"`
}
return tx.AutoMigrate(&Item{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropColumn("people", "age")
},
},
{
// Add column instructions to recipes
ID: "202109070759_add_instructions_to_recipes",
Migrate: func(tx *gorm.DB) error {
type Recipe struct {
Instructions datatypes.JSON
}
return tx.AutoMigrate(&Recipe{})
},
Rollback: func(tx *gorm.DB) error {
type Recipe struct {
Instructions datatypes.JSON
}
return tx.Migrator().DropColumn(&Recipe{}, "instructions")
},
},
})
return m.Migrate()
}
3 changes: 2 additions & 1 deletion internal/pkg/gql/resolvers/decline_store_invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/bradpurchase/grocerytime-backend/internal/pkg/auth"
"github.com/bradpurchase/grocerytime-backend/internal/pkg/stores"
"github.com/graphql-go/graphql"
uuid "github.com/satori/go.uuid"
)

// DeclineStoreInviteResolver resolves the declineStoreResolver resolver by calling
Expand All @@ -16,7 +17,7 @@ func DeclineStoreInviteResolver(p graphql.ResolveParams) (interface{}, error) {
return nil, err
}

storeID := p.Args["storeId"]
storeID := p.Args["storeId"].(uuid.UUID)
storeUser, err := stores.RemoveUserFromStore(user, storeID)
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/gql/resolvers/delete_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/bradpurchase/grocerytime-backend/internal/pkg/auth"
"github.com/bradpurchase/grocerytime-backend/internal/pkg/trips"
"github.com/graphql-go/graphql"
uuid "github.com/satori/go.uuid"
)

// DeleteItemResolver deletes an item by itemId param
Expand All @@ -14,7 +15,7 @@ func DeleteItemResolver(p graphql.ResolveParams) (interface{}, error) {
return nil, err
}

itemID := p.Args["itemId"]
itemID := p.Args["itemId"].(uuid.UUID)
item, err := trips.DeleteItem(itemID)
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/gql/resolvers/delete_meal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/bradpurchase/grocerytime-backend/internal/pkg/auth"
"github.com/bradpurchase/grocerytime-backend/internal/pkg/meals"
"github.com/graphql-go/graphql"
uuid "github.com/satori/go.uuid"
)

// DeleteMealResolver resolves the deleteMeal mutation
Expand All @@ -14,7 +15,7 @@ func DeleteMealResolver(p graphql.ResolveParams) (interface{}, error) {
return nil, err
}

mealID := p.Args["id"]
mealID := p.Args["id"].(uuid.UUID)
userID := user.ID
appScheme := p.Info.RootValue.(map[string]interface{})["App-Scheme"]
meal, err := meals.DeleteMeal(mealID, userID, appScheme.(string))
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/gql/resolvers/delete_recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/bradpurchase/grocerytime-backend/internal/pkg/auth"
"github.com/bradpurchase/grocerytime-backend/internal/pkg/meals"
"github.com/graphql-go/graphql"
uuid "github.com/satori/go.uuid"
)

// DeleteRecipeResolver resolves the deleteRecipe mutation
Expand All @@ -14,7 +15,7 @@ func DeleteRecipeResolver(p graphql.ResolveParams) (interface{}, error) {
return nil, err
}

recipeID := p.Args["id"]
recipeID := p.Args["id"].(uuid.UUID)
userID := user.ID
recipe, err := meals.DeleteRecipe(recipeID, userID)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/gql/resolvers/delete_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/bradpurchase/grocerytime-backend/internal/pkg/auth"
"github.com/bradpurchase/grocerytime-backend/internal/pkg/stores"
"github.com/graphql-go/graphql"
uuid "github.com/satori/go.uuid"
)

// DeleteStoreResolver resolves the deleteStore mutation by deleting a store
Expand All @@ -15,7 +16,7 @@ func DeleteStoreResolver(p graphql.ResolveParams) (interface{}, error) {
return nil, err
}

storeID := p.Args["storeId"]
storeID := p.Args["storeId"].(uuid.UUID)
store, err := stores.DeleteStore(storeID, user.ID)
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/gql/resolvers/grocery_trip.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/bradpurchase/grocerytime-backend/internal/pkg/auth"
"github.com/bradpurchase/grocerytime-backend/internal/pkg/trips"
"github.com/graphql-go/graphql"
uuid "github.com/satori/go.uuid"
)

// GroceryTripResolver retrieves a grocery trip by ID
Expand All @@ -14,7 +15,7 @@ func GroceryTripResolver(p graphql.ResolveParams) (interface{}, error) {
return nil, err
}

tripID := p.Args["id"]
tripID := p.Args["id"].(uuid.UUID)
trip, err := trips.RetrieveTrip(tripID)
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/gql/resolvers/grocery_trips.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/bradpurchase/grocerytime-backend/internal/pkg/auth"
"github.com/bradpurchase/grocerytime-backend/internal/pkg/trips"
"github.com/graphql-go/graphql"
uuid "github.com/satori/go.uuid"
)

// GroceryTripsResolver resolves the trips mutation
Expand All @@ -14,7 +15,7 @@ func GroceryTripsResolver(p graphql.ResolveParams) (interface{}, error) {
return nil, err
}

storeID := p.Args["storeId"]
storeID := p.Args["storeId"].(uuid.UUID)
userID := user.ID
completed := p.Args["completed"].(bool)
trips, err := trips.RetrieveTrips(storeID, userID, completed)
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/gql/resolvers/invite_to_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/bradpurchase/grocerytime-backend/internal/pkg/db/models"
"github.com/bradpurchase/grocerytime-backend/internal/pkg/stores"
"github.com/graphql-go/graphql"
uuid "github.com/satori/go.uuid"
)

// InviteToStoreResolver resolves the inviteToStore mutation by creating a pending
Expand All @@ -20,7 +21,7 @@ func InviteToStoreResolver(p graphql.ResolveParams) (interface{}, error) {
}
userEmail := user.Email

storeID := p.Args["storeId"]
storeID := p.Args["storeId"].(uuid.UUID)
invitedUserEmail := strings.TrimSpace(p.Args["email"].(string))
if userEmail == invitedUserEmail {
return models.StoreUser{}, errors.New("cannot invite yourself to a store")
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/gql/resolvers/join_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package resolvers
import (
"github.com/bradpurchase/grocerytime-backend/internal/pkg/auth"
"github.com/bradpurchase/grocerytime-backend/internal/pkg/stores"
uuid "github.com/satori/go.uuid"

"github.com/graphql-go/graphql"
)
Expand All @@ -16,7 +17,7 @@ func JoinStoreResolver(p graphql.ResolveParams) (interface{}, error) {
return nil, err
}

storeID := p.Args["storeId"]
storeID := p.Args["storeId"].(uuid.UUID)
storeUser, err := stores.AddUserToStore(user, storeID)
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/gql/resolvers/leave_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/bradpurchase/grocerytime-backend/internal/pkg/auth"
"github.com/bradpurchase/grocerytime-backend/internal/pkg/stores"
"github.com/graphql-go/graphql"
uuid "github.com/satori/go.uuid"
)

// LeaveStoreResolver resolves the leaveStore resolver by removing the current user from the store
Expand All @@ -14,7 +15,7 @@ func LeaveStoreResolver(p graphql.ResolveParams) (interface{}, error) {
return nil, err
}

storeID := p.Args["storeId"]
storeID := p.Args["storeId"].(uuid.UUID)
storeUser, err := stores.RemoveUserFromStore(user, storeID)
if err != nil {
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion internal/pkg/gql/resolvers/meal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/bradpurchase/grocerytime-backend/internal/pkg/auth"
"github.com/bradpurchase/grocerytime-backend/internal/pkg/meals"
"github.com/graphql-go/graphql"
uuid "github.com/satori/go.uuid"
)

// MealResolver resolves the meals query
Expand All @@ -14,7 +15,8 @@ func MealResolver(p graphql.ResolveParams) (interface{}, error) {
return nil, err
}

meal, err := meals.RetrieveMealForUser(p.Args["id"], user.ID)
mealID := p.Args["id"].(uuid.UUID)
meal, err := meals.RetrieveMealForUser(mealID, user.ID)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/bradpurchase/grocerytime-backend/internal/pkg/auth"
"github.com/bradpurchase/grocerytime-backend/internal/pkg/notifications"
"github.com/graphql-go/graphql"
uuid "github.com/satori/go.uuid"
)

// NotifyTripUpdatedItemsAddedResolver resolves the notifyTripUpdatedItemsAdded mutation
Expand All @@ -21,7 +22,7 @@ func NotifyTripUpdatedItemsAddedResolver(p graphql.ResolveParams) (interface{},
appScheme := p.Info.RootValue.(map[string]interface{})["App-Scheme"]
if appScheme != nil {
userID := user.ID
storeID := p.Args["storeId"]
storeID := p.Args["storeId"].(uuid.UUID)
numItemsAdded := p.Args["numItemsAdded"].(int)
go notifications.ItemsAdded(userID, storeID, numItemsAdded, appScheme.(string))
return true, nil
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/gql/resolvers/recipes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/bradpurchase/grocerytime-backend/internal/pkg/auth"
"github.com/bradpurchase/grocerytime-backend/internal/pkg/meals"
"github.com/graphql-go/graphql"
uuid "github.com/satori/go.uuid"
)

// RecipesResolver resolves the recipes query
Expand All @@ -24,7 +25,7 @@ func RecipesResolver(p graphql.ResolveParams) (interface{}, error) {

// RecipeResolver resolves the recipe query
func RecipeResolver(p graphql.ResolveParams) (interface{}, error) {
recipeID := p.Args["id"]
recipeID := p.Args["id"].(uuid.UUID)
recipe, err := meals.RetrieveRecipe(recipeID)
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/gql/resolvers/reorder_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/bradpurchase/grocerytime-backend/internal/pkg/auth"
"github.com/bradpurchase/grocerytime-backend/internal/pkg/trips"
"github.com/graphql-go/graphql"
uuid "github.com/satori/go.uuid"
)

// ReorderItemResolver updates the position of an item with the provided params
Expand All @@ -14,7 +15,7 @@ func ReorderItemResolver(p graphql.ResolveParams) (interface{}, error) {
return nil, err
}

itemID := p.Args["itemId"]
itemID := p.Args["itemId"].(uuid.UUID)
position := p.Args["position"].(int)
trip, err := trips.ReorderItem(itemID, position)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion internal/pkg/gql/resolvers/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/bradpurchase/grocerytime-backend/internal/pkg/auth"
"github.com/bradpurchase/grocerytime-backend/internal/pkg/stores"
"github.com/graphql-go/graphql"
uuid "github.com/satori/go.uuid"
)

// StoreResolver resolves the store GraphQL query by retrieving a store by ID param
Expand All @@ -14,7 +15,8 @@ func StoreResolver(p graphql.ResolveParams) (interface{}, error) {
return nil, err
}

store, err := stores.RetrieveStoreForUser(p.Args["id"], user.ID)
storeID := p.Args["id"].(uuid.UUID)
store, err := stores.RetrieveStoreForUser(storeID, user.ID)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/gql/resolvers/store_user_prefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/bradpurchase/grocerytime-backend/internal/pkg/auth"
"github.com/bradpurchase/grocerytime-backend/internal/pkg/stores"
"github.com/graphql-go/graphql"
uuid "github.com/satori/go.uuid"
)

// StoreUserPrefsResolver resolves the storeUserPrefs query
Expand All @@ -15,7 +16,7 @@ func StoreUserPrefsResolver(p graphql.ResolveParams) (interface{}, error) {
}

// Find the StoreUser record from the storeId arg provided and current user ID
storeID := p.Args["storeId"]
storeID := p.Args["storeId"].(uuid.UUID)
userID := user.ID
storeUserID, err := stores.RetrieveStoreUserID(storeID, userID)
if err != nil {
Expand Down
Loading