Skip to content
This repository has been archived by the owner on Dec 23, 2021. It is now read-only.

Commit

Permalink
feat: support PUT method for odata
Browse files Browse the repository at this point in the history
  • Loading branch information
Soontao committed Jul 30, 2020
1 parent d01e7e4 commit a0fe2bd
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/lib/typeorm/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,41 @@ export class TypedController<T extends typeof BaseODataModel = any> extends ODat
@odata.POST
async create(@odata.body body, @odata.context ctx: ODataHttpContext) {
const repo = await this._getRepository(ctx);
await this._executeHooks({ context: ctx, hookType: HookType.beforeCreate, data: body });
return repo.save(body);
await this._executeHooks({ context: ctx, hookType: HookType.beforeCreate, data: body });
// query the created item
const { identifiers: [id] } = await repo.insert(body);
// and return it
return this.findOne(id, ctx);
}

// odata patch will response no content
// create or update
@odata.PUT
async save(@odata.key key, @odata.body body, @odata.context ctx: ODataHttpContext) {
const repo = await this._getRepository(ctx);
if (key) {
const item = await repo.findOne(key);
// if exist
if (item) {
return this.update(key, body, ctx);
}
}
return this.create(body, ctx);
}

// odata patch will not response any content
@odata.PATCH
async update(@odata.key key, @odata.body body, @odata.context ctx: ODataHttpContext) {
const repo = await this._getRepository(ctx);
await this._executeHooks({ context: ctx, hookType: HookType.beforeUpdate, data: body, key });
return repo.update(key, body);
await this._executeHooks({ context: ctx, hookType: HookType.beforeUpdate, data: body, key });
await repo.update(key, body);
}

// odata delete will response no content
// odata delete will not response any content
@odata.DELETE
async delete(@odata.key key, @odata.context ctx: ODataHttpContext) {
const repo = await this._getRepository(ctx);
await this._executeHooks({ context: ctx, hookType: HookType.beforeDelete, key });
return repo.delete(key);
await this._executeHooks({ context: ctx, hookType: HookType.beforeDelete, key });
await repo.delete(key);
}

}
Expand Down

0 comments on commit a0fe2bd

Please sign in to comment.