TimeFrame.cargoBike/.lendingStation implemented

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

@ -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);

@ -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];
}

@ -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: {

@ -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 {

Loading…
Cancel
Save