Skip to content

Commit

Permalink
[feat] add room search api
Browse files Browse the repository at this point in the history
  • Loading branch information
erik1110 committed Jan 4, 2024
1 parent e601014 commit aa8d439
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
33 changes: 32 additions & 1 deletion src/features/room/room.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,39 @@ import { RoomService } from './room.service';
import { Roles } from 'src/auth/decorators/roles.decorator';
import { CreateRoomDto, CreateRoomSuccessDto, DeleteRoomSuccessDto, GetRoomSuccessDto, UpdateRoomSuccessDto } from './dto/room.dto';
import { IsObjectIdPipe } from 'nestjs-object-id';
import { AuthGuard } from '@nestjs/passport';


@ApiTags('Rooms - 房型')
@ApiErrorDecorator(
HttpStatus.INTERNAL_SERVER_ERROR,
'CriticalError',
'系統錯誤,請洽系統管理員',
)
@Controller('/api/v1/rooms')
export class RoomController {
constructor(private readonly roomService: RoomService) {}

@Get('')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: '取得所有房型 Get all rooms' })
@ApiOkResponse({ type: GetRoomSuccessDto })
async getallRooms(@Req() req: Request) {
return await this.roomService.getallRooms(req);
}

@Get(':id')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: '取得單一房型 Get a room' })
@ApiOkResponse({ type: GetRoomSuccessDto })
async getRoomById(
@Param('id', IsObjectIdPipe) id: string,
@Req() req: Request) {
return await this.roomService.getRoomById(id, req);
}

}

@ApiTags('Admin/Rooms - 房型管理')
@UseGuards(RolesGuard)
@ApiBearerAuth()
Expand All @@ -18,7 +49,7 @@ import { IsObjectIdPipe } from 'nestjs-object-id';
'系統錯誤,請洽系統管理員',
)
@Controller('api/v1/admin/rooms')
export class RoomController {
export class RoomAdminController {
constructor(private readonly roomService: RoomService) {}

@Get('')
Expand Down
4 changes: 2 additions & 2 deletions src/features/room/room.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { Module } from '@nestjs/common';
import { RoomService } from './room.service';
import { RoomSchema } from './schemas/room.schema';
import { MongooseModule } from '@nestjs/mongoose';
import { RoomController } from './room.controller';
import { RoomAdminController, RoomController } from './room.controller';

@Module({
imports: [MongooseModule.forFeature([{ name: 'Room', schema: RoomSchema }])],
controllers: [RoomController],
controllers: [RoomAdminController, RoomController],
providers: [RoomService]
})
export class RoomModule {}
19 changes: 17 additions & 2 deletions src/features/room/room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,28 @@ export class RoomService {
async getallRooms(req: Request) {
const result = await this.roomModel.find({
status: 1
});
}, '_id');
const ids = result.map(order => order._id.toString());
return getHttpResponse.successResponse({
message: '取得所有房型',
data: result,
data: ids,
});
}

async getRoomById(id: string, req: Request) {
const result = await this.roomModel.findOne({
_id: id,
status: 1
});
if (!result) {
throw new AppError(HttpStatus.NOT_FOUND, 'UserError', '此房型不存在');
}
return getHttpResponse.successResponse({
message: '取得單一房型',
data: result,
});
}

async createRoom(req: Request, createRoomDto: CreateRoomDto) {
const room = new this.roomModel(createRoomDto);
room.status = 1
Expand Down

0 comments on commit aa8d439

Please sign in to comment.