src/resolvers: added resolvers and lendingStationAPI

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

@ -10,6 +10,30 @@ export class LendingStationAPI extends DataSource {
this.connection = getConnection(); this.connection = getConnection();
} }
async getLendingStationById ({ id }: { id: any }) {
return await this.connection.manager
.createQueryBuilder()
.select('lendingStation')
.from(LendingStation, 'lendingStation')
.where('lendingStation.id = :id', { id: id })
.getOne();
}
/**
* get all lendingStations
*/
async getLendingStations () {
return await this.connection.manager
.createQueryBuilder()
.select('lendingStation')
.from(LendingStation, 'lendingStation')
.getMany() || new GraphQLError('Internal Server Error: could not query data from table lendingStation');
}
/**
* creates new lendingStation and returns new lendingStation with its new id
* @param param0 new lendingStation
*/
async createLendingStation ({ lendingStation }:{ lendingStation: any }) { async createLendingStation ({ lendingStation }:{ lendingStation: any }) {
console.log(lendingStation); console.log(lendingStation);
const inserts = await this.connection.manager const inserts = await this.connection.manager
@ -24,6 +48,10 @@ export class LendingStationAPI extends DataSource {
return newLendingStaion; return newLendingStaion;
} }
/**
* updates lendingStation and return updated lendingStation
* @param param0 lendingStation to be updated
*/
async updateLendingStation ({ lendingStation }:{ lendingStation: any }) { async updateLendingStation ({ lendingStation }:{ lendingStation: any }) {
const oldLendingStation = await this.connection.manager.createQueryBuilder() const oldLendingStation = await this.connection.manager.createQueryBuilder()
.select('lendingStation') .select('lendingStation')
@ -37,12 +65,7 @@ export class LendingStationAPI extends DataSource {
.set({ ...lendingStation }) .set({ ...lendingStation })
.where('id = :id', { id: lendingStation.id }) .where('id = :id', { id: lendingStation.id })
.execute(); .execute();
return await this.connection return this.getLendingStationById({ id: lendingStation.id });
.createQueryBuilder()
.select('lendingStation')
.from(LendingStation, 'lendingStation')
.where('lendingStation.id = :id', { id: lendingStation.id })
.getOne();
} else { } else {
return new GraphQLError('ID not in database'); return new GraphQLError('ID not in database');
} }

@ -4,6 +4,20 @@ import { LendingStation } from '../model/LendingStation';
export default { export default {
Query: { Query: {
lendingStationById: (_: any, { id }: { id: any }, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.ReadBike)) {
return dataSources.lendingStationAPI.getLendingStationById({ id });
} else {
return new GraphQLError('Insufficient Permissions');
}
},
lendingStations: (_: any, __: any, { dataSources, req }: { dataSources: any, req: any }) => {
if (req.permissions.includes(Permission.ReadBike)) {
return dataSources.lendingStationAPI.getLendingStations();
} else {
return new GraphQLError('Insufficient Permissions');
}
}
}, },
Mutation: { Mutation: {
createLendingStation: (_: any, { lendingStation }:{ lendingStation: LendingStation }, { dataSources, req }:{dataSources: any, req: any }) => { createLendingStation: (_: any, { lendingStation }:{ lendingStation: LendingStation }, { dataSources, req }:{dataSources: any, req: any }) => {

@ -574,8 +574,9 @@ input AddressUpdateInput {
type Query { type Query {
cargoBikeById(id:ID!): CargoBike cargoBikeById(id:ID!): CargoBike
"!!!!" "returns all cargoBikes"
cargoBikes: [CargoBike]! cargoBikes: [CargoBike]!
"not important, you can just use providerById {cargoBikes}"
cargoBikesByProvider(providerId:ID!): [CargoBike]! cargoBikesByProvider(providerId:ID!): [CargoBike]!
providerById(id:ID!): Provider providerById(id:ID!): Provider
providers: [Provider]! providers: [Provider]!

Loading…
Cancel
Save