From 71e737b1c6ac2fc3ec8f6233f7ce34d1ceaf566f Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Thu, 24 Sep 2020 19:20:01 +0200 Subject: [PATCH] TimeFrame.cargoBike/.lendingStation implemented --- src/datasources/db/cargobikeAPI.ts | 9 ++++ src/datasources/db/lendingstationAPI.ts | 56 ++++++++++++++---------- src/resolvers/lendingstationResolvers.ts | 9 +++- src/schema/type-defs.ts | 6 ++- 4 files changed, 55 insertions(+), 25 deletions(-) diff --git a/src/datasources/db/cargobikeAPI.ts b/src/datasources/db/cargobikeAPI.ts index 27c3d24..6cd8304 100644 --- a/src/datasources/db/cargobikeAPI.ts +++ b/src/datasources/db/cargobikeAPI.ts @@ -6,6 +6,7 @@ import { BikeEvent } from '../../model/BikeEvent'; import { Equipment } from '../../model/Equipment'; import { Engagement } from '../../model/Engagement'; import { Provider } from '../../model/Provider'; +import { TimeFrame } from '../../model/TimeFrame'; /** * extended datasource to feed resolvers with data about cargoBikes @@ -56,6 +57,14 @@ export class CargoBikeAPI extends DataSource { .loadMany(); } + async cargoBikeByTimeFrameId (id: number) { + return await this.connection.getRepository(TimeFrame) + .createQueryBuilder('timeframe') + .relation(TimeFrame, 'cargoBike') + .of(id) + .loadOne(); + } + async lockCargoBike (id: number, req: any, dataSources: any) { if (await this.lockEntity(CargoBike, 'cargobike', id, req, dataSources)) { return this.findCargoBikeById(id); diff --git a/src/datasources/db/lendingstationAPI.ts b/src/datasources/db/lendingstationAPI.ts index c371cff..614c704 100644 --- a/src/datasources/db/lendingstationAPI.ts +++ b/src/datasources/db/lendingstationAPI.ts @@ -42,6 +42,14 @@ export class LendingStationAPI extends DataSource { .getOne().catch(() => { return null; }); } + async lendingStationByTimeFrameId (id: number) { + await this.connection.getRepository(TimeFrame) + .createQueryBuilder('timeframe') + .relation(TimeFrame, 'lendingStation') + .of(id) + .loadOne(); + } + async timeFrames (offset: number, limit: number) { return await this.connection.getRepository(TimeFrame) .createQueryBuilder('timeframe') @@ -127,28 +135,32 @@ export class LendingStationAPI extends DataSource { async createTimeFrame (timeFrame: any) { let inserts: any; - await this.connection.transaction(async (entityManager: EntityManager) => { - if (timeFrame.to === undefined) { - timeFrame.to = ''; - } - timeFrame.dateRange = '[' + timeFrame.from + ',' + timeFrame.to + ')'; - inserts = await entityManager.getRepository(TimeFrame) - .createQueryBuilder('timeframe') - .insert() - .returning('*') - .values([timeFrame]) - .execute(); - await entityManager.getRepository(TimeFrame) - .createQueryBuilder() - .relation(TimeFrame, 'cargoBike') - .of(inserts.identifiers[0].id) - .set(timeFrame.cargoBikeId); - await entityManager.getRepository(TimeFrame) - .createQueryBuilder() - .relation(TimeFrame, 'lendingStation') - .of(inserts.identifiers[0].id) - .set(timeFrame.lendingStationId); - }); + try { + await this.connection.transaction(async (entityManager: EntityManager) => { + if (timeFrame.to === undefined) { + timeFrame.to = ''; + } + timeFrame.dateRange = '[' + timeFrame.from + ',' + timeFrame.to + ')'; + inserts = await entityManager.getRepository(TimeFrame) + .createQueryBuilder('timeframe') + .insert() + .returning('*') + .values([timeFrame]) + .execute(); + await entityManager.getRepository(TimeFrame) + .createQueryBuilder() + .relation(TimeFrame, 'cargoBike') + .of(inserts.identifiers[0].id) + .set(timeFrame.cargoBikeId); + await entityManager.getRepository(TimeFrame) + .createQueryBuilder() + .relation(TimeFrame, 'lendingStation') + .of(inserts.identifiers[0].id) + .set(timeFrame.lendingStationId); + }); + } catch (e) { + return new GraphQLError('Transaction could not be completed'); + } inserts.generatedMaps[0].id = inserts.identifiers[0].id; return inserts.generatedMaps[0]; } diff --git a/src/resolvers/lendingstationResolvers.ts b/src/resolvers/lendingstationResolvers.ts index 7b43d0f..10fe8e3 100644 --- a/src/resolvers/lendingstationResolvers.ts +++ b/src/resolvers/lendingstationResolvers.ts @@ -45,7 +45,14 @@ export default { return (parent.dateRange as string).split(',')[0].replace('[', ''); }, to (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { - return (parent.dateRange as string).split(',')[1].replace(')', ''); + const str = (parent.dateRange as string).split(',')[1].replace(')', ''); + return (str.length > 0) ? str : null; + }, + cargoBike (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { + return dataSources.cargoBikeAPI.cargoBikeByTimeFrameId(parent.id); + }, + lendingStation (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { + return dataSources.lendingStationAPI.lendingStationByTimeFrameId(parent.id); } }, Mutation: { diff --git a/src/schema/type-defs.ts b/src/schema/type-defs.ts index 4b4da45..7827638 100644 --- a/src/schema/type-defs.ts +++ b/src/schema/type-defs.ts @@ -659,7 +659,9 @@ input LoanPeriodsInput { "(dt. Zeitscheibe) When was a bike where" type TimeFrame { id: ID! + "format YYYY-MM-dd" from: Date! + "formmat YYYY-MM-dd" to: Date note: String lendingStation: LendingStation! @@ -670,8 +672,8 @@ input TimeFrameCreateInput { from: Date! to: Date note: String - lendingStationID: ID! - cargoBikeID: ID! + lendingStationId: ID! + cargoBikeId: ID! } input TimeFrameUpdateInput {