resolver cargoBike.timeFramesimplemented

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

@ -1,4 +1,5 @@
import { DataSource } from 'apollo-datasource'; import { DataSource } from 'apollo-datasource';
import { ApolloError } from 'apollo-server';
import { GraphQLError } from 'graphql'; import { GraphQLError } from 'graphql';
import { Connection, EntityManager, getConnection } from 'typeorm'; import { Connection, EntityManager, getConnection } from 'typeorm';
import { CargoBike } from '../../model/CargoBike'; import { CargoBike } from '../../model/CargoBike';
@ -59,6 +60,14 @@ export class LendingStationAPI extends DataSource {
.getMany() || []; .getMany() || [];
} }
async timeFramesByCargoBikeId (id: number) {
return await this.connection.getRepository(CargoBike)
.createQueryBuilder('cargobike')
.relation(CargoBike, 'timeFrames')
.of(id)
.loadMany();
}
async timeFramesByLendingStationId (id: number) { async timeFramesByLendingStationId (id: number) {
return await this.connection.getRepository(TimeFrame) return await this.connection.getRepository(TimeFrame)
.createQueryBuilder('timeFrame') .createQueryBuilder('timeFrame')
@ -134,6 +143,7 @@ export class LendingStationAPI extends DataSource {
} }
async createTimeFrame (timeFrame: any) { async createTimeFrame (timeFrame: any) {
// TODO check overlapping time frames
let inserts: any; let inserts: any;
try { try {
await this.connection.transaction(async (entityManager: EntityManager) => { await this.connection.transaction(async (entityManager: EntityManager) => {
@ -159,7 +169,8 @@ export class LendingStationAPI extends DataSource {
.set(timeFrame.lendingStationId); .set(timeFrame.lendingStationId);
}); });
} catch (e) { } catch (e) {
return new GraphQLError('Transaction could not be completed'); // TODO: throw diffrent error when date input is wrong
return new ApolloError('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];

@ -127,7 +127,7 @@ export class CargoBike extends Bike implements Lockable {
@OneToMany(type => TimeFrame, loanPeriod => loanPeriod.cargoBike, { @OneToMany(type => TimeFrame, loanPeriod => loanPeriod.cargoBike, {
nullable: true nullable: true
}) })
loanPeriods: TimeFrame[]; timeFrames: TimeFrame[];
// This relation is a little redundant because one could also check all LoanPeriods for current station // This relation is a little redundant because one could also check all LoanPeriods for current station
@ManyToOne(type => LendingStation, lendingStation => lendingStation.cargoBikes, { @ManyToOne(type => LendingStation, lendingStation => lendingStation.cargoBikes, {

@ -24,6 +24,6 @@ export class TimeFrame {
}) })
note: string; note: string;
@ManyToOne(type => CargoBike, cargoBike => cargoBike.loanPeriods) @ManyToOne(type => CargoBike, cargoBike => cargoBike.timeFrames)
cargoBike: CargoBike; cargoBike: CargoBike;
} }

@ -57,6 +57,9 @@ export default {
}, },
lockedBy (): any { lockedBy (): any {
return null; return null;
},
timeFrames (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
return dataSources.lendingStationAPI.timeFramesByCargoBikeId(parent.id, req, dataSources);
} }
}, },
Equipment: { Equipment: {

@ -44,6 +44,7 @@ type CargoBike {
lendingStation: LendingStation lendingStation: LendingStation
taxes: Taxes taxes: Taxes
engagement(offset: Int!, limit: Int!): [Engagement] engagement(offset: Int!, limit: Int!): [Engagement]
timeFrames: [TimeFrame]
isLocked: Boolean! isLocked: Boolean!
"null if not locked by other user" "null if not locked by other user"
lockedBy: ID lockedBy: ID

Loading…
Cancel
Save