From f87aa84e86e5df7a5e1505edfdc716de5c9810f9 Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Fri, 18 Sep 2020 17:04:55 +0200 Subject: [PATCH] added some resolvers and paging on resolver cargobikes --- src/datasources/db/cargobikeAPI.ts | 48 ++++++++++++++++++------------ src/model/BikeEvent.ts | 27 +++++++++++++---- src/model/CargoBike.ts | 10 ++++++- src/model/Equipment.ts | 8 +++++ src/model/Organization.ts | 1 + src/model/Taxes.ts | 4 +-- src/resolvers/cargobikeResolver.ts | 23 +++++++++----- src/schema/type-defs.ts | 34 +++++++++++++++------ 8 files changed, 111 insertions(+), 44 deletions(-) diff --git a/src/datasources/db/cargobikeAPI.ts b/src/datasources/db/cargobikeAPI.ts index b195b9a..4575c40 100644 --- a/src/datasources/db/cargobikeAPI.ts +++ b/src/datasources/db/cargobikeAPI.ts @@ -2,6 +2,7 @@ import { DataSource } from 'apollo-datasource'; import { getConnection, Connection } from 'typeorm'; import { CargoBike } from '../../model/CargoBike'; import { GraphQLError } from 'graphql'; +import { BikeEvent } from '../../model/BikeEvent'; /** * extended datasource to feed resolvers with data about cargoBikes */ @@ -12,10 +13,13 @@ export class CargoBikeAPI extends DataSource { this.connection = getConnection(); } - async getCargoBikes () { + async getCargoBikes (offset: number, limit: number) { return await this.connection.createQueryBuilder() .select('cargoBikes') .from(CargoBike, 'cargoBikes') + .orderBy('name', 'ASC') + .offset(offset) + .limit(limit) .getMany(); } @@ -32,34 +36,18 @@ export class CargoBikeAPI extends DataSource { .getOne() || new GraphQLError('ID not found'); } - async updateBike ({ id, name }:{id:any, name: string }) { - const bike = new CargoBike(); - bike.id = id; - bike.description = 'text'; - bike.name = name; - await this.connection.manager.save(bike); - return { - success: true, - message: 'bla', - cargoBike: { - id, - name - } - }; - } - /** * Updates CargoBike and return updated cargoBike * @param param0 cargoBike to be updated */ - async updateCargoBike ({ cargoBike }:{ cargoBike: any }) { + async updateCargoBike ({ cargoBike }:{ cargoBike: CargoBike }) { const bike = await this.connection.manager.createQueryBuilder() .select('cargoBike') .from(CargoBike, 'cargoBike') .where('cargoBike.id = :id', { id: cargoBike.id }) .getOne(); if (bike) { - await this.connection + await this.connection.manager .createQueryBuilder() .update(CargoBike) .set({ ...cargoBike }) @@ -88,4 +76,26 @@ export class CargoBikeAPI extends DataSource { newbike.id = inserts.identifiers[0].id; return newbike; } + + async createBikeEvent ({ bikeEvent }: { bikeEvent: any }) { + const event = new BikeEvent(); + event.setValues(bikeEvent); + event.cargoBike = await this.findCargoBikeById({ id: bikeEvent.cargoBikeId }) as unknown as CargoBike; + if (event.cargoBike instanceof GraphQLError) { + return event.cargoBike; + } + return await this.connection.manager.save(event); + } + + /** + * return bikeEvent including CargoBike + * @param id of event + */ + async findBikeEventById (id: number) { + return await this.connection.getRepository(BikeEvent) + .createQueryBuilder('bikeEvent') + .leftJoinAndSelect('bikeEvent.cargoBike', 'cargoBike') + .where('bikeEvent.id = :id', { id: id }) + .getOne(); + } } diff --git a/src/model/BikeEvent.ts b/src/model/BikeEvent.ts index 9c32b32..5b271db 100644 --- a/src/model/BikeEvent.ts +++ b/src/model/BikeEvent.ts @@ -1,15 +1,26 @@ /* eslint no-unused-vars: "off" */ -import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; +import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, JoinColumn } from 'typeorm'; +import { CargoBike } from './CargoBike'; export enum BikeEventType { - KAUF = 'Kauf', - INBETRIEBNAHME = 'Inbetriebnahme', - AUSFALL = 'Ausfall', - WARTUNG = 'Wartung' + KAUF = 'KAUF', + INBETRIEBNAHME = 'INBETRIEBNAHME', + AUSFALL = 'AUSFALL', + WARTUNG = 'WARTUNG', + ANDERE = 'ANDERE' } @Entity() export class BikeEvent { + public setValues ({ id, note, date, documents, cargoBike, eventType }: { id: number, note: string, date: Date, documents: string[], cargoBike: CargoBike, eventType: BikeEventType}): void { + this.id = id; + this.note = note; + this.date = date; + this.documents = documents; + this.cargoBike = cargoBike; + this.eventType = eventType; + } + @PrimaryGeneratedColumn() id: number; @@ -28,6 +39,12 @@ export class BikeEvent { }) documents: string[]; + @ManyToOne(tpye => CargoBike, cargoBike => cargoBike.bikeEvents, { + nullable: false + }) + @JoinColumn({ name: 'cargoBikeId' }) + cargoBike: CargoBike; + @Column({ type: 'enum', enum: BikeEventType diff --git a/src/model/CargoBike.ts b/src/model/CargoBike.ts index 6818578..41fa497 100644 --- a/src/model/CargoBike.ts +++ b/src/model/CargoBike.ts @@ -1,5 +1,5 @@ /* eslint no-unused-vars: "off" */ -import { Entity, Column, PrimaryGeneratedColumn, OneToMany, ManyToOne, OneToOne } from 'typeorm'; +import { Entity, Column, PrimaryGeneratedColumn, OneToMany, ManyToOne, JoinColumn } from 'typeorm'; import { Bike } from './BikeFeatures'; import { ChainSwap } from './ChainSwap'; import { Provider } from './Provider'; @@ -10,6 +10,7 @@ import { LendingStation } from './LendingStation'; import { Taxes } from './Taxes'; import { Equipment } from './Equipment'; import { Engagement } from './Engagement'; +import { BikeEvent } from './BikeEvent'; export enum Group { KL = 'KL', @@ -112,6 +113,13 @@ export class CargoBike extends Bike { }) coordinator: Participant; + @OneToMany(type => BikeEvent, bikeEvent => bikeEvent.cargoBike, { + nullable: true, + cascade: true + }) + @JoinColumn() + bikeEvents: BikeEvent[]; + @Column(type => InsuranceData) insuranceData: InsuranceData; diff --git a/src/model/Equipment.ts b/src/model/Equipment.ts index 50a261c..4dfd195 100644 --- a/src/model/Equipment.ts +++ b/src/model/Equipment.ts @@ -9,6 +9,14 @@ export class Equipment { @Column() serialNo: string; + @Column() + title: string; + + @Column({ + nullable: true + }) + description: string; + @OneToMany(type => CargoBike, cargoBike => cargoBike.equipment, { nullable: true }) diff --git a/src/model/Organization.ts b/src/model/Organization.ts index 4ca086f..24f018e 100644 --- a/src/model/Organization.ts +++ b/src/model/Organization.ts @@ -15,6 +15,7 @@ export class Organization { }) provider: Provider; + // Court where association was registerd @Column({ nullable: true }) diff --git a/src/model/Taxes.ts b/src/model/Taxes.ts index bdbb576..1c973ca 100644 --- a/src/model/Taxes.ts +++ b/src/model/Taxes.ts @@ -2,8 +2,8 @@ import { Column } from 'typeorm'; export enum OrganizationArea { - IB, - ZB + IB = 'IB', + ZB = 'ZB' } export class Taxes { @Column() diff --git a/src/resolvers/cargobikeResolver.ts b/src/resolvers/cargobikeResolver.ts index 10dd4f2..f7232d6 100644 --- a/src/resolvers/cargobikeResolver.ts +++ b/src/resolvers/cargobikeResolver.ts @@ -10,32 +10,39 @@ export default { return new GraphQLError('Insufficient Permissions'); } }, - cargoBikes: (_: any, __: any, { dataSources, req }: { dataSources: any, req: any }) => { + cargoBikes: (_: any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) => { if (req.permissions.includes(Permission.ReadBike)) { - return dataSources.cargoBikeAPI.getCargoBikes(); + return dataSources.cargoBikeAPI.getCargoBikes(offset, limit); + } else { + return new GraphQLError('Insufficiant Permissions'); + } + }, + bikeEventById: (_:any, { id }: { id: number }, { dataSources, req }: { dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadBike)) { + return dataSources.cargoBikeAPI.findBikeEventById(id); } else { return new GraphQLError('Insufficiant Permissions'); } } }, Mutation: { - addBike: (_: any, { id, name }:{id: any, name:string}, { dataSources, req }:{dataSources: any, req: any }) => { + createCargoBike: (_: any, { cargoBike }: { cargoBike: any }, { dataSources, req }:{dataSources: any, req: any }) => { if (req.permissions.includes(Permission.WriteBike)) { - return dataSources.cargoBikeAPI.updateBike({ id, name }); + return dataSources.cargoBikeAPI.createCargoBike({ cargoBike }); } else { return new GraphQLError('Insufficient Permissions'); } }, - createCargoBike: (_: any, { cargoBike }: { cargoBike: any }, { dataSources, req }:{dataSources: any, req: any }) => { + updateCargoBike: (_: any, { cargoBike }: { cargoBike: any }, { dataSources, req }:{dataSources: any, req: any }) => { if (req.permissions.includes(Permission.WriteBike)) { - return dataSources.cargoBikeAPI.createCargoBike({ cargoBike }); + return dataSources.cargoBikeAPI.updateCargoBike({ cargoBike }); } else { return new GraphQLError('Insufficient Permissions'); } }, - updateCargoBike: (_: any, { cargoBike }: { cargoBike: any }, { dataSources, req }:{dataSources: any, req: any }) => { + createBikeEvent: (_: any, { bikeEvent }: { bikeEvent: any }, { dataSources, req }: { dataSources: any, req: any }) => { if (req.permissions.includes(Permission.WriteBike)) { - return dataSources.cargoBikeAPI.updateCargoBike({ cargoBike }); + return dataSources.cargoBikeAPI.createBikeEvent({ bikeEvent }); } else { return new GraphQLError('Insufficient Permissions'); } diff --git a/src/schema/type-defs.ts b/src/schema/type-defs.ts index 1fc2d52..3217016 100644 --- a/src/schema/type-defs.ts +++ b/src/schema/type-defs.ts @@ -28,7 +28,7 @@ type CargoBike { Does not refere to an extra table in the database. """ dimensionsAndLoad: DimensionsAndLoad! - events: [BikeEvent] + bikeEvents: [BikeEvent] equipment: [Equipment] "Refers to equipment that is not unique. See kommentierte info tabelle -> Fragen -> Frage 2" otherEquipment: [String] @@ -76,11 +76,11 @@ input CargoBikeCreateInput { note: String provider: String insuranceData: InsuranceDataCreateInput! - taxes: TaxesCreateInput + taxes: TaxesCreateInput! } input CargoBikeUpdateInput { - id: ID! + id: Int! "see column A in info tabelle" group: Group name: String @@ -272,13 +272,26 @@ type Equipment { "An Event is a point in time, when the state of the bike somehow changed." type BikeEvent { id: ID! - eventType: BikeEventType + eventType: BikeEventType! + cargoBike: CargoBike! + date: Date! + note: String + """ + Path to documents + """ + documents: [String]! +} + +input BikeEventCreateInput { + eventType: BikeEventType! + "it is enough to pass the cargoBike id" + cargoBikeId: ID! date: Date! note: String """ Path to documents """ - documents: [String] + documents: [String]! } "TODO: Some eventTypes are missing (und auf deutsch)" @@ -293,6 +306,7 @@ enum BikeEventType { INBETRIEBNAHME AUSFALL WARTUNG + ANDERE } "How are the dimensions and how much weight can handle a bike. This data is merged in the CargoBike table and the BikeModel table." @@ -574,8 +588,8 @@ input AddressUpdateInput { type Query { cargoBikeById(id:ID!): CargoBike - "returns all cargoBikes" - cargoBikes: [CargoBike]! + "returns cargoBikes ordered by name ascending" + cargoBikes(offset: Int!, limit: Int!): [CargoBike]! "not important, you can just use providerById {cargoBikes}" cargoBikesByProvider(providerId:ID!): [CargoBike]! providerById(id:ID!): Provider @@ -585,11 +599,11 @@ type Query { lendingStationById(id:ID!): LendingStation lendingStations: [LendingStation]! contactInformation: [ContactInformation]! + "returns BikeEvent with CargoBike" + bikeEventById(id:ID!): BikeEvent! } type Mutation { - "for testing" - addBike(id: ID!, name: String): CargoBike! "creates new cargoBike and returns cargobike with new ID" createCargoBike(cargoBike: CargoBikeCreateInput!): CargoBike! "updates cargoBike of given ID with supplied fields and returns updated cargoBike" @@ -598,6 +612,8 @@ type Mutation { createLendingStation(lendingStation: LendingStationCreateInput): LendingStation! "updates lendingStation of given ID with supplied fields and returns updated lendingStation" updateLendingStation(lendingstation: LendingStationUpdateInput!): LendingStation! + "creates new BikeEvent" + createBikeEvent(bikeEvent: BikeEventCreateInput): BikeEvent! } `;