1
0

Parts schema and scaffolld, with file uploading for the image (through Filestack)

This commit is contained in:
Ahmed Al-Taiar
2023-10-27 13:25:08 -04:00
parent 2f753308b1
commit 1eaf76fce2
31 changed files with 1492 additions and 13 deletions

View File

@@ -0,0 +1,35 @@
export const schema = gql`
type Part {
id: Int!
name: String!
description: String
availableStock: Int!
imageUrl: String!
createdAt: DateTime!
}
type Query {
parts: [Part!]! @requireAuth
part(id: Int!): Part @requireAuth
}
input CreatePartInput {
name: String!
description: String
availableStock: Int!
imageUrl: String!
}
input UpdatePartInput {
name: String
description: String
availableStock: Int
imageUrl: String
}
type Mutation {
createPart(input: CreatePartInput!): Part! @requireAuth
updatePart(id: Int!, input: UpdatePartInput!): Part! @requireAuth
deletePart(id: Int!): Part! @requireAuth
}
`

View File

@@ -0,0 +1,11 @@
import type { Prisma, Part } from '@prisma/client'
import type { ScenarioData } from '@redwoodjs/testing/api'
export const standard = defineScenario<Prisma.PartCreateArgs>({
part: {
one: { data: { name: 'String' } },
two: { data: { name: 'String' } },
},
})
export type StandardScenario = ScenarioData<Part, 'part'>

View File

@@ -0,0 +1,49 @@
import type { Part } from '@prisma/client'
import { parts, part, createPart, updatePart, deletePart } from './parts'
import type { StandardScenario } from './parts.scenarios'
// Generated boilerplate tests do not account for all circumstances
// and can fail without adjustments, e.g. Float.
// Please refer to the RedwoodJS Testing Docs:
// https://redwoodjs.com/docs/testing#testing-services
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations
describe('parts', () => {
scenario('returns all parts', async (scenario: StandardScenario) => {
const result = await parts()
expect(result.length).toEqual(Object.keys(scenario.part).length)
})
scenario('returns a single part', async (scenario: StandardScenario) => {
const result = await part({ id: scenario.part.one.id })
expect(result).toEqual(scenario.part.one)
})
scenario('creates a part', async () => {
const result = await createPart({
input: { name: 'String' },
})
expect(result.name).toEqual('String')
})
scenario('updates a part', async (scenario: StandardScenario) => {
const original = (await part({ id: scenario.part.one.id })) as Part
const result = await updatePart({
id: original.id,
input: { name: 'String2' },
})
expect(result.name).toEqual('String2')
})
scenario('deletes a part', async (scenario: StandardScenario) => {
const original = (await deletePart({ id: scenario.part.one.id })) as Part
const result = await part({ id: original.id })
expect(result).toEqual(null)
})
})

View File

@@ -0,0 +1,48 @@
import * as Filestack from 'filestack-js'
import type { QueryResolvers, MutationResolvers } from 'types/graphql'
import { db } from 'src/lib/db'
export const parts: QueryResolvers['parts'] = () => {
return db.part.findMany()
}
export const part: QueryResolvers['part'] = ({ id }) => {
return db.part.findUnique({
where: { id },
})
}
export const createPart: MutationResolvers['createPart'] = ({ input }) => {
return db.part.create({
data: input,
})
}
export const updatePart: MutationResolvers['updatePart'] = ({ id, input }) => {
return db.part.update({
data: input,
where: { id },
})
}
export const deletePart: MutationResolvers['deletePart'] = async ({ id }) => {
const client = Filestack.init(process.env.REDWOOD_ENV_FILESTACK_API_KEY)
const part = await db.part.findUnique({ where: { id } })
const handle = part.imageUrl.split('/').pop()
const security = Filestack.getSecurity(
{
expiry: new Date().getTime() + 5 * 60 * 1000,
handle,
call: ['remove'],
},
process.env.REDWOOD_ENV_FILESTACK_SECRET
)
await client.remove(handle, security)
return db.part.delete({
where: { id },
})
}