@mas-soft/mas-core-server
Version:
main application
149 lines • 5.17 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
const common_1 = require("@nestjs/common");
const json_1 = require("../pipes/json");
const util_1 = require("util");
class EntityControler {
constructor(repository) {
this.repository = repository;
}
catch(e) {
if (e instanceof common_1.HttpException)
throw e;
else {
console.dir(e);
}
let msg = "";
if (util_1.isString(e)) {
msg = e;
}
else {
msg = e.Error || e.Message || e.error || e.message || 'Internal Server Error';
}
throw new common_1.HttpException(msg, 500);
}
async add(entity) {
try {
return await this.repository.create(entity);
}
catch (e) {
this.catch(e);
}
}
async get(filter) {
try {
return await this.repository.find(filter);
}
catch (e) {
this.catch(e);
}
}
async count(where) {
try {
return await this.repository.count(where);
}
catch (e) {
this.catch(e);
}
}
async updateAll(entity, where) {
try {
return await this.repository.updateAll(entity, where);
}
catch (e) {
this.catch(e);
}
}
async getOne(filter) {
try {
return await this.repository.findOne(filter);
}
catch (e) {
this.catch(e);
}
}
async edit(id, entity) {
try {
return this.repository.updateById(id, entity);
}
catch (e) {
this.catch(e);
}
}
async delete(id) {
try {
if (!id) {
throw new common_1.HttpException("invalied id", common_1.HttpStatus.BAD_REQUEST);
}
await this.repository.deleteById(id);
}
catch (e) {
this.catch(e);
}
}
}
__decorate([
common_1.Post(),
__param(0, common_1.Body()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], EntityControler.prototype, "add", null);
__decorate([
common_1.Get(),
__param(0, common_1.Query('filter', new json_1.JsonPipe())),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], EntityControler.prototype, "get", null);
__decorate([
common_1.Get('/count'),
__param(0, common_1.Query('where', new json_1.JsonPipe())),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], EntityControler.prototype, "count", null);
__decorate([
common_1.Patch('/all'),
__param(0, common_1.Body()),
__param(1, common_1.Query('where', new json_1.JsonPipe())),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], EntityControler.prototype, "updateAll", null);
__decorate([
common_1.Get('/one'),
__param(0, common_1.Query('filter', new json_1.JsonPipe())),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], EntityControler.prototype, "getOne", null);
__decorate([
common_1.Put(),
__param(0, common_1.Query('id', new json_1.JsonPipe())),
__param(1, common_1.Body()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], EntityControler.prototype, "edit", null);
__decorate([
common_1.Delete(),
__param(0, common_1.Query('id', new json_1.JsonPipe())),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], EntityControler.prototype, "delete", null);
exports.EntityControler = EntityControler;
//# sourceMappingURL=entity-controller.js.map