Skip to content

Commit

Permalink
Feat : 장소 별점 인터셉터 생성
Browse files Browse the repository at this point in the history
소수점 1의 자리수 까지 반올림하여 반환
  • Loading branch information
YubinShin committed Oct 16, 2023
1 parent 515de2d commit 4d58082
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/places/interceptor/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./place-star-rating.interceptor"
35 changes: 35 additions & 0 deletions src/places/interceptor/place-star-rating.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable, throwError } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { LoggerService } from 'src/logger/logger.service';

@Injectable()
export class PlaceStarRatingInterceptor implements NestInterceptor {
constructor(private readonly logger: LoggerService) {}

intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(
map((data) => {
if (Array.isArray(data)) {
// data가 배열인 경우 각 요소의 place_star_rating을 반올림하여 변경
const result = data.map((item) => {
if (item.place_star_rating !== undefined) {
item.place_star_rating = parseFloat(item.place_star_rating.toFixed(1));
}
});
return result; // 배열을 반환해야 합니다.
} else if (data && data.place_star_rating !== undefined) {
// data가 객체인 경우 place_star_rating을 반올림하여 변경
data.place_star_rating = parseFloat(data.place_star_rating.toFixed(1));
return data; // 객체를 반환해야 합니다.
}
return data;
}),
catchError((error) => {
// 오류 발생 시 오류 로그를 출력하고 다시 오류를 throw합니다.
this.logger.error('Error in PlaceStarRatingInterceptor:', error);
return throwError(error);
}),
);
}
}
44 changes: 26 additions & 18 deletions src/places/places.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import {
landmarksResponse,
} from "./responses";
import { UserSubCheckInterceptor } from "src/common/interceptor";
import { PlaceStarRatingInterceptor } from "./interceptor";

@ApiTags("places")
@Controller("places")
@UseInterceptors(PlaceStarRatingInterceptor)
export class PlacesController {
constructor(
private readonly placesService: PlacesService,
Expand All @@ -40,15 +42,16 @@ export class PlacesController {
description: "랜드마크 목록을 성공적으로 가져왔습니다.",
type: landmarksResponse,
})
async getLandmarks(@Res() res) {
async getLandmarks() {
try {
const result = await this.placesService.getLandmarks();
// 클라이언트에게 결과를 응답으로 보내기
return res.status(HttpStatus.OK).json(result);
return result;
// return res.status(HttpStatus.OK).json(result);
} catch (error) {
return res
.status(HttpStatus.NOT_FOUND)
.json({ message: "Error occurred during search." });
// return res
// .status(HttpStatus.NOT_FOUND)
// .json({ message: "Error occurred during search." });
}
}
/** -------------------- */
Expand All @@ -70,7 +73,8 @@ export class PlacesController {
): Promise<void> {
try {
const searchResult = await this.placesService.findSimplePlacesInfo(query);
res.status(HttpStatus.OK).json(searchResult);
return searchResult;
// res.status(HttpStatus.OK).json(searchResult);
} catch (error) {
res
.status(HttpStatus.NOT_FOUND)
Expand All @@ -97,7 +101,7 @@ export class PlacesController {
@Query("x") xCoordinate: number,
@Query("y") yCoordinate: number,
@Res() res
): Promise<void> {
): Promise<any> {
try {
this.logger.log(
`🔍 위도(latitude) : ${yCoordinate}, 경도(longitude) : ${xCoordinate}, 검색어 : ${query}`
Expand All @@ -108,7 +112,9 @@ export class PlacesController {
yCoordinate,
query
);
res.status(HttpStatus.OK).json(searchResult);

return searchResult;
// res.status(HttpStatus.OK).json(searchResult);
} catch (error) {
res
.status(HttpStatus.NOT_FOUND)
Expand All @@ -125,14 +131,15 @@ export class PlacesController {
@ApiCreatedResponse({
status: 200,
})
async getAllPlaces(@Res() res) {
async getAllPlaces() {
try {
const result = await this.placesService.getAll();
return res.status(HttpStatus.OK).json(result);
return result;
// return res.status(HttpStatus.OK).json(result);
} catch (error) {
res
.status(HttpStatus.NOT_FOUND)
.json({ message: "Error occurred during search." });
// res
// .status(HttpStatus.NOT_FOUND)
// .json({ message: "Error occurred during search." });
}
}
/** -------------------- */
Expand Down Expand Up @@ -165,15 +172,16 @@ export class PlacesController {
description: "",
type: PlaceWithPostsResponse,
})
async getPlacePosts(@Param("id", ParseIntPipe) placeId: number, @Res() res) {
async getPlacePosts(@Param("id", ParseIntPipe) placeId: number) {
try {
const result = await this.placesService.getPlacePosts(placeId);
this.logger.log(`장소id (${placeId}) 의 게시물들이 조회됨 `);
return res.status(HttpStatus.OK).json(result);
return result;
// return res.status(HttpStatus.OK).json(result);
} catch (error) {
res
.status(HttpStatus.NOT_FOUND)
.json({ message: "Error occurred during search." });
// res
// .status(HttpStatus.NOT_FOUND)
// .json({ message: "Error occurred during search." });
}
}
/** -------------------- */
Expand Down

0 comments on commit 4d58082

Please sign in to comment.