createTimeFrame implemented

pull/14/head
leonnicolas 4 years ago
parent e1a90670f6
commit 85d8b07856
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

@ -1,6 +1,6 @@
import { DataSource } from 'apollo-datasource'; import { DataSource } from 'apollo-datasource';
import { GraphQLError } from 'graphql'; import { GraphQLError } from 'graphql';
import { Connection, getConnection } from 'typeorm'; import { Connection, EntityManager, getConnection } from 'typeorm';
import { CargoBike } from '../../model/CargoBike'; import { CargoBike } from '../../model/CargoBike';
import { LendingStation } from '../../model/LendingStation'; import { LendingStation } from '../../model/LendingStation';
import { TimeFrame } from '../../model/TimeFrame'; import { TimeFrame } from '../../model/TimeFrame';
@ -42,6 +42,15 @@ export class LendingStationAPI extends DataSource {
.getOne().catch(() => { return null; }); .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) { async timeFramesByLendingStationId (id: number) {
return await this.connection.getRepository(TimeFrame) return await this.connection.getRepository(TimeFrame)
.createQueryBuilder('timeFrame') .createQueryBuilder('timeFrame')
@ -115,4 +124,32 @@ export class LendingStationAPI extends DataSource {
return new GraphQLError('ID not in database'); 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];
}
} }

@ -17,6 +17,13 @@ export default {
} else { } else {
return new GraphQLError('Insufficient Permissions'); 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: { LendingStation: {
@ -33,6 +40,14 @@ export default {
return dataSources.lendingStationAPI.cargoBikesByLendingStationId(parent.id); 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: { Mutation: {
createLendingStation: (_: any, { lendingStation }:{ lendingStation: LendingStation }, { dataSources, req }:{dataSources: any, req: any }) => { createLendingStation: (_: any, { lendingStation }:{ lendingStation: LendingStation }, { dataSources, req }:{dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) { if (req.permissions.includes(Permission.WriteBike)) {
@ -47,6 +62,13 @@ export default {
} else { } else {
return new GraphQLError('Insufficient Permissions'); 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');
}
} }
} }
}; };

@ -656,7 +656,7 @@ input LoanPeriodsInput {
times: [String] times: [String]
} }
"(dt. Zeitscheibe)" "(dt. Zeitscheibe) When was a bike where"
type TimeFrame { type TimeFrame {
id: ID! id: ID!
from: Date! from: Date!
@ -667,11 +667,11 @@ type TimeFrame {
} }
input TimeFrameCreateInput { input TimeFrameCreateInput {
from: Date from: Date!
to: Date to: Date
note: String note: String
lendingStationID: LendingStationCreateInput lendingStationID: ID!
cargoBikeID: CargoBikeCreateInput cargoBikeID: ID!
} }
input TimeFrameUpdateInput { input TimeFrameUpdateInput {
@ -679,8 +679,9 @@ input TimeFrameUpdateInput {
from: Date from: Date
to: Date to: Date
note: String note: String
lendingStation: LendingStationUpdateInput lendingStation: ID
cargoBike: CargoBikeUpdateInput cargoBike: ID
keepLock: Boolean
} }
type Address { type Address {
@ -719,6 +720,7 @@ type Query {
participants(offset: Int!, limit: Int!): [ Participant]! participants(offset: Int!, limit: Int!): [ Participant]!
lendingStationById(id:ID!): LendingStation lendingStationById(id:ID!): LendingStation
lendingStations(offset: Int!, limit: Int!): [LendingStation]! lendingStations(offset: Int!, limit: Int!): [LendingStation]!
timeframes(offset: Int!, limit: Int!): [TimeFrame]!
contactInformation(offset: Int!, limit: Int!): [ContactInformation]! contactInformation(offset: Int!, limit: Int!): [ContactInformation]!
"returns BikeEvent with CargoBike" "returns BikeEvent with CargoBike"
bikeEventById(id:ID!): BikeEvent! bikeEventById(id:ID!): BikeEvent!
@ -745,6 +747,7 @@ type Mutation {
createLendingStation(lendingStation: LendingStationCreateInput): LendingStation! createLendingStation(lendingStation: LendingStationCreateInput): LendingStation!
"updates lendingStation of given ID with supplied fields and returns updated lendingStation" "updates lendingStation of given ID with supplied fields and returns updated lendingStation"
updateLendingStation(lendingstation: LendingStationUpdateInput!): LendingStation! updateLendingStation(lendingstation: LendingStationUpdateInput!): LendingStation!
createTimeFrame(timeFrame: TimeFrameCreateInput!): TimeFrame!
"creates new BikeEvent" "creates new BikeEvent"
createBikeEvent(bikeEvent: BikeEventCreateInput): BikeEvent! createBikeEvent(bikeEvent: BikeEventCreateInput): BikeEvent!
"create participant" "create participant"

Loading…
Cancel
Save