From 91a20704afed06ed72e87b24375d6fe3d7009540 Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Thu, 17 Sep 2020 16:34:17 +0200 Subject: [PATCH] src/resolvers: added resolvers and lendingStationAPI --- src/datasources/db/lendingstationAPI.ts | 35 ++++++++++++++++++++---- src/resolvers/lendingstationResolvers.ts | 14 ++++++++++ src/schema/type-defs.ts | 3 +- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/datasources/db/lendingstationAPI.ts b/src/datasources/db/lendingstationAPI.ts index ddf0ec8..fc75017 100644 --- a/src/datasources/db/lendingstationAPI.ts +++ b/src/datasources/db/lendingstationAPI.ts @@ -10,6 +10,30 @@ export class LendingStationAPI extends DataSource { this.connection = getConnection(); } + async getLendingStationById ({ id }: { id: any }) { + return await this.connection.manager + .createQueryBuilder() + .select('lendingStation') + .from(LendingStation, 'lendingStation') + .where('lendingStation.id = :id', { id: id }) + .getOne(); + } + + /** + * get all lendingStations + */ + async getLendingStations () { + return await this.connection.manager + .createQueryBuilder() + .select('lendingStation') + .from(LendingStation, 'lendingStation') + .getMany() || new GraphQLError('Internal Server Error: could not query data from table lendingStation'); + } + + /** + * creates new lendingStation and returns new lendingStation with its new id + * @param param0 new lendingStation + */ async createLendingStation ({ lendingStation }:{ lendingStation: any }) { console.log(lendingStation); const inserts = await this.connection.manager @@ -24,6 +48,10 @@ export class LendingStationAPI extends DataSource { return newLendingStaion; } + /** + * updates lendingStation and return updated lendingStation + * @param param0 lendingStation to be updated + */ async updateLendingStation ({ lendingStation }:{ lendingStation: any }) { const oldLendingStation = await this.connection.manager.createQueryBuilder() .select('lendingStation') @@ -37,12 +65,7 @@ export class LendingStationAPI extends DataSource { .set({ ...lendingStation }) .where('id = :id', { id: lendingStation.id }) .execute(); - return await this.connection - .createQueryBuilder() - .select('lendingStation') - .from(LendingStation, 'lendingStation') - .where('lendingStation.id = :id', { id: lendingStation.id }) - .getOne(); + return this.getLendingStationById({ id: lendingStation.id }); } else { return new GraphQLError('ID not in database'); } diff --git a/src/resolvers/lendingstationResolvers.ts b/src/resolvers/lendingstationResolvers.ts index 028c7eb..20cc5fe 100644 --- a/src/resolvers/lendingstationResolvers.ts +++ b/src/resolvers/lendingstationResolvers.ts @@ -4,6 +4,20 @@ import { LendingStation } from '../model/LendingStation'; export default { Query: { + lendingStationById: (_: any, { id }: { id: any }, { dataSources, req }: { dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadBike)) { + return dataSources.lendingStationAPI.getLendingStationById({ id }); + } else { + return new GraphQLError('Insufficient Permissions'); + } + }, + lendingStations: (_: any, __: any, { dataSources, req }: { dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadBike)) { + return dataSources.lendingStationAPI.getLendingStations(); + } else { + return new GraphQLError('Insufficient Permissions'); + } + } }, Mutation: { createLendingStation: (_: any, { lendingStation }:{ lendingStation: LendingStation }, { dataSources, req }:{dataSources: any, req: any }) => { diff --git a/src/schema/type-defs.ts b/src/schema/type-defs.ts index 9fa24ef..1fc2d52 100644 --- a/src/schema/type-defs.ts +++ b/src/schema/type-defs.ts @@ -574,8 +574,9 @@ input AddressUpdateInput { type Query { cargoBikeById(id:ID!): CargoBike - "!!!!" + "returns all cargoBikes" cargoBikes: [CargoBike]! + "not important, you can just use providerById {cargoBikes}" cargoBikesByProvider(providerId:ID!): [CargoBike]! providerById(id:ID!): Provider providers: [Provider]!