From 813f41ec8e8690669f5df69b875b875781f16236 Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Wed, 14 Oct 2020 19:33:39 +0200 Subject: [PATCH] finished all Queries --- src/datasources/db/cargobikeAPI.ts | 36 +++++++++++++++++--- src/datasources/db/contactinformationAPI.ts | 34 ++++++------------ src/datasources/db/participantAPI.ts | 26 ++++++++++++++ src/datasources/db/providerAPI.ts | 17 +++++++++ src/datasources/db/workshopAPI.ts | 16 +++++++++ src/resolvers/cargobikeResolver.ts | 30 +++++++++++++++- src/resolvers/contactinformationResolvers.ts | 14 ++++++++ src/resolvers/lendingstationResolvers.ts | 7 ++++ src/resolvers/participantResolvers.ts | 28 +++++++++++++++ src/resolvers/providerResolvers.ts | 14 ++++++++ src/resolvers/workshopResolvers.ts | 14 ++++++++ src/schema/type-defs.ts | 31 ++++++++++++----- 12 files changed, 229 insertions(+), 38 deletions(-) diff --git a/src/datasources/db/cargobikeAPI.ts b/src/datasources/db/cargobikeAPI.ts index 241f595..af7d5f8 100644 --- a/src/datasources/db/cargobikeAPI.ts +++ b/src/datasources/db/cargobikeAPI.ts @@ -187,6 +187,23 @@ export class CargoBikeAPI extends DataSource { .getMany(); } + async bikeEvents (offset: number, limit: number) { + return await this.connection.getRepository(BikeEvent) + .createQueryBuilder('be') + .select() + .skip(offset) + .take(limit) + .getMany(); + } + + async findBikeEventTypeById (id: number) { + return await this.connection.getRepository(BikeEventType) + .createQueryBuilder('bet') + .select() + .where('id = :id', { id: id }) + .getOne(); + } + async responsibleByBikeEventId (id: number) { return await this.connection.getRepository(BikeEvent) .createQueryBuilder('be') @@ -223,7 +240,7 @@ export class CargoBikeAPI extends DataSource { return await LockUtils.unlockEntity(this.connection, BikeEvent, 'be', id, userId); } - async findEquipmentById (id: number) { + async equipmentById (id: number) { return await this.connection.getRepository(Equipment) .createQueryBuilder('equipment') .select() @@ -271,7 +288,7 @@ export class CargoBikeAPI extends DataSource { .of(equipment.id) .set(equipment.cargoBikeId); } - return this.findEquipmentById(inserts.identifiers[0].id); + return this.equipmentById(inserts.identifiers[0].id); } async cargoBikeByEquipmentId (id: number) { @@ -321,9 +338,9 @@ export class CargoBikeAPI extends DataSource { .of(equipment.id) .set(cargoBikeId); !keepLock && LockUtils.unlockEntity(this.connection, Equipment, 'e', equipment.id, userId); - return this.findEquipmentById(equipment.id); + return this.equipmentById(equipment.id); } - return this.findEquipmentById(equipment.id); + return this.equipmentById(equipment.id); } async getEquipment (offset: number, limit: number) { @@ -355,8 +372,17 @@ export class CargoBikeAPI extends DataSource { .getOne(); } + async eqiupmentTypes (offset: number, limit: number) { + return await this.connection.getRepository(EquipmentType) + .createQueryBuilder('et') + .select() + .skip(offset) + .take(limit) + .getMany(); + } + async equipmentTypeByCargoBikeId (id: number) { - return this.connection.getRepository(CargoBike) + return await this.connection.getRepository(CargoBike) .createQueryBuilder('cargobike') .relation(CargoBike, 'equipmentTypeIds') .of(id) diff --git a/src/datasources/db/contactinformationAPI.ts b/src/datasources/db/contactinformationAPI.ts index fd7f3f2..93d8f92 100644 --- a/src/datasources/db/contactinformationAPI.ts +++ b/src/datasources/db/contactinformationAPI.ts @@ -1,7 +1,6 @@ import { DataSource } from 'apollo-datasource'; import { Connection, getConnection } from 'typeorm'; import { ContactInformation } from '../../model/ContactInformation'; -import { LendingStation } from '../../model/LendingStation'; import { Person } from '../../model/Person'; export class ContactInformationAPI extends DataSource { @@ -33,21 +32,12 @@ export class ContactInformationAPI extends DataSource { async contactInformationById (id: number) { return await this.connection.getRepository(ContactInformation) - .createQueryBuilder('contactInformation') + .createQueryBuilder('ci') .select() - .where('"contactInformation".id = :id', { id: id }) + .where('id = :id', { id: id }) .getOne(); } - // TODO change to contactinformation - async contactPersonsByLendingStationId (id: number) { - return await this.connection - .createQueryBuilder() - .relation(LendingStation, 'contactPersons') - .of(id) - .loadMany(); - } - async createPerson (person: any) { const inserts = await this.connection.getRepository(Person) .createQueryBuilder('person') @@ -59,18 +49,6 @@ export class ContactInformationAPI extends DataSource { return inserts.generatedMaps[0]; } - /** - * Return person by ID - * @param id - */ - async personById (id: number) { - return await this.connection.getRepository(Person) - .createQueryBuilder('person') - .select() - .where('person.id = :id', { id: id }) - .getOne(); - } - async persons (offset: number, limit: number) { return await this.connection.getRepository(Person) .createQueryBuilder('person') @@ -80,6 +58,14 @@ export class ContactInformationAPI extends DataSource { .execute(); } + async personById (id: number) { + return await this.connection.getRepository(Person) + .createQueryBuilder('p') + .select() + .where('id = :id', { id: id }) + .getOne(); + } + async personByContactInformationId (id: number) { return await this.connection.getRepository(ContactInformation) .createQueryBuilder('ci') diff --git a/src/datasources/db/participantAPI.ts b/src/datasources/db/participantAPI.ts index 5810fd9..5bf9a9e 100644 --- a/src/datasources/db/participantAPI.ts +++ b/src/datasources/db/participantAPI.ts @@ -69,6 +69,15 @@ export class ParticipantAPI extends DataSource { .getMany(); } + async engagements (offset: number, limit: number) { + return await this.connection.getRepository(Engagement) + .createQueryBuilder('e') + .select() + .skip(offset) + .take(limit) + .getMany(); + } + async engagementById (id: number) { return await this.connection.getRepository(Engagement) .createQueryBuilder('engagement') @@ -77,6 +86,23 @@ export class ParticipantAPI extends DataSource { .getOne(); } + async engagementTypeById (id: number) { + return await this.connection.getRepository(EngagementType) + .createQueryBuilder('et') + .select() + .where('id = :id', { id: id }) + .getOne(); + } + + async engagementTypes (offset: number, limit: number) { + return await this.connection.getRepository(EngagementType) + .createQueryBuilder('et') + .select() + .skip(offset) + .take(limit) + .getMany(); + } + async engagementTypeByEngagementId (id: number) { return await this.connection.getRepository(Engagement) .createQueryBuilder('engagement') diff --git a/src/datasources/db/providerAPI.ts b/src/datasources/db/providerAPI.ts index 0e55ec8..96b4101 100644 --- a/src/datasources/db/providerAPI.ts +++ b/src/datasources/db/providerAPI.ts @@ -53,6 +53,23 @@ export class ProviderAPI extends DataSource { .loadOne(); } + async organisations (offset: number, limit: number) { + return await this.connection.getRepository(Organisation) + .createQueryBuilder('o') + .select() + .skip(offset) + .limit(limit) + .getMany(); + } + + async organisationById (id: number) { + return await this.connection.getRepository(Organisation) + .createQueryBuilder('o') + .select() + .where('id = :id', { id: id }) + .getOne(); + } + async contactInformationByOrganisationId (id: number) { return await this.connection.getRepository(Organisation) .createQueryBuilder('o') diff --git a/src/datasources/db/workshopAPI.ts b/src/datasources/db/workshopAPI.ts index 517260c..9c954bd 100644 --- a/src/datasources/db/workshopAPI.ts +++ b/src/datasources/db/workshopAPI.ts @@ -31,6 +31,14 @@ export class WorkshopAPI extends DataSource { return inserts.generatedMaps[0]; } + async workshopTypeById (id: number) { + return await this.connection.getRepository(WorkshopType) + .createQueryBuilder('wt') + .select() + .where('id = :id', { id: id }) + .getOne(); + } + async workshopTypes (offset: number, limit: number) { return await this.connection.getRepository(WorkshopType) .createQueryBuilder('w') @@ -40,6 +48,14 @@ export class WorkshopAPI extends DataSource { .getMany(); } + async workshopById (id: number) { + return await this.connection.getRepository(Workshop) + .createQueryBuilder('w') + .select() + .where('id = :id', { id: id }) + .getOne(); + } + /** * finds workshops with pagination * @param offset diff --git a/src/resolvers/cargobikeResolver.ts b/src/resolvers/cargobikeResolver.ts index 81d9ccc..d612401 100644 --- a/src/resolvers/cargobikeResolver.ts +++ b/src/resolvers/cargobikeResolver.ts @@ -18,6 +18,13 @@ export default { return new GraphQLError('Insufficiant Permissions'); } }, + bikeEvents: (_:any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadBike)) { + return dataSources.cargoBikeAPI.bikeEvents(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); @@ -25,6 +32,13 @@ export default { return new GraphQLError('Insufficiant Permissions'); } }, + bikeEventTypeByd: (_:any, { id }: { id: number }, { dataSources, req }: { dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadBike)) { + return dataSources.cargoBikeAPI.findBikeEventTypeById(id); + } else { + return new GraphQLError('Insufficiant Permissions'); + } + }, equipment: (_:any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) => { if (req.permissions.includes(Permission.ReadBike)) { return dataSources.cargoBikeAPI.getEquipment(offset, limit); @@ -34,7 +48,21 @@ export default { }, equipmentById: (_:any, { id }: { id: number }, { dataSources, req }: { dataSources: any, req: any }) => { if (req.permissions.includes(Permission.ReadBike)) { - return dataSources.cargoBikeAPI.findEquipmentJoinBikeById(id); + return dataSources.cargoBikeAPI.equipmentById(id); + } else { + return new GraphQLError('Insufficiant Permissions'); + } + }, + equipmentTypes: (_:any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadBike)) { + return dataSources.cargoBikeAPI.equipmentTypes(offset, limit); + } else { + return new GraphQLError('Insufficiant Permissions'); + } + }, + equipmentTypeById: (_:any, { id }: { id: number }, { dataSources, req }: { dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadBike)) { + return dataSources.cargoBikeAPI.equipmentTypeById(id); } else { return new GraphQLError('Insufficiant Permissions'); } diff --git a/src/resolvers/contactinformationResolvers.ts b/src/resolvers/contactinformationResolvers.ts index 78b7fca..87eb098 100644 --- a/src/resolvers/contactinformationResolvers.ts +++ b/src/resolvers/contactinformationResolvers.ts @@ -12,6 +12,20 @@ export default { return new GraphQLError('Insufficient Permissions'); } }, + contactInformationById: (_: any, { id }: { id: number }, { dataSources, req }: { dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadPerson)) { + return dataSources.contactInformationAPI.contactInformationById(id); + } else { + return new GraphQLError('Insufficient Permissions'); + } + }, + personById: (_: any, { id }: { id: number }, { dataSources, req }: { dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadPerson)) { + return dataSources.contactInformationAPI.personById(id); + } else { + return new GraphQLError('Insufficient Permissions'); + } + }, persons: (_: any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) => { if (req.permissions.includes(Permission.ReadPerson)) { return dataSources.contactInformationAPI.persons(offset, limit); diff --git a/src/resolvers/lendingstationResolvers.ts b/src/resolvers/lendingstationResolvers.ts index 34dd237..1391b70 100644 --- a/src/resolvers/lendingstationResolvers.ts +++ b/src/resolvers/lendingstationResolvers.ts @@ -19,6 +19,13 @@ export default { return new GraphQLError('Insufficient Permissions'); } }, + timeFrameById: (_: any, { id }: { id: number }, { dataSources, req }: { dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadBike)) { + return dataSources.lendingStationAPI.timeFrameById(id); + } else { + return new GraphQLError('Insufficient Permissions'); + } + }, timeframes: (_: any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) => { if (req.permissions.includes(Permission.ReadBike)) { return dataSources.lendingStationAPI.timeFrames(offset, limit); diff --git a/src/resolvers/participantResolvers.ts b/src/resolvers/participantResolvers.ts index 88ba876..a5b9129 100644 --- a/src/resolvers/participantResolvers.ts +++ b/src/resolvers/participantResolvers.ts @@ -18,6 +18,34 @@ export default { } else { return new GraphQLError('Insufficient Permissions'); } + }, + engagementById: (_: any, { id }: { id: number }, { dataSources, req }: { dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadBike)) { + return dataSources.participantAPI.engagementById(id); + } else { + return new GraphQLError('Insufficient Permissions'); + } + }, + engagements: (_: any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadBike)) { + return dataSources.participantAPI.engagements(offset, limit); + } else { + return new GraphQLError('Insufficient Permissions'); + } + }, + engagementTypeById: (_: any, { id }: { id: number }, { dataSources, req }: { dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadBike)) { + return dataSources.participantAPI.engagementTypeById(id); + } else { + return new GraphQLError('Insufficient Permissions'); + } + }, + engagementTypes: (_: any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadBike)) { + return dataSources.participantAPI.engagementTypes(offset, limit); + } else { + return new GraphQLError('Insufficient Permissions'); + } } }, Participant: { diff --git a/src/resolvers/providerResolvers.ts b/src/resolvers/providerResolvers.ts index bde3f32..d0b7099 100644 --- a/src/resolvers/providerResolvers.ts +++ b/src/resolvers/providerResolvers.ts @@ -17,6 +17,20 @@ export default { } else { return new GraphQLError('Insufficient Permissions'); } + }, + organisations: (_: any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadBike)) { + return dataSources.providerAPI.organisations(offset, limit); + } else { + return new GraphQLError('Insufficient Permissions'); + } + }, + organisationById: (_: any, { id }: { id: number }, { dataSources, req }: { dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadBike)) { + return dataSources.providerAPI.organisationById(id); + } else { + return new GraphQLError('Insufficient Permissions'); + } } }, Provider: { diff --git a/src/resolvers/workshopResolvers.ts b/src/resolvers/workshopResolvers.ts index 986caa9..89527d6 100644 --- a/src/resolvers/workshopResolvers.ts +++ b/src/resolvers/workshopResolvers.ts @@ -4,6 +4,13 @@ import { isLocked } from '../datasources/db/utils'; export default { Query: { + workshopTypeById: (_: any, { id }: { id: number }, { dataSources, req }: { dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadBike)) { + return dataSources.workshopAPI.workshopTypeById(id); + } else { + return new GraphQLError('Insufficient Permissions'); + } + }, workshopTypes: (_: any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) => { if (req.permissions.includes(Permission.ReadBike)) { return dataSources.workshopAPI.workshopTypes(offset, limit); @@ -11,6 +18,13 @@ export default { return new GraphQLError('Insufficient Permissions'); } }, + workshopById: (_: any, { id }: { id: number }, { dataSources, req }: { dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadBike)) { + return dataSources.workshopAPI.workshopById(id); + } else { + return new GraphQLError('Insufficient Permissions'); + } + }, workshops: (_: any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) => { if (req.permissions.includes(Permission.ReadBike)) { return dataSources.workshopAPI.workshops(offset, limit); diff --git a/src/schema/type-defs.ts b/src/schema/type-defs.ts index b190370..ea3968e 100644 --- a/src/schema/type-defs.ts +++ b/src/schema/type-defs.ts @@ -813,26 +813,39 @@ type Query { cargoBikeById(id:ID!): CargoBike "returns cargoBikes ordered by name ascending, relations are not loaded, use cargoBikeById instead" cargoBikes(offset: Int!, limit: Int!): [CargoBike]! + engagementById(id: ID!): Engagement + engagements(offset: Int!, limit: Int!): [Engagement]! + engagementTypeById(id: ID!): EngagementType + engagementTypes(offset: Int!, limit: Int!): [EngagementType]! + "equipment by id, will return null if id not found" + equipmentById(id: ID!): Equipment + equipment(offset: Int!, limit: Int!): [Equipment]! + equipmentTypeById(id: ID!): EquipmentType + equipmentTypes(offset: Int!, limit: Int!): [EquipmentType]! "return null if id not found" providerById(id:ID!): Provider "unique equipment with pagination, contains relation to bike (with no further joins), so if you wanna know more about the bike, use cargoBikeById" - equipment(offset: Int!, limit: Int!): [Equipment]! - "equipment by id, will return null if id not found" - equipmentById(id: ID!): Equipment providers(offset: Int!, limit: Int!): [Provider]! "participant by id" participantById(id:ID!): Participant - "p" participants(offset: Int!, limit: Int!): [ Participant]! - workshopTypes(offset: Int!, limit: Int!): [WorkshopType] - workshops(offset: Int!, limit: Int!): [Workshop] + workshopTypeById(id: ID!): WorkshopType + workshopTypes(offset: Int!, limit: Int!): [WorkshopType]! + workshopById(id: ID!): Workshop + workshops(offset: Int!, limit: Int!): [Workshop]! lendingStationById(id:ID!): LendingStation lendingStations(offset: Int!, limit: Int!): [LendingStation]! + organisationById(id: ID!): Organisation + organisations(offset: Int!, limit: Int!): [Organisation]! + timeFrameById(id: ID!): TimeFrame timeframes(offset: Int!, limit: Int!): [TimeFrame]! + contactInformationById(id: ID!): ContactInformation contactInformation(offset: Int!, limit: Int!): [ContactInformation]! + personById(id: ID!): Person persons(offset: Int!, limit: Int!): [Person] bikeEventTypes(offset: Int!, limit: Int!): [BikeEventType] - "returns BikeEvent with CargoBike" + bikeEventTypeByd(id: ID!): BikeEventType + bikeEvents(offset: Int!, limit: Int!): [BikeEvent]! bikeEventById(id:ID!): BikeEvent! } @@ -879,7 +892,9 @@ type Mutation { createBikeEvent(bikeEvent: BikeEventCreateInput!): BikeEvent! lockBikeEventById(id: ID!): BikeEvent unlockBikeEventById(id: ID!): Boolean! - "create participant" + """ + PARTICIPANTS + """ createParticipant(participant: ParticipantCreateInput!): Participant! createWorkshopType(workshopType: WorkshopTypeCreateInput!): WorkshopType! createWorkshop(workshop: WorkshopCreateInput!): Workshop!