|
| 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 | +} |
0 commit comments