diff --git a/src/datasources/db/lendingstationAPI.ts b/src/datasources/db/lendingstationAPI.ts index ed5fc44..c371cff 100644 --- a/src/datasources/db/lendingstationAPI.ts +++ b/src/datasources/db/lendingstationAPI.ts @@ -1,6 +1,6 @@ import { DataSource } from 'apollo-datasource'; import { GraphQLError } from 'graphql'; -import { Connection, getConnection } from 'typeorm'; +import { Connection, EntityManager, getConnection } from 'typeorm'; import { CargoBike } from '../../model/CargoBike'; import { LendingStation } from '../../model/LendingStation'; import { TimeFrame } from '../../model/TimeFrame'; @@ -42,6 +42,15 @@ export class LendingStationAPI extends DataSource { .getOne().catch(() => { return null; }); } + async timeFrames (offset: number, limit: number) { + return await this.connection.getRepository(TimeFrame) + .createQueryBuilder('timeframe') + .select() + .offset(offset) + .limit(limit) + .getMany() || []; + } + async timeFramesByLendingStationId (id: number) { return await this.connection.getRepository(TimeFrame) .createQueryBuilder('timeFrame') @@ -115,4 +124,32 @@ export class LendingStationAPI extends DataSource { return new GraphQLError('ID not in database'); } } + + 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); + }); + 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 201c732..7b43d0f 100644 --- a/src/resolvers/lendingstationResolvers.ts +++ b/src/resolvers/lendingstationResolvers.ts @@ -17,6 +17,13 @@ export default { } else { return new GraphQLError('Insufficient Permissions'); } + }, + timeframes: (_: any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadBike)) { + return dataSources.lendingStationAPI.timeFrames(offset, limit); + } else { + return new GraphQLError('Insufficient Permissions'); + } } }, LendingStation: { @@ -33,6 +40,14 @@ export default { return dataSources.lendingStationAPI.cargoBikesByLendingStationId(parent.id); } }, + TimeFrame: { + from (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { + 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(')', ''); + } + }, Mutation: { createLendingStation: (_: any, { lendingStation }:{ lendingStation: LendingStation }, { dataSources, req }:{dataSources: any, req: any }) => { if (req.permissions.includes(Permission.WriteBike)) { @@ -47,6 +62,13 @@ export default { } else { return new GraphQLError('Insufficient Permissions'); } + }, + createTimeFrame: (_: any, { timeFrame }:{ timeFrame: LendingStation }, { dataSources, req }:{dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.WriteBike)) { + return dataSources.lendingStationAPI.createTimeFrame(timeFrame); + } else { + return new GraphQLError('Insufficient Permissions'); + } } } }; diff --git a/src/schema/type-defs.ts b/src/schema/type-defs.ts index 6a1fc29..4b4da45 100644 --- a/src/schema/type-defs.ts +++ b/src/schema/type-defs.ts @@ -656,7 +656,7 @@ input LoanPeriodsInput { times: [String] } -"(dt. Zeitscheibe)" +"(dt. Zeitscheibe) When was a bike where" type TimeFrame { id: ID! from: Date! @@ -667,11 +667,11 @@ type TimeFrame { } input TimeFrameCreateInput { - from: Date + from: Date! to: Date note: String - lendingStationID: LendingStationCreateInput - cargoBikeID: CargoBikeCreateInput + lendingStationID: ID! + cargoBikeID: ID! } input TimeFrameUpdateInput { @@ -679,8 +679,9 @@ input TimeFrameUpdateInput { from: Date to: Date note: String - lendingStation: LendingStationUpdateInput - cargoBike: CargoBikeUpdateInput + lendingStation: ID + cargoBike: ID + keepLock: Boolean } type Address { @@ -719,6 +720,7 @@ type Query { participants(offset: Int!, limit: Int!): [ Participant]! lendingStationById(id:ID!): LendingStation lendingStations(offset: Int!, limit: Int!): [LendingStation]! + timeframes(offset: Int!, limit: Int!): [TimeFrame]! contactInformation(offset: Int!, limit: Int!): [ContactInformation]! "returns BikeEvent with CargoBike" bikeEventById(id:ID!): BikeEvent! @@ -745,6 +747,7 @@ type Mutation { createLendingStation(lendingStation: LendingStationCreateInput): LendingStation! "updates lendingStation of given ID with supplied fields and returns updated lendingStation" updateLendingStation(lendingstation: LendingStationUpdateInput!): LendingStation! + createTimeFrame(timeFrame: TimeFrameCreateInput!): TimeFrame! "creates new BikeEvent" createBikeEvent(bikeEvent: BikeEventCreateInput): BikeEvent! "create participant"