Skip to content

Commit

Permalink
Merge pull request #35 from Bibumhada/feat/33-leftAPIs
Browse files Browse the repository at this point in the history
[feat] 남은 기능API 개발( 중복투표, 1개 조회)
  • Loading branch information
hyunw9 authored Sep 23, 2023
2 parents 15540d2 + acbffc6 commit d53b741
Show file tree
Hide file tree
Showing 14 changed files with 689 additions and 226 deletions.
44 changes: 44 additions & 0 deletions src/docs/asciidoc/resuggest.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,48 @@ Path variable로 전송합니다.
|투표 순위
|===
operation::room-controller-test/re-suggest[snippets="http-request,response-body"]
====

== 재추천 API (1 개)
====
<요청>
----
http://localhost:8080/{roomId}/resuggest/{restaurantid}
----
<응답>
[cols=2*]
|===
|id
|방 고유 ID
|title
|음식점 이름
|category
|카테고리
|count
|투표 받은 횟수
|link
|음식점 링크
|distance
|음식점 거리(m)
|address
|음식점 주소
|roomId
|음식점이 속한 방
|rank
|음식점 순위
|===
operation::room-controller-test/re-suggest-one-restaurant[snippets="http-request,response-body"]
====
71 changes: 70 additions & 1 deletion src/docs/asciidoc/room.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,74 @@
|투표 순위
|===

operation::create-room-controller-test/create-room[snippets="http-request,response-body"]
operation::room-controller-test/create-room[snippets="http-request,response-body"]
====
== 방 조회 API
생성된 방의 정보를 가져옵니다.
====
<요청>
----
"http:localhost:8080/{roomId} -GET "
----
<응답>
[cols=3*]
|===
|id
|방 고유 ID
|
|x
|경도 값
|
|y
|위도 값
|
|total
|총 득표 수
|
|
|restaurantResList
|음식점 리스트
|
|id
|음식점 고유 ID
|
|title
|음식점 이름
|
|category
|음식점 유형
|
|count
|득표 수
|
|link
|음식점 URL
|
|distance
|현 위치로 부터 거리(m)
|
|address
|음식점 주소
|
|roomId
|속한 방 ID
|
|rank
|투표 순위
|===
operation::room-controller-test/get-room-info[snippets="http-request,response-body"]
====
4 changes: 2 additions & 2 deletions src/docs/asciidoc/vote.adoc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
== 투표 API
Path Variable 로 전송합니다. +
Path Variable 으로 방 ID를, Json 형식으로 투표하고자 하는 음식점들을 전달합니다. +
해당 ID의 음식점 득표 수를 증가시킵니다. +
====
<요청>
----
/{roomId}/vote/{restaurantId}
/{roomId}/vote
----

응답
Expand Down
39 changes: 29 additions & 10 deletions src/main/java/com/bibum_server/domain/application/RoomService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bibum_server.domain.application;

import com.bibum_server.domain.dto.request.ReSuggestReq;
import com.bibum_server.domain.dto.request.VoteReq;
import com.bibum_server.domain.dto.response.*;
import com.bibum_server.domain.dto.request.LocationReq;
import com.bibum_server.domain.restaurant.entity.Restaurant;
Expand All @@ -13,11 +14,9 @@
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.security.InvalidParameterException;
import java.util.*;
import java.util.stream.Collectors;


