import { CommunityService } from '../services/community.service'; import { Body, Controller, Get, HttpException, HttpStatus, Param, Post, Put, Query, UseGuards, } from '@nestjs/common'; import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard'; import { AddCommunityDto, AddUserCommunityDto, } from '../dtos/add.community.dto'; import { GetCommunityChildDto } from '../dtos/get.community.dto'; import { UpdateCommunityNameDto } from '../dtos/update.community.dto'; import { CheckUserCommunityGuard } from 'src/guards/user.community.guard'; import { CommunityPermissionGuard } from 'src/guards/community.permission.guard'; @ApiTags('Community Module') @Controller({ version: '1', path: 'community', }) export class CommunityController { constructor(private readonly communityService: CommunityService) {} @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Post() async addCommunity(@Body() addCommunityDto: AddCommunityDto) { try { const community = await this.communityService.addCommunity(addCommunityDto); return { statusCode: HttpStatus.CREATED, success: true, message: 'Community added successfully', data: community, }; } catch (error) { throw new HttpException( error.message || 'Internal server error', error.status || HttpStatus.INTERNAL_SERVER_ERROR, ); } } @ApiBearerAuth() @UseGuards(JwtAuthGuard, CommunityPermissionGuard) @Get(':communityUuid') async getCommunityByUuid(@Param('communityUuid') communityUuid: string) { try { const community = await this.communityService.getCommunityByUuid(communityUuid); return community; } catch (error) { throw new HttpException( error.message || 'Internal server error', error.status || HttpStatus.INTERNAL_SERVER_ERROR, ); } } @ApiBearerAuth() @UseGuards(JwtAuthGuard, CommunityPermissionGuard) @Get('child/:communityUuid') async getCommunityChildByUuid( @Param('communityUuid') communityUuid: string, @Query() query: GetCommunityChildDto, ) { try { const community = await this.communityService.getCommunityChildByUuid( communityUuid, query, ); return community; } catch (error) { throw new HttpException( error.message || 'Internal server error', error.status || HttpStatus.INTERNAL_SERVER_ERROR, ); } } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get('user/:userUuid') async getCommunitiesByUserId(@Param('userUuid') userUuid: string) { try { return await this.communityService.getCommunitiesByUserId(userUuid); } catch (error) { throw new HttpException( error.message || 'Internal server error', error.status || HttpStatus.INTERNAL_SERVER_ERROR, ); } } @ApiBearerAuth() @UseGuards(JwtAuthGuard, CheckUserCommunityGuard) @Post('user') async addUserCommunity(@Body() addUserCommunityDto: AddUserCommunityDto) { try { await this.communityService.addUserCommunity(addUserCommunityDto); return { statusCode: HttpStatus.CREATED, success: true, message: 'user community added successfully', }; } catch (error) { throw new HttpException( error.message || 'Internal server error', error.status || HttpStatus.INTERNAL_SERVER_ERROR, ); } } @ApiBearerAuth() @UseGuards(JwtAuthGuard, CommunityPermissionGuard) @Put('rename/:communityUuid') async renameCommunityByUuid( @Param('communityUuid') communityUuid: string, @Body() updateCommunityDto: UpdateCommunityNameDto, ) { try { const community = await this.communityService.renameCommunityByUuid( communityUuid, updateCommunityDto, ); return community; } catch (error) { throw new HttpException( error.message || 'Internal server error', error.status || HttpStatus.INTERNAL_SERVER_ERROR, ); } } }