Part browsing
This commit is contained in:
@@ -8,7 +8,29 @@ export const schema = gql`
|
||||
createdAt: DateTime!
|
||||
}
|
||||
|
||||
type PartPage {
|
||||
parts: [Part!]!
|
||||
count: Int!
|
||||
page: Int!
|
||||
sort: SortMethod!
|
||||
order: SortOrder!
|
||||
}
|
||||
|
||||
enum SortMethod {
|
||||
id
|
||||
name
|
||||
description
|
||||
stock
|
||||
createdAt
|
||||
}
|
||||
|
||||
enum SortOrder {
|
||||
ascending
|
||||
descending
|
||||
}
|
||||
|
||||
type Query {
|
||||
partPage(page: Int, sort: SortMethod, order: SortOrder): PartPage @skipAuth
|
||||
parts: [Part!]! @skipAuth
|
||||
part(id: Int!): Part @skipAuth
|
||||
}
|
||||
|
||||
@@ -1,8 +1,18 @@
|
||||
import * as Filestack from 'filestack-js'
|
||||
import type { QueryResolvers, MutationResolvers } from 'types/graphql'
|
||||
import type {
|
||||
QueryResolvers,
|
||||
MutationResolvers,
|
||||
SortMethod,
|
||||
SortOrder,
|
||||
} from 'types/graphql'
|
||||
|
||||
import { db } from 'src/lib/db'
|
||||
|
||||
const PARTS_PER_PAGE = 8
|
||||
|
||||
const removeEnding = (input: string): string =>
|
||||
input.endsWith('ending') ? input.slice(0, -6) : input
|
||||
|
||||
export const parts: QueryResolvers['parts'] = () => {
|
||||
return db.part.findMany()
|
||||
}
|
||||
@@ -13,6 +23,59 @@ export const part: QueryResolvers['part'] = ({ id }) => {
|
||||
})
|
||||
}
|
||||
|
||||
export const partPage = ({
|
||||
page = 1,
|
||||
sort = 'id',
|
||||
order = 'ascending',
|
||||
}: {
|
||||
page: number
|
||||
sort: SortMethod
|
||||
order: SortOrder
|
||||
}) => {
|
||||
const offset = (page - 1) * PARTS_PER_PAGE
|
||||
let orderByCase
|
||||
|
||||
switch (sort) {
|
||||
case 'id':
|
||||
orderByCase = { id: removeEnding(order) }
|
||||
break
|
||||
|
||||
case 'name':
|
||||
orderByCase = { name: removeEnding(order) }
|
||||
break
|
||||
|
||||
case 'createdAt':
|
||||
orderByCase = { createdAt: removeEnding(order) }
|
||||
break
|
||||
|
||||
case 'description':
|
||||
orderByCase = { description: removeEnding(order) }
|
||||
break
|
||||
|
||||
case 'stock':
|
||||
orderByCase = {
|
||||
availableStock: removeEnding(order),
|
||||
}
|
||||
break
|
||||
|
||||
default:
|
||||
orderByCase = { id: removeEnding(order) }
|
||||
break
|
||||
}
|
||||
|
||||
return {
|
||||
parts: db.part.findMany({
|
||||
take: PARTS_PER_PAGE,
|
||||
skip: offset,
|
||||
orderBy: orderByCase,
|
||||
}),
|
||||
count: db.part.count(),
|
||||
page,
|
||||
sort,
|
||||
order,
|
||||
}
|
||||
}
|
||||
|
||||
export const createPart: MutationResolvers['createPart'] = ({ input }) => {
|
||||
return db.part.create({
|
||||
data: input,
|
||||
|
||||
Reference in New Issue
Block a user