@Service
@RequiredArgsConstructor
public class RoomService {
Expand All @@ -30,7 +29,7 @@ public RoomRes getRoomInfo(Long roomId) {
Room room = roomRepository.findById(roomId).orElseThrow(NoSuchElementException::new);
List<RestaurantRes> restaurantList = restaurantRepository.findAllByRoomId(roomId)
.stream()
.map(restaurant -> RestaurantRes.fromEntity(restaurant))
.map(RestaurantRes::fromEntity)
.toList();
return RoomRes.builder()
.id(room.getId())
Expand Down Expand Up @@ -77,12 +76,32 @@ public RoomRes createRoom(LocationReq locationReq) {
}

@Transactional
public RestaurantRes voteRestaurant(Long roomId, Long restaurantId) {
Restaurant restaurant = restaurantRepository.findByRoomIdAndId(roomId, restaurantId);
Optional<Room> room = roomRepository.findById(roomId);
room.ifPresent(Room::incrementTotal);
restaurant.incrementCount();
return RestaurantRes.fromEntity(restaurant);
public VoteRes voteRestaurant(Long roomId,VoteReq voteReq) {
List<Long> restaurantReqList = voteReq.getRestaurantIdList();
Room room = roomRepository.findById(roomId).orElseThrow(NoSuchElementException::new);
List<Restaurant> restaurantList = restaurantRepository.findByRoomIdAndIdIn(roomId, restaurantReqList);

restaurantList.forEach(Restaurant::incrementCount);
room.incrementTotal();

List<VoteRes.RestaurantVote> votes =
restaurantList.stream()
.map(r -> {
return VoteRes.RestaurantVote.builder()
.id(r.getId())
.title(r.getTitle())
.category(r.getCategory())
.count(r.getCount())
.link(r.getLink())
.distance(r.getDistance())
.address(r.getAddress())
.rank(0L)
.roomId(room.getId())
.build();
})
.toList();

return new VoteRes(votes);
}

public MostPopularRestaurantRes checkBestRestaurant(Long roomId) {
Expand Down Expand Up @@ -193,7 +212,7 @@ public RestaurantRes reSuggestOneRestaurant(Long roomId, Long restaurantId) {
.build();
Restaurant deleteRestaurant = restaurantRepository.findByRoomIdAndId(roomId, restaurantId);
restaurantRepository.delete(deleteRestaurant);
List<KakaoApiRes.RestaurantResponse> receivedRestaurantResponse = webClientUtil.reSuggestRestaurant(reSuggestReq);
List<KakaoApiRes.RestaurantResponse> receivedRestaurantResponse = webClientUtil.reSuggestOneRestaurant(reSuggestReq);

Restaurant restaurant = receivedRestaurantResponse.stream()
.map(restaurantResponse -> Restaurant.builder()
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/bibum_server/domain/dto/request/VoteReq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.bibum_server.domain.dto.request;


import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@NoArgsConstructor
public class VoteReq {
List<Long> restaurantIdList;

@Builder
public VoteReq( final List<Long> restaurantIdList) {
this.restaurantIdList = restaurantIdList;
}
}
47 changes: 40 additions & 7 deletions src/main/java/com/bibum_server/domain/dto/response/VoteRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,49 @@

import com.bibum_server.domain.room.entity.Room;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@NoArgsConstructor
public class VoteRes {
Long id;
Room room;
Long count;

private List<RestaurantVote> restaurantVotes;

@Builder
public VoteRes(Long id, Room room, Long count) {
this.id = id;
this.room = room;
this.count = count;
public VoteRes(List<RestaurantVote> restaurantVotes) {
this.restaurantVotes = restaurantVotes;
}

@Getter
@Setter
@NoArgsConstructor
public static class RestaurantVote {
private Long id;
private String title;
private String category;
private Long count;
private String link;
private Long distance;
private String address;
private Long roomId;
private Long rank;

@Builder
public RestaurantVote(Long id, String title, String category, Long count, String link, Long distance, String address, Long roomId, Long rank) {
this.id = id;
this.title = title;
this.category = category;
this.count = count;
this.link = link;
this.distance = distance;
this.address = address;
this.roomId = roomId;
this.rank = rank;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.bibum_server.domain.presentation;

import com.bibum_server.domain.application.RoomService;
import com.bibum_server.domain.dto.request.VoteReq;
import com.bibum_server.domain.dto.response.NaverApiItemRes;
import com.bibum_server.domain.dto.response.RestaurantRes;
import com.bibum_server.domain.dto.response.RoomRes;
import com.bibum_server.domain.dto.request.LocationReq;
import com.bibum_server.domain.dto.response.MostPopularRestaurantRes;
import com.bibum_server.domain.dto.response.VoteRes;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

Expand All @@ -27,9 +29,9 @@ public RoomRes getRoomInfo(@PathVariable Long roomId){
return roomService.getRoomInfo(roomId);
}

@PostMapping("/{roomId}/vote/{restaurantId}")
public RestaurantRes voteRestaurant(@PathVariable("roomId") Long roomId, @PathVariable("restaurantId") Long restaurantId) {
return roomService.voteRestaurant(roomId, restaurantId);
@PostMapping(value = "/{roomId}/vote",produces = "application/json")
public VoteRes voteRestaurant(@PathVariable Long roomId, @RequestBody VoteReq voteReq) {
return roomService.voteRestaurant(roomId, voteReq);
}

@GetMapping("/{roomId}/result")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import java.util.List;

public interface RestaurantRepository extends JpaRepository<Restaurant,Long> {

List<Restaurant> findAllByRoomId(Long id);
List<Restaurant> findByRoomIdAndIdIn(Long roomId, List<Long> restaurantIds);
Restaurant findByRoomIdAndId(Long roomId, Long restaurantId);

void deleteAllByRoomId(Long roomId);
}
4 changes: 2 additions & 2 deletions src/main/java/com/bibum_server/domain/util/WebClientUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public List<KakaoApiRes.RestaurantResponse> reSuggestRestaurant(ReSuggestReq reS
.getDocuments();
Collections.shuffle(restaurants);

return restaurants.subList(0, 1);
return restaurants.subList(0, 5);
}

public List<KakaoApiRes.RestaurantResponse> reSuggestOneRestaurant(ReSuggestReq reSuggestReq){
Expand All @@ -119,7 +119,7 @@ public List<KakaoApiRes.RestaurantResponse> reSuggestOneRestaurant(ReSuggestReq
.getDocuments();
Collections.shuffle(restaurants);

return restaurants.subList(0, 5);
return restaurants.subList(0, 1);
}


Expand Down
Loading

0 comments on commit d53b741

Please sign in to comment.