mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 13:44:55 +00:00
Add endpoint to get community by UUID
This commit is contained in:
@ -2,8 +2,10 @@ import { CommunityService } from '../services/community.service';
|
|||||||
import {
|
import {
|
||||||
Body,
|
Body,
|
||||||
Controller,
|
Controller,
|
||||||
|
Get,
|
||||||
HttpException,
|
HttpException,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
|
Param,
|
||||||
Post,
|
Post,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
@ -33,4 +35,25 @@ export class CommunityController {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get(':communityUuid')
|
||||||
|
async getCommunityByUuid(@Param('communityUuid') communityUuid: string) {
|
||||||
|
try {
|
||||||
|
const community =
|
||||||
|
await this.communityService.getCommunityByUuid(communityUuid);
|
||||||
|
|
||||||
|
return community;
|
||||||
|
} catch (error) {
|
||||||
|
if (error.status === 404) {
|
||||||
|
throw new HttpException('Community not found', HttpStatus.NOT_FOUND);
|
||||||
|
} else {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/community/dtos/get.community.dto.ts
Normal file
12
src/community/dtos/get.community.dto.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
|
||||||
|
export class GetCommunityDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'communityUuid',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public communityUuid: string;
|
||||||
|
}
|
||||||
12
src/community/interface/community.interface.ts
Normal file
12
src/community/interface/community.interface.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
export interface GetCommunityByUuidInterface {
|
||||||
|
uuid: string;
|
||||||
|
createdAt: Date;
|
||||||
|
updatedAt: Date;
|
||||||
|
spaceName: string;
|
||||||
|
spaceType: {
|
||||||
|
uuid: string;
|
||||||
|
createdAt: Date;
|
||||||
|
updatedAt: Date;
|
||||||
|
type: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -1,6 +1,7 @@
|
|||||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { AddCommunityDto } from '../dtos';
|
import { AddCommunityDto } from '../dtos';
|
||||||
|
import { GetCommunityByUuidInterface } from '../interface/community.interface';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CommunityService {
|
export class CommunityService {
|
||||||
@ -17,4 +18,27 @@ export class CommunityService {
|
|||||||
throw new HttpException(err.message, HttpStatus.INTERNAL_SERVER_ERROR);
|
throw new HttpException(err.message, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getCommunityByUuid(
|
||||||
|
communityUuid: string,
|
||||||
|
): Promise<GetCommunityByUuidInterface> {
|
||||||
|
try {
|
||||||
|
const community: GetCommunityByUuidInterface =
|
||||||
|
await this.spaceRepository.findOne({
|
||||||
|
where: {
|
||||||
|
uuid: communityUuid,
|
||||||
|
},
|
||||||
|
relations: ['spaceType'],
|
||||||
|
});
|
||||||
|
if (!community) {
|
||||||
|
throw new HttpException('Community not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
return community;
|
||||||
|
} catch (err) {
|
||||||
|
throw new HttpException(
|
||||||
|
err.message,
|
||||||
|
err.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user