implemented bike lendingstation relation

pull/11/head
leonnicolas 4 years ago
parent 86eb74fd7f
commit 297b5592e5
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

@ -30,7 +30,7 @@ export class CargoBikeAPI extends DataSource {
* @param param0 id of bike * @param param0 id of bike
*/ */
async findCargoBikeById ({ id }:{id: any}) { 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() /* .createQueryBuilder()
.select('cargoBike') .select('cargoBike')
.from(CargoBike, 'cargoBike') .from(CargoBike, 'cargoBike')
@ -42,19 +42,28 @@ export class CargoBikeAPI extends DataSource {
* Updates CargoBike and return updated cargoBike * Updates CargoBike and return updated cargoBike
* @param param0 cargoBike to be updated * @param param0 cargoBike to be updated
*/ */
async updateCargoBike ({ cargoBike }:{ cargoBike: CargoBike }) { async updateCargoBike ({ cargoBike }:{ cargoBike: any }) {
const bike = await this.connection.manager.createQueryBuilder() const bike = await this.connection.manager.createQueryBuilder()
.select('cargoBike') .select('cargoBike')
.from(CargoBike, 'cargoBike') .from(CargoBike, 'cargoBike')
.where('cargoBike.id = :id', { id: cargoBike.id }) .where('cargoBike.id = :id', { id: cargoBike.id })
.getOne(); .getOne();
if (bike) { if (bike) {
const lendingStationId = cargoBike.lendingStationId;
delete cargoBike.lendingStationId;
await this.connection.manager await this.connection.manager
.createQueryBuilder() .createQueryBuilder()
.update(CargoBike) .update(CargoBike)
.set({ ...cargoBike }) .set({ ...cargoBike })
.where('id = :id', { id: bike.id }) .where('id = :id', { id: bike.id })
.execute(); .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 }); return await this.findCargoBikeById({ id: bike.id });
} else { } else {
return new GraphQLError('ID not in database'); return new GraphQLError('ID not in database');

@ -1,6 +1,7 @@
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, getConnection } from 'typeorm';
import { CargoBike } from '../../model/CargoBike';
import { LendingStation } from '../../model/LendingStation'; import { LendingStation } from '../../model/LendingStation';
export class LendingStationAPI extends DataSource { export class LendingStationAPI extends DataSource {
@ -24,9 +25,8 @@ export class LendingStationAPI extends DataSource {
*/ */
async getLendingStations () { async getLendingStations () {
return await this.connection.manager return await this.connection.manager
.createQueryBuilder() .createQueryBuilder(LendingStation, 'lendingStation')
.select('lendingStation') .leftJoinAndSelect('CargoBike', 'cargoBike', 'cargoBike.lendingStation = lendingStation.id')// ('lendingStation.cargoBikes', 'cargoBike.lendingStation', 'cargoBike', 'cargoBike.lendingStationId = lendingStation.id')
.from(LendingStation, 'lendingStation')
.getMany() || new GraphQLError('Internal Server Error: could not query data from table lendingStation'); .getMany() || new GraphQLError('Internal Server Error: could not query data from table lendingStation');
} }

@ -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 // 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, {
nullable: true nullable: true,
eager: true
}) })
lendingStation: LendingStation; lendingStation: LendingStation;

@ -23,7 +23,9 @@ export class LendingStation {
@OneToMany(type => LoanPeriod, loanPeriod => loanPeriod.lendingStation) @OneToMany(type => LoanPeriod, loanPeriod => loanPeriod.lendingStation)
loanPeriods: LoanPeriod[]; loanPeriods: LoanPeriod[];
@OneToMany(type => CargoBike, cargoBike => cargoBike.lendingStation) @OneToMany(type => CargoBike, cargoBike => cargoBike.lendingStation, {
eager: false
})
cargoBikes: CargoBike[]; cargoBikes: CargoBike[];
@ManyToOne(type => Organization, organization => organization.lendingStations) @ManyToOne(type => Organization, organization => organization.lendingStations)

@ -39,7 +39,7 @@ type CargoBike {
provider: Provider provider: Provider
coordinator: Participant coordinator: Participant
insuranceData: InsuranceData! insuranceData: InsuranceData!
lendingstation: LendingStation lendingStation: LendingStation
taxes: Taxes taxes: Taxes
"null if not locked by other user" "null if not locked by other user"
lockedBy: ID lockedBy: ID
@ -104,7 +104,7 @@ input CargoBikeUpdateInput {
dimensionsAndLoad: DimensionsAndLoadUpdateInput dimensionsAndLoad: DimensionsAndLoadUpdateInput
"Refers to equipment that is not unique. See kommentierte info tabelle -> Fragen -> Frage 2" "Refers to equipment that is not unique. See kommentierte info tabelle -> Fragen -> Frage 2"
otherEquipment: [String] otherEquipment: [String]
lendingStationId: ID
"Sticker State" "Sticker State"
stickerBikeNameState: StickerBikeNameState stickerBikeNameState: StickerBikeNameState
note: String note: String
@ -518,6 +518,9 @@ type LendingStation {
address: Address! address: Address!
loanTimes: LoanTimes loanTimes: LoanTimes
loanPeriods: [LoanPeriod]! loanPeriods: [LoanPeriod]!
cargoBikes: [CargoBike]
"Totola Amount of cargoBikes currently assigned to the lending station"
numCargoBikes: Int!
} }
input LendingStationCreateInput { input LendingStationCreateInput {
@ -535,6 +538,7 @@ input LendingStationUpdateInput {
address: AddressUpdateInput address: AddressUpdateInput
loanTimes: LoanTimesInput loanTimes: LoanTimesInput
loanPeriods: [LoanPeriodUpdateInput] loanPeriods: [LoanPeriodUpdateInput]
} }
""" """

Loading…
Cancel
Save