workshop, workshop types create and read
parent
c26e9b78af
commit
77fd61194b
@ -0,0 +1,72 @@
|
||||
import { DataSource } from 'apollo-datasource';
|
||||
import { Connection, getConnection } from 'typeorm';
|
||||
import { WorkshopType } from '../../model/WorkshopType';
|
||||
import { Workshop } from '../../model/Workshop';
|
||||
|
||||
export class WorkshopAPI extends DataSource {
|
||||
connection: Connection
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
this.connection = getConnection();
|
||||
}
|
||||
|
||||
async createWorkshop (workshop: any) {
|
||||
const inserts = await this.connection.getRepository(Workshop)
|
||||
.createQueryBuilder('w')
|
||||
.insert()
|
||||
.values([workshop])
|
||||
.returning('*')
|
||||
.execute();
|
||||
return inserts.generatedMaps[0];
|
||||
}
|
||||
|
||||
async createWorkshopType (workshopType: any) {
|
||||
const inserts = await this.connection.getRepository(WorkshopType)
|
||||
.createQueryBuilder('wt')
|
||||
.insert()
|
||||
.values([workshopType])
|
||||
.returning('*')
|
||||
.execute();
|
||||
return inserts.generatedMaps[0];
|
||||
}
|
||||
|
||||
async workshopTypes (offset: number, limit: number) {
|
||||
return await this.connection.getRepository(WorkshopType)
|
||||
.createQueryBuilder('w')
|
||||
.select()
|
||||
.skip(offset)
|
||||
.take(limit)
|
||||
.getMany();
|
||||
}
|
||||
|
||||
/**
|
||||
* finds workshops with pagination
|
||||
* @param offset
|
||||
* @param limit
|
||||
*/
|
||||
async workshops (offset: number, limit: number) {
|
||||
return await this.connection.getRepository(Workshop)
|
||||
.createQueryBuilder('w')
|
||||
.select()
|
||||
.skip(offset)
|
||||
.take(limit)
|
||||
.getMany();
|
||||
}
|
||||
|
||||
async trainer1ByWorkshopId (id: number) {
|
||||
return await this.connection.getRepository(Workshop)
|
||||
.createQueryBuilder('w')
|
||||
.relation(Workshop, 'trainer1Id')
|
||||
.of(id)
|
||||
.loadOne();
|
||||
}
|
||||
|
||||
async trainer2ByWorkshopId (id: number) {
|
||||
return await this.connection.getRepository(Workshop)
|
||||
.createQueryBuilder('w')
|
||||
.relation(Workshop, 'trainer2Id')
|
||||
.of(id)
|
||||
.loadOne();
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
import { Permission } from '../datasources/userserver/permission';
|
||||
import { GraphQLError } from 'graphql';
|
||||
|
||||
export default {
|
||||
Query: {
|
||||
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);
|
||||
} 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);
|
||||
} else {
|
||||
return new GraphQLError('Insufficient Permissions');
|
||||
}
|
||||
}
|
||||
},
|
||||
Workshop: {
|
||||
trainer1: (parent: any, __:any, { dataSources, req }: { dataSources: any, req: any }) => {
|
||||
return dataSources.workshopAPI.trainer1ByWorkshopId(parent.id);
|
||||
},
|
||||
trainer2: (parent: any, __:any, { dataSources, req }: { dataSources: any, req: any }) => {
|
||||
return dataSources.workshopAPI.trainer2ByWorkshopId(parent.id);
|
||||
}
|
||||
},
|
||||
Mutation: {
|
||||
createWorkshop: (_: any, { workshop }: { workshop: number }, { dataSources, req }: { dataSources: any, req: any }) => {
|
||||
if (req.permissions.includes(Permission.WriteWorkshopType)) {
|
||||
return dataSources.workshopAPI.createWorkshop(workshop);
|
||||
} else {
|
||||
return new GraphQLError('Insufficient Permissions');
|
||||
}
|
||||
},
|
||||
createWorkshopType: (_: any, { workshopType }: { workshopType: number }, { dataSources, req }: { dataSources: any, req: any }) => {
|
||||
if (req.permissions.includes(Permission.WriteWorkshopType)) {
|
||||
return dataSources.workshopAPI.createWorkshopType(workshopType);
|
||||
} else {
|
||||
return new GraphQLError('Insufficient Permissions');
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue