From b0fc6cabe39780c8b49e3f55e605d88a5f321551 Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Sun, 29 Nov 2020 00:39:24 +0100 Subject: [PATCH] src/* : use numRange for box dimensions instead of minBoxWidth, etc, use range type NumRange --- src/datasources/db/utils.ts | 46 +++++++++++++++++++----------- src/resolvers/cargoBikeResolver.ts | 43 ++++------------------------ src/schema/type-defs.ts | 42 +++++++++++++++------------ 3 files changed, 59 insertions(+), 72 deletions(-) diff --git a/src/datasources/db/utils.ts b/src/datasources/db/utils.ts index 69318dc..443c9d3 100644 --- a/src/datasources/db/utils.ts +++ b/src/datasources/db/utils.ts @@ -40,13 +40,33 @@ export function genDateRange (struct: any) { * @param from * @param to */ -function genNumRange (from: number, to: number) { - if (from === null || from === undefined) { - from = to; - } else if (to === null || to === undefined) { - to = from; +function genNumRange (range: { min: number, max: number}) :string { + if (!range || (!range.max && !range.min)) { + return null; + } else if (range.min === null || range.min === undefined) { + range.min = range.max; + } else if (range.max === null || range.max === undefined) { + range.max = range.min; } - return from ? '[' + from + ',' + to + ']' : null; + if (range.min < 0) { + throw new UserInputError('Minimal value must be greater or equal to 0'); + } + return range.min ? '[' + range.min + ',' + range.max + ']' : null; +} + +export function minNumRange (parent: string) { + if (!parent || parent === 'empty') { + return null; + } + return (parent).split(',')[0].replace('[', ''); +} + +export function maxNumRange (parent: string) { + if (!parent || parent === 'empty') { + return null; + } + const str = (parent).split(',')[1].replace(']', ''); + return (str.length > 0) ? str : null; } /** @@ -55,16 +75,10 @@ function genNumRange (from: number, to: number) { * @param cargoBike */ export function genBoxDimensions (cargoBike: any) { - cargoBike.dimensionsAndLoad.boxLengthRange = genNumRange(cargoBike.dimensionsAndLoad.minBoxLength, cargoBike.dimensionsAndLoad.maxBoxLength); - cargoBike.dimensionsAndLoad.boxWidthRange = genNumRange(cargoBike.dimensionsAndLoad.minBoxWidth, cargoBike.dimensionsAndLoad.maxBoxWidth); - cargoBike.dimensionsAndLoad.boxHeightRange = genNumRange(cargoBike.dimensionsAndLoad.minBoxHeight, cargoBike.dimensionsAndLoad.maxBoxHeight); - // delete this so update cargo bike works - delete cargoBike.dimensionsAndLoad.minBoxLength; - delete cargoBike.dimensionsAndLoad.maxBoxLength; - delete cargoBike.dimensionsAndLoad.minBoxWidth; - delete cargoBike.dimensionsAndLoad.maxBoxWidth; - delete cargoBike.dimensionsAndLoad.minBoxHeight; - delete cargoBike.dimensionsAndLoad.maxBoxHeight; + if (!cargoBike.dimensionsAndLoad) { return; } + cargoBike.dimensionsAndLoad.boxLengthRange = genNumRange(cargoBike.dimensionsAndLoad.boxLengthRange); + cargoBike.dimensionsAndLoad.boxWidthRange = genNumRange(cargoBike.dimensionsAndLoad.boxWidthRange); + cargoBike.dimensionsAndLoad.boxHeightRange = genNumRange(cargoBike.dimensionsAndLoad.boxHeightRange); } /** diff --git a/src/resolvers/cargoBikeResolver.ts b/src/resolvers/cargoBikeResolver.ts index 502f181..27542dd 100644 --- a/src/resolvers/cargoBikeResolver.ts +++ b/src/resolvers/cargoBikeResolver.ts @@ -161,45 +161,12 @@ export default { isLockedByMe: (parent: any, __: any, { req }: { req: any }) => isLockedByMe(parent, { req }), isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req }) }, - DimensionsAndLoad: { - minBoxLength: (parent: any) => { - if (!parent.boxLengthRange || parent.boxLengthRange === 'empty') { - return null; - } - return parent.boxLengthRange ? (parent.boxLengthRange as string).split(',')[0].replace('[', '') : null; - }, - maxBoxLength: (parent: any) => { - if (!parent.boxLengthRange || parent.boxLengthRange === 'empty') { - return null; - } - const str = (parent.boxLengthRange as string).split(',')[1].replace(']', ''); - return (str.length > 0) ? str : null; - }, - minBoxWidth: (parent: any) => { - if (!parent.boxWidthRange || parent.boxWidthRange === 'empty') { - return null; - } - return parent.boxWidthRange ? (parent.boxWidthRange as string).split(',')[0].replace('[', '') : null; + NumRange: { + min: (parent: string) => { + return parent.split(',')[0].replace('[', ''); }, - maxBoxWidth: (parent: any) => { - if (!parent.boxWidthRange || parent.boxWidthRange === 'empty') { - return null; - } - const str = (parent.boxWidthRange as string).split(',')[1].replace(']', ''); - return (str.length > 0) ? str : null; - }, - minBoxHeight: (parent: any) => { - if (!parent.boxHeightRange || parent.boxHeightRange === 'empty') { - return null; - } - return parent.boxHeightRange ? (parent.boxHeightRange as string).split(',')[0].replace('[', '') : null; - }, - maxBoxHeight: (parent: any) => { - if (!parent.boxHeightRange || parent.boxHeightRange === 'empty') { - return null; - } - const str = (parent.boxHeightRange as string).split(',')[1].replace(']', ''); - return (str.length > 0) ? str : null; + max: (parent: string) => { + return parent.split(',')[1].replace(']', ''); } }, Equipment: { diff --git a/src/schema/type-defs.ts b/src/schema/type-defs.ts index ff66e76..af19618 100644 --- a/src/schema/type-defs.ts +++ b/src/schema/type-defs.ts @@ -33,6 +33,7 @@ export default gql` The kind of currency depends on the database. """ scalar Money + "The CargoBike type is central to the graph. You could call it the root." type CargoBike { id: ID! @@ -266,17 +267,28 @@ export default gql` notes: String } + type NumRange { + min: Float + max: Float + } + + """ + If min or max is omitted, the omitted value will be the same as the other given value + So if you pass one as null, both values with be over written with null. + """ + input NumRangeInput { + min: Float + max: Float + } + "How are the dimensions and how much weight can handle a bike. This data is merged in the CargoBike table and the BikeModel table." type DimensionsAndLoad { hasCoverBox: Boolean "cover box can be locked" lockable: Boolean - minBoxLength: Float - maxBoxLength: Float - minBoxWidth: Float - maxBoxWidth: Float - minBoxHeight: Float - maxBoxHeight: Float + boxLengthRange: NumRange + boxWidthRange: NumRange + boxHeightRange: NumRange maxWeightBox: Float maxWeightLuggageRack: Float maxWeightTotal: Float @@ -289,12 +301,9 @@ export default gql` input DimensionsAndLoadCreateInput { hasCoverBox: Boolean lockable: Boolean - minBoxLength: Float - maxBoxLength: Float - minBoxWidth: Float - maxBoxWidth: Float - minBoxHeight: Float - maxBoxHeight: Float + boxLengthRange: NumRangeInput + boxWidthRange: NumRangeInput + boxHeightRange: NumRangeInput maxWeightBox: Float maxWeightLuggageRack: Float maxWeightTotal: Float @@ -307,12 +316,9 @@ export default gql` input DimensionsAndLoadUpdateInput { hasCoverBox: Boolean lockable: Boolean - minBoxLength: Float - maxBoxLength: Float - minBoxWidth: Float - maxBoxWidth: Float - minBoxHeight: Float - maxBoxHeight: Float + boxLengthRange: NumRangeInput + boxWidthRange: NumRangeInput + boxHeightRange: NumRangeInput maxWeightBox: Float maxWeightLuggageRack: Float maxWeightTotal: Float