Skip to content

Commit

Permalink
Merge pull request #221 from GeunH/feature/be-home-final
Browse files Browse the repository at this point in the history
[Home-Final/be] 음식점 관련 응답에 사진 추가
  • Loading branch information
LeeTH916 authored Dec 6, 2023
2 parents a142ed7 + 2e839f5 commit 8b80335
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 14 deletions.
13 changes: 10 additions & 3 deletions be/src/restaurant/restaurant.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { LocationDto } from "./dto/location.dto";
@ApiTags("Home")
@Controller("restaurant")
export class RestaurantController {
constructor(private restaurantService: RestaurantService) {}
constructor(private restaurantService: RestaurantService) { }
@Get("autocomplete/:partialRestaurantName")
@UseGuards(AuthGuard("jwt"))
@ApiBearerAuth()
Expand Down Expand Up @@ -162,6 +162,12 @@ export class RestaurantController {
type: String,
description: "검색 반경",
})
@ApiQuery({
name: "limit",
required: false,
type: String,
description: "응답 개수",
})
@ApiResponse({
status: 200,
description: "전체 음식점 리스트 요청 성공",
Expand All @@ -174,8 +180,9 @@ export class RestaurantController {
@UsePipes(new ValidationPipe())
entireRestaurantList(
@GetUser() tokenInfo: TokenInfo,
@Query() locationDto: LocationDto
@Query() locationDto: LocationDto,
@Query("limit") limit: string
) {
return this.restaurantService.entireRestaurantList(locationDto, tokenInfo);
return this.restaurantService.entireRestaurantList(locationDto, tokenInfo, limit);
}
}
5 changes: 4 additions & 1 deletion be/src/restaurant/restaurant.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ export class RestaurantRepository extends Repository<RestaurantInfoEntity> {
}
}

async entireRestaurantList(locationDto: LocationDto, tokenInfo: TokenInfo) {
async entireRestaurantList(locationDto: LocationDto, tokenInfo: TokenInfo, limit: string = "40") {
const limitNum = parseInt(limit);

return this.createQueryBuilder("restaurant")
.leftJoin(
UserRestaurantListEntity,
Expand Down Expand Up @@ -208,6 +210,7 @@ export class RestaurantRepository extends Repository<RestaurantInfoEntity> {
location,
ST_GeomFromText('POINT(${locationDto.longitude} ${locationDto.latitude})', 4326)) < ${locationDto.radius}`
)
.limit(limitNum)
.getRawMany();
}

Expand Down
54 changes: 51 additions & 3 deletions be/src/restaurant/restaurant.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ export class RestaurantService implements OnModuleInit {
})
.getCount();

const reviewInfo = await this.reviewRepository
.createQueryBuilder("review")
.leftJoin("review.reviewLikes", "reviewLike")
.select(["review.id", "review.reviewImage"],)
.groupBy("review.id")
.where("review.restaurant_id = :restaurantId and review.reviewImage is NOT NULL", { restaurantId: restaurant.restaurant_id })
.orderBy("COUNT(CASE WHEN reviewLike.isLike = true THEN 1 ELSE NULL END)", "DESC")
.getRawOne();
if (reviewInfo) {
restaurant.restaurant_reviewImage = this.awsService.getImageURL(reviewInfo.review_reviewImage);
}
else {
restaurant.restaurant_reviewImage = this.awsService.getImageURL("review/images/defaultImage.png");
}


restaurant.restaurant_reviewCnt = reviewCount;
}

Expand All @@ -71,6 +87,7 @@ export class RestaurantService implements OnModuleInit {
"review.restroomCleanliness",
"review.overallExperience",
"user.nickName as reviewer",
"user.profileImage",
"review.createdAt",
"review.reviewImage",
"reviewLike.isLike as isLike"
Expand All @@ -84,6 +101,7 @@ export class RestaurantService implements OnModuleInit {
const reviewList = reviews.slice(0, 3);
reviewList.forEach((element) => {
if (element.review_reviewImage) element.review_reviewImage = this.awsService.getImageURL(element.review_reviewImage);
if (element.user_profileImage) element.user_profileImage = this.awsService.getImageURL(element.user_profileImage);
})
restaurant.reviews = reviewList;
return restaurant;
Expand Down Expand Up @@ -112,16 +130,33 @@ export class RestaurantService implements OnModuleInit {
})
.getCount();

const reviewInfo = await this.reviewRepository
.createQueryBuilder("review")
.leftJoin("review.reviewLikes", "reviewLike")
.select(["review.id", "review.reviewImage"],)
.groupBy("review.id")
.where("review.restaurant_id = :restaurantId and review.reviewImage is NOT NULL", { restaurantId: restaurant.restaurant_id })
.orderBy("COUNT(CASE WHEN reviewLike.isLike = true THEN 1 ELSE NULL END)", "DESC")
.getRawOne();
if (reviewInfo) {
restaurant.restaurant_reviewImage = this.awsService.getImageURL(reviewInfo.review_reviewImage);
}
else {
restaurant.restaurant_reviewImage = this.awsService.getImageURL("review/images/defaultImage.png");
}


restaurant.restaurant_reviewCnt = reviewCount;
}

return restaurants;
}

async entireRestaurantList(locationDto: LocationDto, tokenInfo: TokenInfo) {
async entireRestaurantList(locationDto: LocationDto, tokenInfo: TokenInfo, limit: string) {
const restaurants = await this.restaurantRepository.entireRestaurantList(
locationDto,
tokenInfo
tokenInfo,
limit
);

for (const restaurant of restaurants) {
Expand All @@ -131,7 +166,20 @@ export class RestaurantService implements OnModuleInit {
restaurantId: restaurant.restaurant_id,
})
.getCount();

const reviewInfo = await this.reviewRepository
.createQueryBuilder("review")
.leftJoin("review.reviewLikes", "reviewLike")
.select(["review.id", "review.reviewImage"],)
.groupBy("review.id")
.where("review.restaurant_id = :restaurantId and review.reviewImage is NOT NULL", { restaurantId: restaurant.restaurant_id })
.orderBy("COUNT(CASE WHEN reviewLike.isLike = true THEN 1 ELSE NULL END)", "DESC")
.getRawOne();
if (reviewInfo) {
restaurant.restaurant_reviewImage = this.awsService.getImageURL(reviewInfo.review_reviewImage);
}
else {
restaurant.restaurant_reviewImage = this.awsService.getImageURL("review/images/defaultImage.png");
}
restaurant.restaurant_reviewCnt = reviewCount;
}

Expand Down
8 changes: 6 additions & 2 deletions be/src/review/dto/reviewInfo.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export class ReviewInfoDto {
example: "0",
description: "transportation Accessibility for visiting",
})
@Transform(({ value }) => parseInt(value))
@Transform(({ value }) => {
return !value ? null : parseInt(value, 10);
})
@IsInt()
@IsOptional()
@Min(0)
Expand All @@ -37,7 +39,9 @@ export class ReviewInfoDto {
example: "0",
description: "condition of the restaurant's parking area",
})
@Transform(({ value }) => parseInt(value))
@Transform(({ value }) => {
return !value ? null : parseInt(value, 10);
})
@IsInt()
@IsOptional()
@Min(0)
Expand Down
2 changes: 1 addition & 1 deletion be/src/review/entities/review.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class ReviewInfoEntity {
@Column({ type: "text" })
overallExperience: string;

@Column({ type: "text", nullable: true })
@Column({ type: "text", nullable: true, default: null })
reviewImage: string;

@CreateDateColumn({ name: "created_at" })
Expand Down
3 changes: 3 additions & 0 deletions be/src/review/review.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class ReviewRepository extends Repository<ReviewInfoEntity> {
"review.restroomCleanliness",
"review.overallExperience",
"user.nickName as reviewer",
"user.profileImage",
"review.createdAt",
"review.reviewImage",
"reviewLike.isLike as isLike",
Expand Down Expand Up @@ -81,6 +82,7 @@ export class ReviewRepository extends Repository<ReviewInfoEntity> {
"review.restroomCleanliness",
"review.overallExperience",
"user.nickName as reviewer",
"user.profileImage",
"review.createdAt",
"review.reviewImage",
"reviewLike.isLike as isLike",
Expand Down Expand Up @@ -113,6 +115,7 @@ export class ReviewRepository extends Repository<ReviewInfoEntity> {
"review.restroomCleanliness",
"review.overallExperience",
"user.nickName as reviewer",
"user.profileImage",
"review.createdAt",
"review.reviewImage",
"reviewLike.isLike as isLike",
Expand Down
6 changes: 4 additions & 2 deletions be/src/review/review.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { ReviewRepository } from './review.repository';
import { TokenInfo } from 'src/user/user.decorator';
import { ReviewLikeRepository } from './review.like.repository';
import { SortInfoDto } from 'src/utils/sortInfo.dto';
import { AwsService } from 'src/aws/aws.service';

@Injectable()
export class ReviewService {
constructor(
private reviewRepository: ReviewRepository,
private reviewLikeRepository: ReviewLikeRepository
private reviewLikeRepository: ReviewLikeRepository,
private awsService: AwsService
) { }

async getSortedReviews(tokenInfo: TokenInfo, restaurantId: number, getSortedReviewsDto: SortInfoDto) {
Expand All @@ -30,7 +32,7 @@ export class ReviewService {
.where("reviewLike.reviewId = :reviewId", { reviewId: review.review_id })
.groupBy("reviewLike.isLike")
.getRawMany();

if (review.user_profileImage) review.user_profileImage = this.awsService.getImageURL(review.user_profileImage);
review.likeCount = Number(likeCounts.find(lc => lc.status === true)?.count) || 0;
review.dislikeCount = Number(likeCounts.find(lc => lc.status === false)?.count) || 0;
}
Expand Down
4 changes: 2 additions & 2 deletions be/src/user/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class User {
@Column({ type: "varchar", length: 20, nullable: true })
provider: string | null;

@Column({ type: "text", default : "profile/images/defaultprofile.png"})
@Column({ type: "text", default: "profile/images/defaultprofile.png" })
profileImage: string;

@CreateDateColumn({ type: "timestamp" })
Expand All @@ -55,7 +55,7 @@ export class User {
@OneToMany(() => FollowEntity, (follow) => follow.followedUserId)
follower: FollowEntity[];

@OneToMany(() => UserRestaurantListEntity, (list) => list.userId)
@OneToMany(() => UserRestaurantListEntity, (list) => list.user)
restaurant: UserRestaurantListEntity[];

@OneToMany(() => ReviewInfoEntity, (review) => review.user)
Expand Down
18 changes: 18 additions & 0 deletions be/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ export class UserService {
})
.getCount();

const reviewInfo = await this.reviewRepository
.createQueryBuilder("review")
.leftJoin("review.reviewLikes", "reviewLike")
.select(["review.id", "review.reviewImage"],)
.groupBy("review.id")
.where("review.restaurant_id = :restaurantId and review.reviewImage is NOT NULL", { restaurantId: restaurant.restaurant_id })
.orderBy("COUNT(CASE WHEN reviewLike.isLike = true THEN 1 ELSE NULL END)", "DESC")
.getRawOne();
if (reviewInfo) {
restaurant.restaurant_reviewImage = this.awsService.getImageURL(reviewInfo.review_reviewImage);
}
else {
restaurant.restaurant_reviewImage = this.awsService.getImageURL("review/images/defaultImage.png");
}

restaurant.isMy = true;
restaurant.restaurant_reviewCnt = reviewCount;
}
Expand Down Expand Up @@ -294,6 +309,9 @@ export class UserService {
reviewImage = `review/images/${uuid}.png`;
reviewEntity.reviewImage = reviewImage;
}
else {
reviewEntity.reviewImage = `review/images/defaultImage.png`;
}
const userEntity = new User();
userEntity.id = tokenInfo["id"];
reviewEntity.user = userEntity;
Expand Down

0 comments on commit 8b80335

Please sign in to comment.