Add endpoint to get community by UUID

This commit is contained in:
faris Aljohari
2024-04-01 00:04:38 +03:00
parent d765d090ea
commit ae8d909268
4 changed files with 71 additions and 0 deletions

View File

@ -2,8 +2,10 @@ import { CommunityService } from '../services/community.service';
import {
Body,
Controller,
Get,
HttpException,
HttpStatus,
Param,
Post,
UseGuards,
} 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,
);
}
}
}
}

View 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;
}

View 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;
};
}

View File

@ -1,6 +1,7 @@
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { SpaceRepository } from '@app/common/modules/space/repositories';
import { AddCommunityDto } from '../dtos';
import { GetCommunityByUuidInterface } from '../interface/community.interface';
@Injectable()
export class CommunityService {
@ -17,4 +18,27 @@ export class CommunityService {
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,
);
}
}
}