Skip to content

Commit

Permalink
Merge 74a83e6 into a36ec64
Browse files Browse the repository at this point in the history
  • Loading branch information
codyjdalton committed Sep 25, 2018
2 parents a36ec64 + 74a83e6 commit dc0a90f
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ export abstract class ResourceComponent {
@GetMapping()
public findAll(res: HttpResponse): void {
this.mainService.findAll()
.then((items: Document[]) => res.success(items));
.subscribe((items: Document[]) => res.success(items));
}

@GetMapping({
path: ':id'
})
public findOne(req: HttpRequest, res: HttpResponse): void {
this.mainService.findByIdObservable(req.params.id)
this.mainService.findById(req.params.id)
.subscribe(
(item: Document) => res.success(item),
() => res.errored(404)
Expand Down
28 changes: 10 additions & 18 deletions modules/common/services/knot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,17 @@ export class KnotService extends ResourceService {
super();
}

create(narrativeId: string, key: string, title: string): Promise<IKnot> {
return new Promise((resolve, reject) => {

const aKnot: IKnot = new Knot({
id: uuid.v4(),
narrativeId: narrativeId,
key: key,
title: title
});

// ensure knot id exists
this.narrativeService.findById(narrativeId)
.then(() => {
aKnot.save()
.then((knot: IKnot) => resolve(knot))
.catch(() => reject());
})
.catch(() => reject());
create(narrativeId: string, key: string, title: string): Observable<IKnot> {

const aKnot: IKnot = new Knot({
id: uuid.v4(),
narrativeId: narrativeId,
key: key,
title: title
});

// ensure knot id exists
return from(aKnot.save());
}

public deleteById(id: string): Observable<any> {
Expand Down
31 changes: 11 additions & 20 deletions modules/common/services/outcome.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,18 @@ export class OutcomeService extends ResourceService {
}

public create(narrativeId: string, knotId: string, key: string,
destinationType: DestinationTypes, destinationId: string): Promise<IOutcome> {
return new Promise((resolve, reject) => {

const anOutcome: IOutcome = new Outcome({
id: uuid.v4(),
narrativeId: narrativeId,
knotId: knotId,
key: key,
destinationType: destinationType,
destinationId: destinationId
});

// verify the knot is found
this.knotService.findById(knotId)
.then(() => {
anOutcome.save()
.then((outcome: IOutcome) => resolve(outcome))
.catch(err => reject(err));
})
.catch(err => reject(err))
destinationType: DestinationTypes, destinationId: string): Observable<IOutcome> {

const anOutcome: IOutcome = new Outcome({
id: uuid.v4(),
narrativeId: narrativeId,
knotId: knotId,
key: key,
destinationType: destinationType,
destinationId: destinationId
});

return from(anOutcome.save());
}

public deleteById(id: string): Observable<any> {
Expand Down
25 changes: 13 additions & 12 deletions modules/common/services/resource.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,32 @@ describe('Service:ResourceService', () => {
it('should throw an error when unable to find by params', (done) => {

service.findByParams(1)
.then(() => {})
.catch(() => {
done();
});
.subscribe(
() => {},
() => done()
);
});

it('should throw an error when unable to find by id', (done) => {

const disruptor: any = {};

service.findById(disruptor)
.then(() => {})
.catch(() => {
done();
});
.subscribe(
() => {},
() => done()
);
});

it('should throw an error when unable to find by id', (done) => {

const disruptor: any = {};

service.findById(disruptor)
.then(() => {})
.catch(() => {
done();
});
.subscribe(
() => {},
() => done()
);
});
});

51 changes: 16 additions & 35 deletions modules/common/services/resource.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,30 @@ export abstract class ResourceService {

model: Model<Document>;

public findByParams(params: any): Promise<Document[]> {
return new Promise((resolve, reject) => {
this.model.find(params)
.then((items: any[]) => resolve(items))
.catch(err => reject(err))
});
public findByParams(params: any): Observable<Document[]> {
return from(this.model.find(params));
}

public findAll(): Promise<Document[]> {
public findAll(): Observable<Document[]> {
return this.findByParams({});
}

public updateById(id: string, updateBody: object): Promise<Document> {
return new Promise((resolve, reject) => {
this.model.findOne({ id: id })
.then((item: Document) => {
Object.keys(updateBody).forEach(
key => {
if(updateBody[key] !== undefined && key !== 'id' && key !== '_id') {
item[key] = updateBody[key];
}
public updateByIdObservable(id: string, updateBody: object): Observable<Document> {
return from(this.model.findOne({ id: id })).pipe(
concatMap((item: Document) => {
Object.keys(updateBody).forEach(
key => {
if(updateBody[key] !== undefined && key !== 'id' && key !== '_id') {
item[key] = updateBody[key];
}
);
item.save()
.then((item: Document) => resolve(item))
.catch(err => reject(err))
})
.catch(err => reject(err))
});
}

public findById(id: string): Promise<Document> {
return new Promise((resolve, reject) => {
this.model.findOne({ id: id })
.then((item: Document) => {
if(item) resolve(item);
reject(null);
})
.catch(err => reject(err))
});
}
);
return from(item.save());
})
);
}

public findByIdObservable(id: string): Observable<Document> {
public findById(id: string): Observable<Document> {
return from(this.model.findOne({ id: id })).pipe(
map((result: Document) => {
if (!result) {
Expand Down
36 changes: 11 additions & 25 deletions modules/knots/components/knot/knot.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ describe('KnotComponent', () => {
component.post('/')
.send({ narrativeId: narrative.id, key: 'test knot', title: 'test title' })
.expect(201)
.end((err, res: IKnot) => {
.end((err, res) => {
if (err) return done(err);
knotService.findById(res.id)
.then((knot: IKnot) => {
expect(knot.id).to.equal(res.id)
done();
})
.catch((err) => done(err));
knotService.findById(res.body.id)
.subscribe(
(knot: IKnot) => {
expect(knot.id).to.equal(res.body.id)
done();
},
(err) => done(err)
);
});
}, (err) => done(err));
});
Expand All @@ -69,28 +71,12 @@ describe('KnotComponent', () => {
}, (err) => done(err));
});

it('should throw an error if a narrative is not associated with a knot', (done) => {

component.post('/')
.send({ narrativeId: 'does-not-exist', key: 'test-key', title: 'test-title' })
.expect(400)
.end((err, res: IKnot) => {
if (err) return done(err);
knotService.findById(res.id)
.then((knot: IKnot) => {
expect(knot.id).to.equal(res.id)
done();
})
.catch((err) => done(err));
});
});

it('should allow deleting a knot', (done) => {

narrativeService.create('testKey', 'test title')
.subscribe((narrative: INarrative) => {
knotService.create(narrative.id, 'testKey', 'title')
.then((knot) => {
.subscribe((knot) => {
component.delete('/' + knot.id)
.expect(204)
.end((err) => {
Expand All @@ -105,7 +91,7 @@ describe('KnotComponent', () => {
narrativeService.create('testKey', 'test title')
.subscribe((narrative: INarrative) => {
knotService.create(narrative.id, 'testKey', 'title')
.then((knot) => {
.subscribe((knot) => {
component.patch('/' + knot.id)
.send({ title: 'anothertesttitle' })
.expect(200)
Expand Down
14 changes: 9 additions & 5 deletions modules/knots/components/knot/knot.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ export class KnotComponent extends ResourceComponent {
@PostMapping()
public createOne(req: HttpRequest, res: HttpResponse): void {
this.mainService.create(req.body.narrativeId, req.body.key, req.body.title)
.then((knot: IKnot) => res.success(knot, 201))
.catch(() => res.errored(400));
.subscribe(
(knot: IKnot) => res.success(knot, 201),
() => res.errored(400)
);
}

@PatchMapping({
Expand All @@ -33,8 +35,10 @@ export class KnotComponent extends ResourceComponent {
title: req.body.title || undefined
};

this.mainService.updateById(req.params.id, updateBody)
.then((item: IKnot) => res.success(item))
.catch((err) => res.errored(400, err));
this.mainService.updateByIdObservable(req.params.id, updateBody)
.subscribe(
(item: IKnot) => res.success(item),
(err) => res.errored(400, err)
);
}
}
24 changes: 14 additions & 10 deletions modules/narratives/components/narrative/narrative.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ describe('NarrativeComponent', () => {
if (err) return done(err);

narrativeService.findById(res.body.id)
.then((narrative) => {
expect(narrative.id).to.equal(res.body.id);
done();
})
.catch((err) => done(err));
.subscribe(
(narrative) => {
expect(narrative.id).to.equal(res.body.id);
done();
},
(err) => done(err)
);
});
});

Expand Down Expand Up @@ -158,11 +160,13 @@ describe('NarrativeComponent', () => {
if (err) return done(err);

narrativeService.findById(narrative.id)
.then((checkNarrative: INarrative) => {
expect(checkNarrative.title).to.equal(newTitle);
done();
})
.catch(() => done(new Error('Unable to find patched narrative')))
.subscribe(
(checkNarrative: INarrative) => {
expect(checkNarrative.title).to.equal(newTitle);
done();
},
() => done(new Error('Unable to find patched narrative'))
);
});
}, () => done(new Error('Unable to create narrative')));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ export class NarrativeComponent extends ResourceComponent {
title: req.body.title || undefined
};

this.mainService.updateById(req.params.id, updateBody)
.then((narrative: INarrative) => res.success(narrative))
.catch((err) => res.errored(400, err));
this.mainService.updateByIdObservable(req.params.id, updateBody)
.subscribe(
(narrative: INarrative) => res.success(narrative),
(err) => res.errored(400, err)
);
}
}
11 changes: 5 additions & 6 deletions modules/outcomes/components/outcome/outcome.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('OutcomeComponent', () => {
narrativeService.create('testKey', 'Test Title')
.subscribe((narrative: INarrative) => {
knotService.create(narrative.id, 'testKey', 'Test Title')
.then((knot: IKnot) => {
.subscribe((knot: IKnot) => {
narrativeId = narrative.id;
knotId = knot.id;
done();
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('OutcomeComponent', () => {

// verify the item made it to storage
outcomeService.findById(res.body.id)
.then(() => {
.subscribe(() => {
done();
});
});
Expand All @@ -102,7 +102,7 @@ describe('OutcomeComponent', () => {

// verify the item made it to storage
outcomeService.findById(res.body.id)
.then(() => {
.subscribe(() => {
done();
});
});
Expand Down Expand Up @@ -136,16 +136,15 @@ describe('OutcomeComponent', () => {
it('should allow patching an existing outcome', (done) => {

outcomeService.create(narrativeId, knotId, 'testOutcome', DestinationTypes.DONE, undefined)
.then((outcome: IOutcome) => {

.subscribe((outcome: IOutcome) => {
component.patch('/' + outcome.id)
.send({ key: 'newKey' })
.expect(200)
.end((err, res: IOutcome) => {
if(err) { return done(err) }

outcomeService.findById(outcome.id)
.then((anOutcome: IOutcome) => {
.subscribe((anOutcome: IOutcome) => {

expect(anOutcome.key).to.equal('newKey');
done();
Expand Down

0 comments on commit dc0a90f

Please sign in to comment.