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
*/
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');

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

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

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

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

Loading…
Cancel
Save