|
|
@ -3,6 +3,8 @@ import { getConnection, Connection } from 'typeorm';
|
|
|
|
import { CargoBike } from '../../model/CargoBike';
|
|
|
|
import { CargoBike } from '../../model/CargoBike';
|
|
|
|
import { GraphQLError } from 'graphql';
|
|
|
|
import { GraphQLError } from 'graphql';
|
|
|
|
import { BikeEvent } from '../../model/BikeEvent';
|
|
|
|
import { BikeEvent } from '../../model/BikeEvent';
|
|
|
|
|
|
|
|
import { Equipment } from '../../model/Equipment';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* extended datasource to feed resolvers with data about cargoBikes
|
|
|
|
* extended datasource to feed resolvers with data about cargoBikes
|
|
|
|
*/
|
|
|
|
*/
|
|
|
@ -15,8 +17,8 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
|
|
|
|
|
|
|
|
async getCargoBikes (offset: number, limit: number) {
|
|
|
|
async getCargoBikes (offset: number, limit: number) {
|
|
|
|
return await this.connection.createQueryBuilder()
|
|
|
|
return await this.connection.createQueryBuilder()
|
|
|
|
.select('cargoBikes')
|
|
|
|
.select('cargoBike')
|
|
|
|
.from(CargoBike, 'cargoBikes')
|
|
|
|
.from(CargoBike, 'cargoBike')
|
|
|
|
.orderBy('name', 'ASC')
|
|
|
|
.orderBy('name', 'ASC')
|
|
|
|
.offset(offset)
|
|
|
|
.offset(offset)
|
|
|
|
.limit(limit)
|
|
|
|
.limit(limit)
|
|
|
@ -24,16 +26,16 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Finds cargo bike by id, retuns error if id not found
|
|
|
|
* Finds cargo bike by id, retuns null if id was not found
|
|
|
|
* @param param0 id of bike
|
|
|
|
* @param param0 id of bike
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async findCargoBikeById ({ id }:{id: any}) {
|
|
|
|
async findCargoBikeById ({ id }:{id: any}) {
|
|
|
|
return await this.connection.manager
|
|
|
|
return await this.connection.manager.getRepository(CargoBike).findOne({ id: id });
|
|
|
|
.createQueryBuilder()
|
|
|
|
/* .createQueryBuilder()
|
|
|
|
.select('cargoBike')
|
|
|
|
.select('cargoBike')
|
|
|
|
.from(CargoBike, 'cargoBike')
|
|
|
|
.from(CargoBike, 'cargoBike')
|
|
|
|
.where('cargoBike.id = :id', { id })
|
|
|
|
.where('cargoBike.id = :id', { id })
|
|
|
|
.getOne() || new GraphQLError('ID not found');
|
|
|
|
.getOne() || new GraphQLError('ID not found'); */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -98,4 +100,78 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
.where('bikeEvent.id = :id', { id: id })
|
|
|
|
.where('bikeEvent.id = :id', { id: id })
|
|
|
|
.getOne();
|
|
|
|
.getOne();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async findEquipmentById (id: number) {
|
|
|
|
|
|
|
|
return await this.connection.getRepository(Equipment)
|
|
|
|
|
|
|
|
.createQueryBuilder('equipment')
|
|
|
|
|
|
|
|
.select()
|
|
|
|
|
|
|
|
.where('equipment.id = :id', { id: id })
|
|
|
|
|
|
|
|
.getOne();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async findEquipmentJoinBikeById (id: number) {
|
|
|
|
|
|
|
|
return await this.connection.getRepository(Equipment)
|
|
|
|
|
|
|
|
.createQueryBuilder('equipment')
|
|
|
|
|
|
|
|
.leftJoinAndSelect('equipment.cargoBike', 'cargoBike')
|
|
|
|
|
|
|
|
.where('equipment.id = :id', { id: id })
|
|
|
|
|
|
|
|
.getOne();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async createEquipment ({ equipment }: { equipment: any }) {
|
|
|
|
|
|
|
|
const inserts = await this.connection.getRepository(Equipment)
|
|
|
|
|
|
|
|
.createQueryBuilder('equipment')
|
|
|
|
|
|
|
|
.insert()
|
|
|
|
|
|
|
|
.into(Equipment)
|
|
|
|
|
|
|
|
.values([equipment])
|
|
|
|
|
|
|
|
.returning('*')
|
|
|
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
if (equipment.cargoBikeId) {
|
|
|
|
|
|
|
|
await this.connection
|
|
|
|
|
|
|
|
.createQueryBuilder()
|
|
|
|
|
|
|
|
.relation(Equipment, 'cargoBike')
|
|
|
|
|
|
|
|
.of(equipment.id)
|
|
|
|
|
|
|
|
.set(equipment.cargoBikeId);
|
|
|
|
|
|
|
|
return this.findEquipmentJoinBikeById(inserts.identifiers[0].id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.findEquipmentById(inserts.identifiers[0].id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Will update Equipment, crashes when id not in db or cargoBikeId not db.
|
|
|
|
|
|
|
|
* Will return updated Equipment joined with CargoBike only if cargoBike is was set in param0
|
|
|
|
|
|
|
|
* @param param0 struct with equipment properites
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
async updateEquipment ({ equipment }: { equipment: any }) {
|
|
|
|
|
|
|
|
console.log(equipment);
|
|
|
|
|
|
|
|
const cargoBikeId = equipment.cargoBikeId;
|
|
|
|
|
|
|
|
delete equipment.cargoBikeId;
|
|
|
|
|
|
|
|
console.log(equipment);
|
|
|
|
|
|
|
|
const inserts = await this.connection.getRepository(Equipment)
|
|
|
|
|
|
|
|
.createQueryBuilder('equipment')
|
|
|
|
|
|
|
|
.update()
|
|
|
|
|
|
|
|
.set({ ...equipment })
|
|
|
|
|
|
|
|
.where('id = :id', { id: equipment.id })
|
|
|
|
|
|
|
|
.returning('*')
|
|
|
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
console.log(inserts.raw[0]);
|
|
|
|
|
|
|
|
if (cargoBikeId || cargoBikeId === null) {
|
|
|
|
|
|
|
|
await this.connection.getRepository(Equipment)
|
|
|
|
|
|
|
|
.createQueryBuilder()
|
|
|
|
|
|
|
|
.relation(Equipment, 'cargoBike')
|
|
|
|
|
|
|
|
.of(equipment.id)
|
|
|
|
|
|
|
|
.set(cargoBikeId);
|
|
|
|
|
|
|
|
return this.findEquipmentJoinBikeById(equipment.id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.findEquipmentById(equipment.id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async getEquipment (offset: number, limit: number) {
|
|
|
|
|
|
|
|
return await this.connection.getRepository(Equipment)
|
|
|
|
|
|
|
|
.createQueryBuilder('equipment')
|
|
|
|
|
|
|
|
.leftJoinAndSelect('equipment.cargoBike', 'cargoBike')
|
|
|
|
|
|
|
|
.orderBy('title', 'ASC')
|
|
|
|
|
|
|
|
.offset(offset)
|
|
|
|
|
|
|
|
.limit(limit)
|
|
|
|
|
|
|
|
.getMany();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|