use ids insteaf of objects

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

@ -118,11 +118,6 @@ export class CargoBikeAPI extends DataSource {
.values([cargoBike]) .values([cargoBike])
.returning('*') .returning('*')
.execute(); .execute();
await entityManager.getRepository(CargoBike)
.createQueryBuilder('cargobike')
.relation(CargoBike, 'provider')
.of(inserts.identifiers[0].id)
.set(cargoBike?.providerId);
cargoBike?.equipmentTypeIds && await entityManager.getRepository(CargoBike) cargoBike?.equipmentTypeIds && await entityManager.getRepository(CargoBike)
.createQueryBuilder('cargobike') .createQueryBuilder('cargobike')
.relation(CargoBike, 'equipmentTypeIds') .relation(CargoBike, 'equipmentTypeIds')
@ -372,7 +367,7 @@ export class CargoBikeAPI extends DataSource {
.getOne(); .getOne();
} }
async eqiupmentTypes (offset: number, limit: number) { async equipmentTypes (offset: number, limit: number) {
return await this.connection.getRepository(EquipmentType) return await this.connection.getRepository(EquipmentType)
.createQueryBuilder('et') .createQueryBuilder('et')
.select() .select()

@ -41,18 +41,22 @@ export class LendingStationAPI extends DataSource {
* @param id of cargoBike * @param id of cargoBike
*/ */
async lendingStationByCargoBikeId (id: number) { async lendingStationByCargoBikeId (id: number) {
return (await this.connection.getRepository(TimeFrame) return await this.connection.getRepository(TimeFrame)
.createQueryBuilder('timeframe') .createQueryBuilder('tf')
.leftJoinAndSelect('timeframe.lendingStation', 'lendingStation') .relation(TimeFrame, 'lendingStationId')
.where('timeframe."cargoBikeId" = :id', { id: id }) .of((await this.connection.getRepository(TimeFrame)// TODO maybe this can be done with a sub query
.andWhere('timeframe."dateRange" && daterange(CURRENT_DATE,CURRENT_DATE,\'[]\')') .createQueryBuilder('tf')
.getOne())?.lendingStation; .select()
.where('"cargoBikeId" = :cid', { cid: id })
.andWhere('"dateRange" && daterange(CURRENT_DATE,CURRENT_DATE,\'[]\')')
.getOne())?.id)
.loadOne();
} }
async lendingStationByTimeFrameId (id: number) { async lendingStationByTimeFrameId (id: number) {
return await this.connection.getRepository(LendingStation) return await this.connection.getRepository(LendingStation)
.createQueryBuilder('lendingStation') .createQueryBuilder('lendingStation')
.relation(TimeFrame, 'lendingStation') .relation(TimeFrame, 'lendingStationId')
.of(id) .of(id)
.loadOne(); .loadOne();
} }
@ -198,7 +202,7 @@ export class LendingStationAPI extends DataSource {
.returning('*') .returning('*')
.values([timeFrame]) .values([timeFrame])
.execute(); .execute();
await entityManager.getRepository(TimeFrame) /* await entityManager.getRepository(TimeFrame)
.createQueryBuilder() .createQueryBuilder()
.relation(TimeFrame, 'cargoBike') .relation(TimeFrame, 'cargoBike')
.of(inserts.identifiers[0].id) .of(inserts.identifiers[0].id)
@ -208,6 +212,7 @@ export class LendingStationAPI extends DataSource {
.relation(TimeFrame, 'lendingStation') .relation(TimeFrame, 'lendingStation')
.of(inserts.identifiers[0].id) .of(inserts.identifiers[0].id)
.set(timeFrame.lendingStationId); .set(timeFrame.lendingStationId);
*/
}); });
} catch (e) { } catch (e) {
if (e instanceof UserInputError) { if (e instanceof UserInputError) {

@ -40,12 +40,16 @@ export class ParticipantAPI extends DataSource {
} }
async participantByCargoBikeId (id:number) { async participantByCargoBikeId (id:number) {
return await this.connection.getRepository(Participant) return await this.connection.getRepository(Engagement)
.createQueryBuilder('participant') .createQueryBuilder('e')
.leftJoinAndSelect('participant.cargoBike', 'cargoBike') .relation(Engagement, 'participantId')
.where('"cargoBike".id = :id', { id: id }) .of((await this.connection.getRepository(Engagement)// TODO do this with a sub query
.andWhere('"cargoBike"."participantId" = participant.id') .createQueryBuilder('e')
.getOne(); .select()
.where('"dateRange" && daterange(CURRENT_DATE,CURRENT_DATE,\'[]\')')
.andWhere('"cargoBikeId" = :id', { id: id })
.getOne())?.id
).loadOne();
} }
async engagementByParticipantId (id: number) { async engagementByParticipantId (id: number) {
@ -128,7 +132,7 @@ export class ParticipantAPI extends DataSource {
} }
/** /**
* creates participant and creates realtion to given contactInformation * creates participant and creates relation to given contactInformation
* @param participant to be created * @param participant to be created
*/ */
async createParticipant (participant: any) { async createParticipant (participant: any) {
@ -142,7 +146,7 @@ export class ParticipantAPI extends DataSource {
.returning('*') .returning('*')
.execute(); .execute();
}); });
return this.getParticipantById(inserts.identifiers[0].id); return this.getParticipantById(inserts?.identifiers[0].id);
} }
async createEngagement (engagement: any) { async createEngagement (engagement: any) {
@ -167,7 +171,7 @@ export class ParticipantAPI extends DataSource {
.returning('*') .returning('*')
.execute(); .execute();
}); });
return this.engagementById(inserts.identifiers[0].id); return this.engagementById(inserts?.identifiers[0].id);
} }
async createEngagementType (engagementType: any) { async createEngagementType (engagementType: any) {

@ -40,7 +40,7 @@ export class ProviderAPI extends DataSource {
async providerByCargoBikeId (id: number) { async providerByCargoBikeId (id: number) {
return await this.connection.getRepository(CargoBike) return await this.connection.getRepository(CargoBike)
.createQueryBuilder('cb') .createQueryBuilder('cb')
.relation(CargoBike, 'provider') .relation(CargoBike, 'providerId')
.of(id) .of(id)
.loadOne(); .loadOne();
} }

@ -181,10 +181,10 @@ export class CargoBike implements Lockable {
}) })
note: string; note: string;
@ManyToOne(type => Provider, { @ManyToOne(type => Provider, provider => provider.cargoBikeIds, {
nullable: true nullable: true
}) })
provider: Provider; providerId: number;
@OneToMany(type => BikeEvent, bikeEvent => bikeEvent.cargoBikeId, { @OneToMany(type => BikeEvent, bikeEvent => bikeEvent.cargoBikeId, {
nullable: true, nullable: true,
@ -196,7 +196,7 @@ export class CargoBike implements Lockable {
@Column(type => InsuranceData) @Column(type => InsuranceData)
insuranceData: InsuranceData; insuranceData: InsuranceData;
@OneToMany(type => TimeFrame, loanPeriod => loanPeriod.cargoBike, { @OneToMany(type => TimeFrame, timeFrame => timeFrame.cargoBikeId, {
nullable: true nullable: true
}) })
timeFrames: TimeFrame[]; timeFrames: TimeFrame[];

@ -3,20 +3,6 @@ import { CargoBike, Lockable } from './CargoBike';
@Entity() @Entity()
export class Equipment implements Lockable { export class Equipment implements Lockable {
setValues ({ id, serialNo, title, description, cargoBike }: {
id: number,
serialNo: string,
title: string,
description: string,
cargoBike: CargoBike
}) {
this.id = id;
this.serialNo = serialNo;
this.title = title;
this.description = description;
this.cargoBike = cargoBike;
}
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()
id: number; id: number;

@ -56,7 +56,7 @@ export class LendingStation implements Lockable {
@Column(type => LoanPeriod) @Column(type => LoanPeriod)
loanPeriod: LoanPeriod; loanPeriod: LoanPeriod;
@OneToMany(type => TimeFrame, timeFrame => timeFrame.lendingStation) @OneToMany(type => TimeFrame, timeFrame => timeFrame.lendingStationId)
timeFrames: TimeFrame[]; timeFrames: TimeFrame[];
@ManyToOne(type => Organisation, organization => organization.lendingStations) @ManyToOne(type => Organisation, organization => organization.lendingStations)

@ -55,8 +55,8 @@ export class Provider implements Lockable {
}) })
organisationId: number; organisationId: number;
@OneToMany(type => CargoBike, cargoBike => cargoBike.provider) @OneToMany(type => CargoBike, cargoBike => cargoBike.providerId)
cargoBikes: CargoBike[]; cargoBikeIds: number[];
@Column({ @Column({
nullable: true, nullable: true,

@ -1,4 +1,4 @@
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm'; import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
import { LendingStation } from './LendingStation'; import { LendingStation } from './LendingStation';
import { CargoBike, Lockable } from './CargoBike'; import { CargoBike, Lockable } from './CargoBike';
@ -16,7 +16,10 @@ export class TimeFrame implements Lockable {
dateRange: Date[]; dateRange: Date[];
@ManyToOne(type => LendingStation, lendingStation => lendingStation.timeFrames) @ManyToOne(type => LendingStation, lendingStation => lendingStation.timeFrames)
lendingStation: LendingStation; @JoinColumn({
name: 'lendingStationId'
})
lendingStationId: number;
@Column({ @Column({
nullable: true nullable: true
@ -24,7 +27,10 @@ export class TimeFrame implements Lockable {
note: string; note: string;
@ManyToOne(type => CargoBike, cargoBike => cargoBike.timeFrames) @ManyToOne(type => CargoBike, cargoBike => cargoBike.timeFrames)
cargoBike: CargoBike; @JoinColumn({
name: 'cargoBikeId'
})
cargoBikeId: number;
@Column({ @Column({
nullable: true, nullable: true,

Loading…
Cancel
Save