|
|
|
@ -3,6 +3,8 @@ import { getConnection, Connection } from 'typeorm';
|
|
|
|
|
import { CargoBike } from '../../model/CargoBike';
|
|
|
|
|
import { GraphQLError } from 'graphql';
|
|
|
|
|
import { BikeEvent } from '../../model/BikeEvent';
|
|
|
|
|
import { Equipment } from '../../model/Equipment';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* extended datasource to feed resolvers with data about cargoBikes
|
|
|
|
|
*/
|
|
|
|
@ -15,8 +17,8 @@ export class CargoBikeAPI extends DataSource {
|
|
|
|
|
|
|
|
|
|
async getCargoBikes (offset: number, limit: number) {
|
|
|
|
|
return await this.connection.createQueryBuilder()
|
|
|
|
|
.select('cargoBikes')
|
|
|
|
|
.from(CargoBike, 'cargoBikes')
|
|
|
|
|
.select('cargoBike')
|
|
|
|
|
.from(CargoBike, 'cargoBike')
|
|
|
|
|
.orderBy('name', 'ASC')
|
|
|
|
|
.offset(offset)
|
|
|
|
|
.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
|
|
|
|
|
*/
|
|
|
|
|
async findCargoBikeById ({ id }:{id: any}) {
|
|
|
|
|
return await this.connection.manager
|
|
|
|
|
.createQueryBuilder()
|
|
|
|
|
return await this.connection.manager.getRepository(CargoBike).findOne({ id: id });
|
|
|
|
|
/* .createQueryBuilder()
|
|
|
|
|
.select('cargoBike')
|
|
|
|
|
.from(CargoBike, 'cargoBike')
|
|
|
|
|
.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 })
|
|
|
|
|
.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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|