From e1e2302a1a2218dc2d288821459bdf644c5c8beb Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Thu, 24 Sep 2020 19:45:58 +0200 Subject: [PATCH] resolver cargoBike.timeFramesimplemented --- src/datasources/db/lendingstationAPI.ts | 13 ++++++++++++- src/model/CargoBike.ts | 2 +- src/model/TimeFrame.ts | 2 +- src/resolvers/cargobikeResolver.ts | 3 +++ src/schema/type-defs.ts | 1 + 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/datasources/db/lendingstationAPI.ts b/src/datasources/db/lendingstationAPI.ts index 614c704..fefa78c 100644 --- a/src/datasources/db/lendingstationAPI.ts +++ b/src/datasources/db/lendingstationAPI.ts @@ -1,4 +1,5 @@ import { DataSource } from 'apollo-datasource'; +import { ApolloError } from 'apollo-server'; import { GraphQLError } from 'graphql'; import { Connection, EntityManager, getConnection } from 'typeorm'; import { CargoBike } from '../../model/CargoBike'; @@ -59,6 +60,14 @@ export class LendingStationAPI extends DataSource { .getMany() || []; } + async timeFramesByCargoBikeId (id: number) { + return await this.connection.getRepository(CargoBike) + .createQueryBuilder('cargobike') + .relation(CargoBike, 'timeFrames') + .of(id) + .loadMany(); + } + async timeFramesByLendingStationId (id: number) { return await this.connection.getRepository(TimeFrame) .createQueryBuilder('timeFrame') @@ -134,6 +143,7 @@ export class LendingStationAPI extends DataSource { } async createTimeFrame (timeFrame: any) { + // TODO check overlapping time frames let inserts: any; try { await this.connection.transaction(async (entityManager: EntityManager) => { @@ -159,7 +169,8 @@ export class LendingStationAPI extends DataSource { .set(timeFrame.lendingStationId); }); } catch (e) { - return new GraphQLError('Transaction could not be completed'); + // TODO: throw diffrent error when date input is wrong + return new ApolloError('Transaction could not be completed'); } inserts.generatedMaps[0].id = inserts.identifiers[0].id; return inserts.generatedMaps[0]; diff --git a/src/model/CargoBike.ts b/src/model/CargoBike.ts index 5da5190..459e73c 100644 --- a/src/model/CargoBike.ts +++ b/src/model/CargoBike.ts @@ -127,7 +127,7 @@ export class CargoBike extends Bike implements Lockable { @OneToMany(type => TimeFrame, loanPeriod => loanPeriod.cargoBike, { nullable: true }) - loanPeriods: TimeFrame[]; + timeFrames: TimeFrame[]; // This relation is a little redundant because one could also check all LoanPeriods for current station @ManyToOne(type => LendingStation, lendingStation => lendingStation.cargoBikes, { diff --git a/src/model/TimeFrame.ts b/src/model/TimeFrame.ts index 78115da..51d261a 100644 --- a/src/model/TimeFrame.ts +++ b/src/model/TimeFrame.ts @@ -24,6 +24,6 @@ export class TimeFrame { }) note: string; - @ManyToOne(type => CargoBike, cargoBike => cargoBike.loanPeriods) + @ManyToOne(type => CargoBike, cargoBike => cargoBike.timeFrames) cargoBike: CargoBike; } diff --git a/src/resolvers/cargobikeResolver.ts b/src/resolvers/cargobikeResolver.ts index ae98c3d..cdc2140 100644 --- a/src/resolvers/cargobikeResolver.ts +++ b/src/resolvers/cargobikeResolver.ts @@ -57,6 +57,9 @@ export default { }, lockedBy (): any { return null; + }, + timeFrames (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { + return dataSources.lendingStationAPI.timeFramesByCargoBikeId(parent.id, req, dataSources); } }, Equipment: { diff --git a/src/schema/type-defs.ts b/src/schema/type-defs.ts index 7827638..37a5a48 100644 --- a/src/schema/type-defs.ts +++ b/src/schema/type-defs.ts @@ -44,6 +44,7 @@ type CargoBike { lendingStation: LendingStation taxes: Taxes engagement(offset: Int!, limit: Int!): [Engagement] + timeFrames: [TimeFrame] isLocked: Boolean! "null if not locked by other user" lockedBy: ID