Skip to content

Commit 9066c9c

Browse files
committed
【新增】捐款接收方模型、接口 (closes #7)
1 parent 7e507a6 commit 9066c9c

9 files changed

+131
-5
lines changed

ReadMe.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
7. [酒店管理](source/controller/Hotel.ts)
1717
8. [供应商管理](source/controller/Vendor.ts)
1818
9. [义诊管理](source/controller/Clinic.ts)
19+
10. [捐款接收方管理](source/controller/DonationRecipient.ts)
1920

2021
## 环境变量
2122

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@wuhan2020/rest-api",
3-
"version": "0.8.0",
3+
"version": "1.0.0",
44
"description": "Node.js Back-end project scaffold based on Koa, TypeScript & LeanCloud",
55
"author": "shiy2008@gmail.com",
66
"private": true,
@@ -32,7 +32,7 @@
3232
"@typescript-eslint/parser": "^2.18.0",
3333
"eslint": "^6.8.0",
3434
"husky": "^4.2.1",
35-
"lint-staged": "^10.0.6",
35+
"lint-staged": "^10.0.7",
3636
"nodemon": "^2.0.2",
3737
"prettier": "^1.19.1",
3838
"ts-node": "^8.6.2",

source/WebServer.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
LogisticsController,
1616
HotelController,
1717
VendorController,
18-
ClinicController
18+
ClinicController,
19+
DonationRecipientController
1920
} from './controller';
2021

2122
const {
@@ -45,6 +46,7 @@ useKoaServer(app, {
4546
cors: { credentials: true },
4647
authorizationChecker: ({ context }) => !!(context as LCContext).currentUser,
4748
controllers: [
49+
DonationRecipientController,
4850
ClinicController,
4951
VendorController,
5052
HotelController,
+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { Object as LCObject, Query, ACL } from 'leanengine';
2+
import {
3+
JsonController,
4+
Post,
5+
Authorized,
6+
Ctx,
7+
Body,
8+
ForbiddenError,
9+
Get,
10+
QueryParam,
11+
Param,
12+
Put,
13+
Delete,
14+
OnUndefined
15+
} from 'routing-controllers';
16+
17+
import { LCContext, queryPage } from '../utility';
18+
import { DonationRecipientModel } from '../model';
19+
import { RoleController } from './Role';
20+
21+
export class DonationRecipient extends LCObject {}
22+
23+
@JsonController('/donation/recipient')
24+
export class DonationRecipientController {
25+
@Post()
26+
@Authorized()
27+
async create(
28+
@Ctx() { currentUser: user }: LCContext,
29+
@Body() { name, ...rest }: DonationRecipientModel
30+
) {
31+
let donationRecipient = await new Query(DonationRecipient)
32+
.equalTo('name', name)
33+
.first();
34+
35+
if (donationRecipient)
36+
throw new ForbiddenError(
37+
'同一捐款接收方不能重复发布,请联系原发布者修改'
38+
);
39+
40+
const acl = new ACL();
41+
42+
acl.setPublicReadAccess(true),
43+
acl.setPublicWriteAccess(false),
44+
acl.setWriteAccess(user, true),
45+
acl.setRoleWriteAccess(await RoleController.getAdmin(), true);
46+
47+
donationRecipient = await new DonationRecipient()
48+
.setACL(acl)
49+
.save({ ...rest, name, creator: user }, { user });
50+
51+
return donationRecipient.toJSON();
52+
}
53+
54+
@Get()
55+
getList(
56+
@QueryParam('pageSize') size: number,
57+
@QueryParam('pageIndex') index: number
58+
) {
59+
return queryPage(DonationRecipient, {
60+
include: ['creator'],
61+
size,
62+
index
63+
});
64+
}
65+
66+
@Get('/:id')
67+
async getOne(@Param('id') id: string) {
68+
const donationRecipient = await new Query(DonationRecipient).get(id);
69+
70+
return donationRecipient.toJSON();
71+
}
72+
73+
@Put('/:id')
74+
@Authorized()
75+
async edit(
76+
@Ctx() { currentUser: user }: LCContext,
77+
@Param('id') id: string,
78+
@Body() { name, ...rest }: DonationRecipientModel
79+
) {
80+
let donationRecipient = LCObject.createWithoutData(
81+
'DonationRecipient',
82+
id
83+
);
84+
await donationRecipient.save(rest, { user });
85+
86+
donationRecipient = await new Query(DonationRecipient)
87+
.include('creator')
88+
.get(id);
89+
90+
return donationRecipient.toJSON();
91+
}
92+
93+
@Delete('/:id')
94+
@Authorized()
95+
@OnUndefined(204)
96+
async delete(
97+
@Ctx() { currentUser: user }: LCContext,
98+
@Param('id') id: string
99+
) {
100+
await LCObject.createWithoutData('DonationRecipient', id).destroy({
101+
user
102+
});
103+
}
104+
}

source/controller/Logistics.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from 'routing-controllers';
1616

1717
import { LCContext, queryPage } from '../utility';
18-
import { LogisticsModel } from '../model/Logistics';
18+
import { LogisticsModel } from '../model';
1919
import { RoleController } from './Role';
2020

2121
export class Logistics extends LCObject {}

source/controller/SuppliesRequirement.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from 'routing-controllers';
1616

1717
import { LCContext, queryPage } from '../utility';
18-
import { RequirementModel } from '../model/Supplies';
18+
import { RequirementModel } from '../model';
1919
import { RoleController } from './Role';
2020

2121
export class SuppliesRequirement extends LCObject {}

source/controller/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export * from './Logistics';
88
export * from './Hotel';
99
export * from './Vendor';
1010
export * from './Clinic';
11+
export * from './DonationRecipient';

source/model/Donation.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Length, IsArray } from 'class-validator';
2+
3+
import { OrganizationModel } from './Place';
4+
5+
export interface BankAccount {
6+
name: string;
7+
number: string;
8+
bank: string;
9+
}
10+
11+
export class DonationRecipientModel extends OrganizationModel {
12+
@Length(2)
13+
name: string;
14+
15+
@IsArray()
16+
accounts: BankAccount[];
17+
}

source/model/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './Logistics';
55
export * from './Hotel';
66
export * from './Vendor';
77
export * from './Clinic';
8+
export * from './Donation';

0 commit comments

Comments
 (0)