Skip to content

Commit

Permalink
Merge pull request #18 from include-davis/7-api-product
Browse files Browse the repository at this point in the history
Feat/API-Product
  • Loading branch information
brandonw504 authored Apr 1, 2024
2 parents ecd2e5b + 0cee518 commit c59de61
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 2 deletions.
15 changes: 15 additions & 0 deletions prisma/migrations/20240314173520_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- CreateTable
CREATE TABLE "Products" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"price" INTEGER NOT NULL,
"description" TEXT NOT NULL,
"details" TEXT NOT NULL,
"weight" INTEGER NOT NULL,
"height" INTEGER NOT NULL,
"width" INTEGER NOT NULL,
"depth" INTEGER NOT NULL,
"special_label_needed" BOOLEAN NOT NULL,

CONSTRAINT "Products_pkey" PRIMARY KEY ("id")
);
12 changes: 12 additions & 0 deletions prisma/models/Product.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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
}
13 changes: 13 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ model PlaylistToSong {
@@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
Expand Down
15 changes: 15 additions & 0 deletions src/resolvers/Products.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Products from "../services/Products";

const resolvers = {
Query: {
products: (_, { id }) => Products.find({ id }),
manyProducts: (_, { ids }) => Products.findAll({ ids }),
},
Mutation: {
createProducts: (_, { input }) => Products.create({ input }),
updateProducts: (_, { id, input }) => Products.update({ id, input }),
deleteProducts: (_, { id }) => Products.delete({ id }),
},
};

export default resolvers;
3 changes: 2 additions & 1 deletion src/resolvers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import User from "./User";
import Playlist from "./Playlist";
import Song from "./Song";
import Products from "./Products";

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

export default class Products {
// CREATE
static async create({ input }) {
const products = await prisma.products.create({
data: input,
});
return products;
}

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

static async findAll({ ids }) {
if (!ids) {
return prisma.products.findMany();
}
return prisma.products.findMany({
where: {
id: {
in: ids,
},
},
});
}

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

// DELETE
static async delete({ id }) {
try {
await prisma.products.delete({
where: {
id,
},
});
return true;
} catch (e) {
return false;
}
}
}
52 changes: 52 additions & 0 deletions src/typeDefs/Products.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import gql from "graphql-tag";

const typeDefs = gql`
type Products {
id: ID!
name: String!
price: Int!
description: String!
details: String!
weight: Int!
height: Int!
width: Int!
depth: Int!
special_label_needed: Boolean!
}
input ProductsInput {
name: String!
price: Int!
description: String!
details: String!
weight: Int!
height: Int!
width: Int!
depth: Int!
special_label_needed: Boolean!
}
input ProductsInputOptional {
name: String
price: Int
description: String
details: String
weight: Int
height: Int
width: Int
depth: Int
special_label_needed: Boolean
}
type Query {
products(id: ID!): Products
manyProducts(id: [ID]): [Products]
}
type Mutation {
createProducts(input: ProductsInput!): Products
updateProducts(id: ID!, input: ProductsInputOptional!): Products
deleteProducts(id: ID!): Boolean
}
`;
export default typeDefs;
3 changes: 2 additions & 1 deletion src/typeDefs/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import User from "./User.js";
import Playlist from "./Playlist.js";
import Song from "./Song.js";
import Products from "./Products.js";

export default [User, Playlist, Song];
export default [User, Playlist, Song, Products];

0 comments on commit c59de61

Please sign in to comment.