From 297b5592e56e47a97a9cd8d4565bbce83d10de17 Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Sun, 20 Sep 2020 14:04:44 +0200 Subject: [PATCH] implemented bike lendingstation relation --- src/datasources/db/cargobikeAPI.ts | 13 +++++++++++-- src/datasources/db/lendingstationAPI.ts | 6 +++--- src/model/CargoBike.ts | 3 ++- src/model/LendingStation.ts | 4 +++- src/schema/type-defs.ts | 8 ++++++-- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/datasources/db/cargobikeAPI.ts b/src/datasources/db/cargobikeAPI.ts index ee94e41..bab71f0 100644 --- a/src/datasources/db/cargobikeAPI.ts +++ b/src/datasources/db/cargobikeAPI.ts @@ -30,7 +30,7 @@ export class CargoBikeAPI extends DataSource { * @param param0 id of bike */ async findCargoBikeById ({ id }:{id: any}) { - return await this.connection.manager.getRepository(CargoBike).findOne({ id: id }); + return (await this.connection.manager.getRepository(CargoBike).findByIds([id], { relations: ['lendingStation'] }))[0]; /* .createQueryBuilder() .select('cargoBike') .from(CargoBike, 'cargoBike') @@ -42,19 +42,28 @@ export class CargoBikeAPI extends DataSource { * Updates CargoBike and return updated cargoBike * @param param0 cargoBike to be updated */ - async updateCargoBike ({ cargoBike }:{ cargoBike: CargoBike }) { + async updateCargoBike ({ cargoBike }:{ cargoBike: any }) { const bike = await this.connection.manager.createQueryBuilder() .select('cargoBike') .from(CargoBike, 'cargoBike') .where('cargoBike.id = :id', { id: cargoBike.id }) .getOne(); if (bike) { + const lendingStationId = cargoBike.lendingStationId; + delete cargoBike.lendingStationId; await this.connection.manager .createQueryBuilder() .update(CargoBike) .set({ ...cargoBike }) .where('id = :id', { id: bike.id }) .execute(); + if (lendingStationId || lendingStationId === null) { + await this.connection.getRepository(CargoBike) + .createQueryBuilder() + .relation(CargoBike, 'lendingStation') + .of(cargoBike.id) + .set(lendingStationId); + } return await this.findCargoBikeById({ id: bike.id }); } else { return new GraphQLError('ID not in database'); diff --git a/src/datasources/db/lendingstationAPI.ts b/src/datasources/db/lendingstationAPI.ts index fc75017..c9b13d5 100644 --- a/src/datasources/db/lendingstationAPI.ts +++ b/src/datasources/db/lendingstationAPI.ts @@ -1,6 +1,7 @@ import { DataSource } from 'apollo-datasource'; import { GraphQLError } from 'graphql'; import { Connection, getConnection } from 'typeorm'; +import { CargoBike } from '../../model/CargoBike'; import { LendingStation } from '../../model/LendingStation'; export class LendingStationAPI extends DataSource { @@ -24,9 +25,8 @@ export class LendingStationAPI extends DataSource { */ async getLendingStations () { return await this.connection.manager - .createQueryBuilder() - .select('lendingStation') - .from(LendingStation, 'lendingStation') + .createQueryBuilder(LendingStation, 'lendingStation') + .leftJoinAndSelect('CargoBike', 'cargoBike', 'cargoBike.lendingStation = lendingStation.id')// ('lendingStation.cargoBikes', 'cargoBike.lendingStation', 'cargoBike', 'cargoBike.lendingStationId = lendingStation.id') .getMany() || new GraphQLError('Internal Server Error: could not query data from table lendingStation'); } diff --git a/src/model/CargoBike.ts b/src/model/CargoBike.ts index 8472442..e012c9d 100644 --- a/src/model/CargoBike.ts +++ b/src/model/CargoBike.ts @@ -131,7 +131,8 @@ export class CargoBike extends Bike { // This relation is a little redundant because one could also check all LoanPeriods for current station @ManyToOne(type => LendingStation, lendingStation => lendingStation.cargoBikes, { - nullable: true + nullable: true, + eager: true }) lendingStation: LendingStation; diff --git a/src/model/LendingStation.ts b/src/model/LendingStation.ts index 642c5d5..21af80d 100644 --- a/src/model/LendingStation.ts +++ b/src/model/LendingStation.ts @@ -23,7 +23,9 @@ export class LendingStation { @OneToMany(type => LoanPeriod, loanPeriod => loanPeriod.lendingStation) loanPeriods: LoanPeriod[]; - @OneToMany(type => CargoBike, cargoBike => cargoBike.lendingStation) + @OneToMany(type => CargoBike, cargoBike => cargoBike.lendingStation, { + eager: false + }) cargoBikes: CargoBike[]; @ManyToOne(type => Organization, organization => organization.lendingStations) diff --git a/src/schema/type-defs.ts b/src/schema/type-defs.ts index 19f9469..1323f8b 100644 --- a/src/schema/type-defs.ts +++ b/src/schema/type-defs.ts @@ -39,7 +39,7 @@ type CargoBike { provider: Provider coordinator: Participant insuranceData: InsuranceData! - lendingstation: LendingStation + lendingStation: LendingStation taxes: Taxes "null if not locked by other user" lockedBy: ID @@ -104,7 +104,7 @@ input CargoBikeUpdateInput { dimensionsAndLoad: DimensionsAndLoadUpdateInput "Refers to equipment that is not unique. See kommentierte info tabelle -> Fragen -> Frage 2" otherEquipment: [String] - + lendingStationId: ID "Sticker State" stickerBikeNameState: StickerBikeNameState note: String @@ -518,6 +518,9 @@ type LendingStation { address: Address! loanTimes: LoanTimes loanPeriods: [LoanPeriod]! + cargoBikes: [CargoBike] + "Totola Amount of cargoBikes currently assigned to the lending station" + numCargoBikes: Int! } input LendingStationCreateInput { @@ -535,6 +538,7 @@ input LendingStationUpdateInput { address: AddressUpdateInput loanTimes: LoanTimesInput loanPeriods: [LoanPeriodUpdateInput] + } """