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 { Equipment } from '../../model/Equipment';
import { Engagement } from '../../model/Engagement'; import { Engagement } from '../../model/Engagement';
import { Provider } from '../../model/Provider'; import { Provider } from '../../model/Provider';
import { TimeFrame } from '../../model/TimeFrame';
/** /**
* extended datasource to feed resolvers with data about cargoBikes * extended datasource to feed resolvers with data about cargoBikes
@ -56,6 +57,14 @@ export class CargoBikeAPI extends DataSource {
.loadMany(); .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) { async lockCargoBike (id: number, req: any, dataSources: any) {
if (await this.lockEntity(CargoBike, 'cargobike', id, req, dataSources)) { if (await this.lockEntity(CargoBike, 'cargobike', id, req, dataSources)) {
return this.findCargoBikeById(id); return this.findCargoBikeById(id);

@ -42,6 +42,14 @@ export class LendingStationAPI extends DataSource {
.getOne().catch(() => { return null; }); .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) { async timeFrames (offset: number, limit: number) {
return await this.connection.getRepository(TimeFrame) return await this.connection.getRepository(TimeFrame)
.createQueryBuilder('timeframe') .createQueryBuilder('timeframe')
@ -127,28 +135,32 @@ export class LendingStationAPI extends DataSource {
async createTimeFrame (timeFrame: any) { async createTimeFrame (timeFrame: any) {
let inserts: any; let inserts: any;
await this.connection.transaction(async (entityManager: EntityManager) => { try {
if (timeFrame.to === undefined) { await this.connection.transaction(async (entityManager: EntityManager) => {
timeFrame.to = ''; if (timeFrame.to === undefined) {
} timeFrame.to = '';
timeFrame.dateRange = '[' + timeFrame.from + ',' + timeFrame.to + ')'; }
inserts = await entityManager.getRepository(TimeFrame) timeFrame.dateRange = '[' + timeFrame.from + ',' + timeFrame.to + ')';
.createQueryBuilder('timeframe') inserts = await entityManager.getRepository(TimeFrame)
.insert() .createQueryBuilder('timeframe')
.returning('*') .insert()
.values([timeFrame]) .returning('*')
.execute(); .values([timeFrame])
await entityManager.getRepository(TimeFrame) .execute();
.createQueryBuilder() await entityManager.getRepository(TimeFrame)
.relation(TimeFrame, 'cargoBike') .createQueryBuilder()
.of(inserts.identifiers[0].id) .relation(TimeFrame, 'cargoBike')
.set(timeFrame.cargoBikeId); .of(inserts.identifiers[0].id)
await entityManager.getRepository(TimeFrame) .set(timeFrame.cargoBikeId);
.createQueryBuilder() await entityManager.getRepository(TimeFrame)
.relation(TimeFrame, 'lendingStation') .createQueryBuilder()
.of(inserts.identifiers[0].id) .relation(TimeFrame, 'lendingStation')
.set(timeFrame.lendingStationId); .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; inserts.generatedMaps[0].id = inserts.identifiers[0].id;
return inserts.generatedMaps[0]; return inserts.generatedMaps[0];
} }

@ -45,7 +45,14 @@ export default {
return (parent.dateRange as string).split(',')[0].replace('[', ''); return (parent.dateRange as string).split(',')[0].replace('[', '');
}, },
to (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { 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: { Mutation: {

@ -659,7 +659,9 @@ input LoanPeriodsInput {
"(dt. Zeitscheibe) When was a bike where" "(dt. Zeitscheibe) When was a bike where"
type TimeFrame { type TimeFrame {
id: ID! id: ID!
"format YYYY-MM-dd"
from: Date! from: Date!
"formmat YYYY-MM-dd"
to: Date to: Date
note: String note: String
lendingStation: LendingStation! lendingStation: LendingStation!
@ -670,8 +672,8 @@ input TimeFrameCreateInput {
from: Date! from: Date!
to: Date to: Date
note: String note: String
lendingStationID: ID! lendingStationId: ID!
cargoBikeID: ID! cargoBikeId: ID!
} }
input TimeFrameUpdateInput { input TimeFrameUpdateInput {

Loading…
Cancel
Save