-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
소수점 1의 자리수 까지 반올림하여 반환
- Loading branch information
Showing
3 changed files
with
62 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./place-star-rating.interceptor" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters