UNPKG

7.81 kBJavaScriptView Raw
1"use strict";
2var __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};
8var __metadata = (this && this.__metadata) || function (k, v) {
9 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10};
11var __param = (this && this.__param) || function (paramIndex, decorator) {
12 return function (target, key) { decorator(target, key, paramIndex); }
13};
14var __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};
22Object.defineProperty(exports, "__esModule", { value: true });
23const common_1 = require("@nestjs/common");
24const typeorm_1 = require("@nestjs/typeorm");
25const typeorm_2 = require("typeorm");
26const info_group_entity_1 = require("../model/info.group.entity");
27const info_item_entity_1 = require("../model/info.item.entity");
28let InfoGroupService = class InfoGroupService {
29 constructor(infoItemRepository, infoGroupRepository) {
30 this.infoItemRepository = infoItemRepository;
31 this.infoGroupRepository = infoGroupRepository;
32 }
33 getAll() {
34 return __awaiter(this, void 0, void 0, function* () {
35 return this.infoGroupRepository.find();
36 });
37 }
38 getInfoItems(id) {
39 return __awaiter(this, void 0, void 0, function* () {
40 const infoGroup = yield this.infoGroupRepository.findOne(id, { relations: ["items"] });
41 return infoGroup ? infoGroup.items : undefined;
42 });
43 }
44 createInfoGroup(name) {
45 return __awaiter(this, void 0, void 0, function* () {
46 const exist = yield this.infoGroupRepository.findOne({ name });
47 if (exist) {
48 throw new common_1.HttpException(`给定名称name=${name}信息组已存在`, 407);
49 }
50 const infoGroup = this.infoGroupRepository.create({ name, default: false, status: true });
51 try {
52 yield this.infoGroupRepository.save(infoGroup);
53 }
54 catch (err) {
55 throw new common_1.HttpException(`数据库错误:${err.toString()}`, 401);
56 }
57 });
58 }
59 updateInfoGroup(id, name) {
60 return __awaiter(this, void 0, void 0, function* () {
61 const exist = yield this.infoGroupRepository.findOne(id);
62 if (!exist) {
63 throw new common_1.HttpException(`给定id=${id}信息组不存在`, 408);
64 }
65 if (exist.default) {
66 throw new common_1.HttpException("默认信息组不可更改", 408);
67 }
68 if (name !== exist.name) {
69 const exist1 = yield this.infoGroupRepository.findOne({ name });
70 if (exist1) {
71 throw new common_1.HttpException(`指定名称信息组已存在:${name}`, 408);
72 }
73 }
74 try {
75 exist.name = name;
76 yield this.infoGroupRepository.save(exist);
77 }
78 catch (err) {
79 throw new common_1.HttpException(`数据库错误:${err.toString()}`, 401);
80 }
81 });
82 }
83 deleteInfoGroup(id) {
84 return __awaiter(this, void 0, void 0, function* () {
85 const exist = yield this.infoGroupRepository.findOne(id);
86 if (!exist) {
87 throw new common_1.HttpException(`给定id=${id}信息组不存在`, 408);
88 }
89 if (exist.default) {
90 throw new common_1.HttpException("默认信息组不可删除", 408);
91 }
92 try {
93 yield this.infoGroupRepository.remove(exist);
94 }
95 catch (err) {
96 throw new common_1.HttpException(`数据库错误:${err.toString()}`, 401);
97 }
98 });
99 }
100 addInfoItem(id, infoItemId) {
101 return __awaiter(this, void 0, void 0, function* () {
102 const group = yield this.infoGroupRepository.findOne(id, { relations: ["items"] });
103 if (!group) {
104 throw new common_1.HttpException(`给定id=${id}信息组不存在`, 408);
105 }
106 if (group.default) {
107 throw new common_1.HttpException("默认信息组不可更改", 408);
108 }
109 const item = yield this.infoItemRepository.findOne(infoItemId);
110 if (!item) {
111 throw new common_1.HttpException(`指定id=${infoItemId}信息项不存在`, 409);
112 }
113 if (item.default) {
114 throw new common_1.HttpException("默认信息项不可添加", 408);
115 }
116 const find = group.items.find(item => {
117 return item.id === id;
118 });
119 if (find) {
120 throw new common_1.HttpException(`指定信息项id=${infoItemId}已经存在于指定信息组id=${id}中`, 410);
121 }
122 try {
123 group.items.push(item);
124 yield this.infoGroupRepository.save(group);
125 }
126 catch (err) {
127 throw new common_1.HttpException(`数据库错误:${err.toString()}`, 401);
128 }
129 });
130 }
131 removeInfoItem(id, infoItemId) {
132 return __awaiter(this, void 0, void 0, function* () {
133 const group = yield this.infoGroupRepository.findOne(id, { relations: ["items"] });
134 if (!group) {
135 throw new common_1.HttpException(`给定id=${id}信息组不存在`, 408);
136 }
137 if (group.default) {
138 throw new common_1.HttpException("默认信息组不可更改", 408);
139 }
140 const item = yield this.infoItemRepository.findOne(infoItemId);
141 if (!item) {
142 throw new common_1.HttpException(`指定id=${infoItemId}信息项不存在`, 409);
143 }
144 const index = group.items.findIndex(item => {
145 return item.id === id;
146 });
147 if (index < 0) {
148 throw new common_1.HttpException(`指定信息项id=${infoItemId}不存在于指定信息组id=${id}中`, 411);
149 }
150 try {
151 group.items.splice(index, 1);
152 yield this.infoGroupRepository.save(group);
153 }
154 catch (err) {
155 throw new common_1.HttpException(`数据库错误:${err.toString()}`, 401);
156 }
157 });
158 }
159};
160InfoGroupService = __decorate([
161 common_1.Injectable(),
162 __param(0, typeorm_1.InjectRepository(info_item_entity_1.InfoItem)),
163 __param(1, typeorm_1.InjectRepository(info_group_entity_1.InfoGroup)),
164 __metadata("design:paramtypes", [typeorm_2.Repository,
165 typeorm_2.Repository])
166], InfoGroupService);
167exports.InfoGroupService = InfoGroupService;
168
169//# sourceMappingURL=info.group.service.js.map