added some resolvers and paging on resolver cargobikes

pull/9/head
leonnicolas 4 years ago
parent 91a20704af
commit f87aa84e86
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

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

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

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

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

@ -15,6 +15,7 @@ export class Organization {
})
provider: Provider;
// Court where association was registerd
@Column({
nullable: true
})

@ -2,8 +2,8 @@
import { Column } from 'typeorm';
export enum OrganizationArea {
IB,
ZB
IB = 'IB',
ZB = 'ZB'
}
export class Taxes {
@Column()

@ -10,22 +10,22 @@ 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');
}
}
},
Mutation: {
addBike: (_: any, { id, name }:{id: any, name:string}, { dataSources, req }:{dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) {
return dataSources.cargoBikeAPI.updateBike({ id, name });
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('Insufficient Permissions');
return new GraphQLError('Insufficiant Permissions');
}
}
},
Mutation: {
createCargoBike: (_: any, { cargoBike }: { cargoBike: any }, { dataSources, req }:{dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) {
return dataSources.cargoBikeAPI.createCargoBike({ cargoBike });
@ -39,6 +39,13 @@ export default {
} else {
return new GraphQLError('Insufficient Permissions');
}
},
createBikeEvent: (_: any, { bikeEvent }: { bikeEvent: any }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.WriteBike)) {
return dataSources.cargoBikeAPI.createBikeEvent({ bikeEvent });
} else {
return new GraphQLError('Insufficient Permissions');
}
}
}
};

@ -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!
}
`;

Loading…
Cancel
Save