1 | "use strict";
|
2 | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
3 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
4 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
5 | 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;
|
6 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
7 | };
|
8 | var __metadata = (this && this.__metadata) || function (k, v) {
|
9 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
10 | };
|
11 | var __param = (this && this.__param) || function (paramIndex, decorator) {
|
12 | return function (target, key) { decorator(target, key, paramIndex); }
|
13 | };
|
14 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
15 | return new (P || (P = Promise))(function (resolve, reject) {
|
16 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
17 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
18 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
19 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
20 | });
|
21 | };
|
22 | Object.defineProperty(exports, "__esModule", { value: true });
|
23 | const common_1 = require("@nestjs/common");
|
24 | const typeorm_1 = require("@nestjs/typeorm");
|
25 | const typeorm_2 = require("typeorm");
|
26 | const score_type_entity_1 = require("../model/score.type.entity");
|
27 | let ScoreTypeService = class ScoreTypeService {
|
28 | constructor(scoreTypeRepository) {
|
29 | this.scoreTypeRepository = scoreTypeRepository;
|
30 | }
|
31 | getAll() {
|
32 | return __awaiter(this, void 0, void 0, function* () {
|
33 | return this.scoreTypeRepository.find();
|
34 | });
|
35 | }
|
36 | createScoreType(name, type, description) {
|
37 | return __awaiter(this, void 0, void 0, function* () {
|
38 | const exist = yield this.scoreTypeRepository.findOne({ name });
|
39 | if (exist) {
|
40 | throw new common_1.HttpException("指定名称name=" + name + "积分类型已存在", 424);
|
41 | }
|
42 | const scoreType = this.scoreTypeRepository.create({ name, type, default: false, description });
|
43 | try {
|
44 | yield this.scoreTypeRepository.save(scoreType);
|
45 | }
|
46 | catch (err) {
|
47 | throw new common_1.HttpException("数据库错误" + err.toString(), 401);
|
48 | }
|
49 | });
|
50 | }
|
51 | updateScoreType(id, name, type, description) {
|
52 | return __awaiter(this, void 0, void 0, function* () {
|
53 | const original = yield this.scoreTypeRepository.findOneById(id);
|
54 | if (!original) {
|
55 | throw new common_1.HttpException("指定id=" + id + "积分类型不存在", 425);
|
56 | }
|
57 | if (original.default) {
|
58 | throw new common_1.HttpException("默认积分类型不允许更改", 426);
|
59 | }
|
60 | if (name !== original.name) {
|
61 | const exist = yield this.scoreTypeRepository.findOne({ name });
|
62 | if (exist) {
|
63 | throw new common_1.HttpException("指定名称name=" + name + "积分类型已存在", 424);
|
64 | }
|
65 | }
|
66 | try {
|
67 | original.name = name;
|
68 | original.type = type;
|
69 | original.description = description;
|
70 | yield this.scoreTypeRepository.save(original);
|
71 | }
|
72 | catch (err) {
|
73 | throw new common_1.HttpException("数据库错误" + err.toString(), 401);
|
74 | }
|
75 | });
|
76 | }
|
77 | deleteScoreType(id) {
|
78 | return __awaiter(this, void 0, void 0, function* () {
|
79 | const exist = yield this.scoreTypeRepository.findOneById(id);
|
80 | if (!exist) {
|
81 | throw new common_1.HttpException("指定id=" + id + "积分类型不存在", 425);
|
82 | }
|
83 | if (exist.default) {
|
84 | throw new common_1.HttpException("默认积分类型不允许删除", 426);
|
85 | }
|
86 | try {
|
87 | yield this.scoreTypeRepository.remove(exist);
|
88 | }
|
89 | catch (err) {
|
90 | throw new common_1.HttpException("数据库错误" + err.toString(), 401);
|
91 | }
|
92 | });
|
93 | }
|
94 | deleteScoreTypes(ids) {
|
95 | return __awaiter(this, void 0, void 0, function* () {
|
96 | const exists = yield this.scoreTypeRepository.findByIds(ids);
|
97 | ids.forEach(id => {
|
98 | const find = exists.find(exist => {
|
99 | return exist.id === id;
|
100 | });
|
101 | if (!find) {
|
102 | throw new common_1.HttpException("指定id=" + id + "积分类型不存在", 425);
|
103 | }
|
104 | if (find.default) {
|
105 | throw new common_1.HttpException("默认积分类型不允许删除", 426);
|
106 | }
|
107 | });
|
108 | try {
|
109 | yield this.scoreTypeRepository.remove(exists);
|
110 | }
|
111 | catch (err) {
|
112 | throw new common_1.HttpException("数据库错误" + err.toString(), 401);
|
113 | }
|
114 | });
|
115 | }
|
116 | };
|
117 | ScoreTypeService = __decorate([
|
118 | common_1.Component(),
|
119 | __param(0, typeorm_1.InjectRepository(score_type_entity_1.ScoreType)),
|
120 | __metadata("design:paramtypes", [typeorm_2.Repository])
|
121 | ], ScoreTypeService);
|
122 | exports.ScoreTypeService = ScoreTypeService;
|