22 Authorized ,
33 Body ,
44 CurrentUser ,
5+ ForbiddenError ,
56 Get ,
67 JsonController ,
78 OnNull ,
@@ -12,45 +13,53 @@ import {
1213} from 'routing-controllers' ;
1314import { ResponseSchema } from 'routing-controllers-openapi' ;
1415
15- import { BaseFilter , dataSource , Organization , Place , PlaceListChunk , User } from '../../model' ;
16+ import {
17+ BaseFilter ,
18+ dataSource ,
19+ Forum ,
20+ Organization ,
21+ Place ,
22+ PlaceListChunk ,
23+ User
24+ } from '../../model' ;
1625import { searchConditionOf } from '../../utility' ;
1726import { ActivityLogController } from '../User' ;
18- import { OrganizationController } from './Organization' ;
1927
20- const placeStore = dataSource . getRepository ( Place ) ;
28+ const placeStore = dataSource . getRepository ( Place ) ,
29+ forumStore = dataSource . getRepository ( Forum ) ;
2130
22- @JsonController ( '/organization ' )
31+ @JsonController ( '/place ' )
2332export class PlaceController {
24- @Post ( '/:oid/place' )
33+ static async assertAdmin ( user : User , id : number ) {
34+ const isPlaceUser = await forumStore . exists ( {
35+ where : { place : { id } , activity : { organization : { members : { id : user . id } } } }
36+ } ) ;
37+ if ( ! isPlaceUser ) throw new ForbiddenError ( 'You never used this place' ) ;
38+ }
39+
40+ @Post ( )
2541 @Authorized ( )
2642 @ResponseSchema ( Place )
27- async create ( @CurrentUser ( ) createdBy : User , @Param ( 'oid' ) oid : number , @Body ( ) body : Place ) {
28- await OrganizationController . assertAdmin ( createdBy , oid ) ;
29-
30- const place = await placeStore . save ( { ...body , organization : { id : oid } , createdBy } ) ;
43+ async create ( @CurrentUser ( ) createdBy : User , @Body ( ) body : Place ) {
44+ const place = await placeStore . save ( { ...body , createdBy } ) ;
3145
3246 await ActivityLogController . logCreate ( createdBy , 'Place' , place . id ) ;
3347
3448 return place ;
3549 }
3650
37- @Get ( '/:oid/place/: id' )
51+ @Get ( '/:id' )
3852 @OnNull ( 404 )
3953 @ResponseSchema ( Place )
4054 getOne ( @Param ( 'id' ) id : number ) {
41- return placeStore . findOneBy ( { id } ) ;
55+ return placeStore . findOne ( { where : { id } , relations : [ 'organization' , 'createdBy' ] } ) ;
4256 }
4357
44- @Put ( '/:oid/place/: id' )
58+ @Put ( '/:id' )
4559 @Authorized ( )
4660 @ResponseSchema ( Place )
47- async edit (
48- @CurrentUser ( ) updatedBy : User ,
49- @Param ( 'oid' ) oid : number ,
50- @Param ( 'id' ) id : number ,
51- @Body ( ) body : Place
52- ) {
53- await OrganizationController . assertAdmin ( updatedBy , oid ) ;
61+ async edit ( @CurrentUser ( ) updatedBy : User , @Param ( 'id' ) id : number , @Body ( ) body : Place ) {
62+ await PlaceController . assertAdmin ( updatedBy , id ) ;
5463
5564 const place = await placeStore . save ( { ...body , id, updatedBy } ) ;
5665
@@ -59,15 +68,11 @@ export class PlaceController {
5968 return place ;
6069 }
6170
62- @Get ( '/:oid/place' )
71+ @Get ( )
6372 @ResponseSchema ( PlaceListChunk )
64- async getList (
65- @Param ( 'oid' ) oid : number ,
66- @QueryParams ( ) { keywords, pageSize = 10 , pageIndex = 1 } : BaseFilter
67- ) {
68- const where = searchConditionOf < Place > ( [ 'name' , 'address' ] , keywords , {
69- organization : { id : oid }
70- } ) ;
73+ async getList ( @QueryParams ( ) { keywords, pageSize = 10 , pageIndex = 1 } : BaseFilter ) {
74+ const where = searchConditionOf < Place > ( [ 'name' , 'address' ] , keywords ) ;
75+
7176 const [ list , count ] = await placeStore . findAndCount ( {
7277 where,
7378 skip : pageSize * ( pageIndex - 1 ) ,
0 commit comments