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

Created Product and Inventory #19

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
11 changes: 11 additions & 0 deletions prisma/models/Inventory.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
model Inventory {
id String @id @unique
product Product @relation("product_inventory", fields: [id], references: [id], onDelete: Cascade)
available_quantity Int
cost_of_production Float
lead_time Int
reorder_point Int
reorder_quantity Int
safety_stock Int
stock_on_order Int
}
9 changes: 0 additions & 9 deletions prisma/models/Playlist.prisma

This file was deleted.

10 changes: 0 additions & 10 deletions prisma/models/PlaylistToSong.prisma

This file was deleted.

23 changes: 12 additions & 11 deletions prisma/models/Product.prisma
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
model Products {
id String @id @default(uuid())
name String
price Int
description String
details String
weight Int
height Int
width Int
depth Int
special_label_needed Boolean
model Product {
id String @id @default(uuid())
inventory Inventory? @relation("product_inventory")
name String
price Float
description String
details String
weight Int
height Int
width Int
depth Int
special_label_needed Boolean
}
7 changes: 0 additions & 7 deletions prisma/models/Song.prisma

This file was deleted.

7 changes: 0 additions & 7 deletions prisma/models/User.prisma

This file was deleted.

68 changes: 22 additions & 46 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,28 @@ datasource db {
url = env("DATABASE_URL")
}

model Playlist {
id String @id @default(uuid())
name String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
songs PlaylistToSong[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
model Product {
KiranJoji marked this conversation as resolved.
Show resolved Hide resolved
id String @id @default(uuid())
inventory Inventory? @relation("product_inventory")
name String
price Int
description String
details String
weight Int
height Int
width Int
depth Int
special_label_needed Boolean
}

model PlaylistToSong {
playlistId String
playlist Playlist @relation(fields: [playlistId], references: [id], onDelete: Cascade)
songId String
song Song @relation(fields: [songId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

@@id(name: "id", fields: [playlistId, songId])
}

model Products {
id String @id @default(uuid())
name String
price Int
description String
details String
weight Int
height Int
width Int
depth Int
special_label_needed Boolean
}

model Song {
id String @id @default(uuid())
name String
playlists PlaylistToSong[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

model User {
id String @id @default(uuid())
name String
playlists Playlist[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
model Inventory {
id String @id @unique
product Product @relation("product_inventory", fields: [id], references: [id], onDelete: Cascade)
available_quantity Int
cost_of_production Int
lead_time Int
reorder_point Int
reorder_quantity Int
safety_stock Int
stock_on_order Int
}
15 changes: 15 additions & 0 deletions src/resolvers/Inventory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Inventories from "../services/Inventories";

const resolvers = {
Inventory: {},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if you've implemented the fix I mentioned but just make sure you fill out the product field so that when you query an inventory you're able to also see the product field. It's kind of like what you did in Product.ts.
Product: { inventory: ({ id }) => Inventories.find({ id }), },

Query: {
inventory: (_, { id }) => Inventories.find({ id }),
},
Mutation: {
createInventory: (_, { input }) => Inventories.create({ input }),
updateInventory: (_, { id, input }) => Inventories.update({ id, input }),
deleteInventory: (_, { id }) => Inventories.delete({ id }),
},
};

export default resolvers;
20 changes: 0 additions & 20 deletions src/resolvers/Playlist.ts

This file was deleted.

19 changes: 19 additions & 0 deletions src/resolvers/Product.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Products from "../services/Products";
import Inventories from "../services/Inventories";

const resolvers = {
Product: {
inventory: ({ id }) => Inventories.find({ id }),
},
Query: {
product: (_, { id }) => Products.find({ id }),
products: (_, { ids }) => Products.findAll({ ids }),
},
Mutation: {
createProduct: (_, { input }) => Products.create({ input }),
updateProduct: (_, { id, input }) => Products.update({ id, input }),
deleteProduct: (_, { id }) => Products.delete({ id }),
},
};

export default resolvers;
15 changes: 0 additions & 15 deletions src/resolvers/Song.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/resolvers/User.ts

This file was deleted.

8 changes: 3 additions & 5 deletions src/resolvers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import User from "./User";
import Playlist from "./Playlist";
import Song from "./Song";
import Products from "./Products";
import Product from "./Product";
import Inventory from "./Inventory";

export default [User, Playlist, Products, Song];
export default [Inventory, Product];
70 changes: 70 additions & 0 deletions src/services/Inventories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import prisma from "../prisma/client";

export default class Inventories {
// CREATE
static async create({ input }) {
const {
id,
availableQuantity,
costOfProduction,
leadTime,
reorderPoint,
reorderQuantity,
safetyStock,
stockOnOrder,
} = input;
const inventory = await prisma.inventory.create({
data: {
product: {
connect: { id },
},
available_quantity: availableQuantity || 0,
cost_of_production: costOfProduction || 0.00,
lead_time: leadTime || 0,
reorder_point: reorderPoint || 0,
reorder_quantity: reorderQuantity || 0,
safety_stock: safetyStock || 0,
stock_on_order: stockOnOrder || 0,
},
});
return inventory;
}

// READ
static async find({ id }) {
return prisma.inventory.findUnique({ where: { id } });
}

// static async findAll() {
// return prisma.inventory.findMany();
// }

// UPDATE
static async update({ id, input }) {
try {
const inventory = await prisma.inventory.update({
where: {
id,
},
data: input,
});
return inventory;
} catch (e) {
return null;
}
}

// DELETE
static async delete({ id }) {
try {
await prisma.inventory.delete({
where: {
id,
},
});
return true;
} catch (e) {
return false;
}
}
}
59 changes: 0 additions & 59 deletions src/services/Playlists.ts

This file was deleted.

Loading