From bc76ec1a86a766f57e12f27b3fcc3ed58f9afa92 Mon Sep 17 00:00:00 2001 From: hyunw9 Date: Sat, 23 Sep 2023 16:52:41 +0900 Subject: [PATCH 1/4] docs: Adocfile --- src/docs/asciidoc/resuggest.adoc | 44 ++++++++++++++++++++ src/docs/asciidoc/room.adoc | 71 +++++++++++++++++++++++++++++++- src/docs/asciidoc/vote.adoc | 4 +- 3 files changed, 116 insertions(+), 3 deletions(-) diff --git a/src/docs/asciidoc/resuggest.adoc b/src/docs/asciidoc/resuggest.adoc index 49c585e..4083bfa 100644 --- a/src/docs/asciidoc/resuggest.adoc +++ b/src/docs/asciidoc/resuggest.adoc @@ -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"] ==== \ No newline at end of file diff --git a/src/docs/asciidoc/room.adoc b/src/docs/asciidoc/room.adoc index e145c08..4f3a693 100644 --- a/src/docs/asciidoc/room.adoc +++ b/src/docs/asciidoc/room.adoc @@ -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"] ==== \ No newline at end of file diff --git a/src/docs/asciidoc/vote.adoc b/src/docs/asciidoc/vote.adoc index 04586c8..05aba11 100644 --- a/src/docs/asciidoc/vote.adoc +++ b/src/docs/asciidoc/vote.adoc @@ -1,10 +1,10 @@ == 투표 API -Path Variable 로 전송합니다. + +Path Variable 으로 방 ID를, Json 형식으로 투표하고자 하는 음식점들을 전달합니다. + 해당 ID의 음식점 득표 수를 증가시킵니다. + ==== <요청> ---- -/{roomId}/vote/{restaurantId} +/{roomId}/vote ---- 응답 From 522a12d1323979b3568891365a51540b7586f8a9 Mon Sep 17 00:00:00 2001 From: hyunw9 Date: Sat, 23 Sep 2023 16:53:21 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20=EC=A1=B0=ED=9A=8C,=EB=9E=9C?= =?UTF-8?q?=EB=8D=A4,=ED=88=AC=ED=91=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/application/RoomService.java | 39 +++++++++++---- .../domain/dto/request/VoteReq.java | 21 +++++++++ .../domain/dto/response/VoteRes.java | 47 ++++++++++++++++--- .../presentation/TodayMenuController.java | 8 ++-- .../repository/RestaurantRepository.java | 3 ++ .../domain/util/WebClientUtil.java | 4 +- 6 files changed, 100 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/bibum_server/domain/dto/request/VoteReq.java diff --git a/src/main/java/com/bibum_server/domain/application/RoomService.java b/src/main/java/com/bibum_server/domain/application/RoomService.java index 20d83f7..b716695 100644 --- a/src/main/java/com/bibum_server/domain/application/RoomService.java +++ b/src/main/java/com/bibum_server/domain/application/RoomService.java @@ -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; @@ -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 { @@ -30,7 +29,7 @@ public RoomRes getRoomInfo(Long roomId) { Room room = roomRepository.findById(roomId).orElseThrow(NoSuchElementException::new); List restaurantList = restaurantRepository.findAllByRoomId(roomId) .stream() - .map(restaurant -> RestaurantRes.fromEntity(restaurant)) + .map(RestaurantRes::fromEntity) .toList(); return RoomRes.builder() .id(room.getId()) @@ -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 = roomRepository.findById(roomId); - room.ifPresent(Room::incrementTotal); - restaurant.incrementCount(); - return RestaurantRes.fromEntity(restaurant); + public VoteRes voteRestaurant(Long roomId,VoteReq voteReq) { + List restaurantReqList = voteReq.getRestaurantIdList(); + Room room = roomRepository.findById(roomId).orElseThrow(NoSuchElementException::new); + List restaurantList = restaurantRepository.findByRoomIdAndIdIn(roomId, restaurantReqList); + + restaurantList.forEach(Restaurant::incrementCount); + room.incrementTotal(); + + List 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) { @@ -193,7 +212,7 @@ public RestaurantRes reSuggestOneRestaurant(Long roomId, Long restaurantId) { .build(); Restaurant deleteRestaurant = restaurantRepository.findByRoomIdAndId(roomId, restaurantId); restaurantRepository.delete(deleteRestaurant); - List receivedRestaurantResponse = webClientUtil.reSuggestRestaurant(reSuggestReq); + List receivedRestaurantResponse = webClientUtil.reSuggestOneRestaurant(reSuggestReq); Restaurant restaurant = receivedRestaurantResponse.stream() .map(restaurantResponse -> Restaurant.builder() diff --git a/src/main/java/com/bibum_server/domain/dto/request/VoteReq.java b/src/main/java/com/bibum_server/domain/dto/request/VoteReq.java new file mode 100644 index 0000000..2080270 --- /dev/null +++ b/src/main/java/com/bibum_server/domain/dto/request/VoteReq.java @@ -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 restaurantIdList; + + @Builder + public VoteReq( final List restaurantIdList) { + this.restaurantIdList = restaurantIdList; + } +} diff --git a/src/main/java/com/bibum_server/domain/dto/response/VoteRes.java b/src/main/java/com/bibum_server/domain/dto/response/VoteRes.java index 32db802..33c0922 100644 --- a/src/main/java/com/bibum_server/domain/dto/response/VoteRes.java +++ b/src/main/java/com/bibum_server/domain/dto/response/VoteRes.java @@ -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 restaurantVotes; @Builder - public VoteRes(Long id, Room room, Long count) { - this.id = id; - this.room = room; - this.count = count; + public VoteRes(List 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; + } } } diff --git a/src/main/java/com/bibum_server/domain/presentation/TodayMenuController.java b/src/main/java/com/bibum_server/domain/presentation/TodayMenuController.java index 721b150..d0c6e15 100644 --- a/src/main/java/com/bibum_server/domain/presentation/TodayMenuController.java +++ b/src/main/java/com/bibum_server/domain/presentation/TodayMenuController.java @@ -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.*; @@ -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") diff --git a/src/main/java/com/bibum_server/domain/restaurant/repository/RestaurantRepository.java b/src/main/java/com/bibum_server/domain/restaurant/repository/RestaurantRepository.java index c8a7f53..b93af1d 100644 --- a/src/main/java/com/bibum_server/domain/restaurant/repository/RestaurantRepository.java +++ b/src/main/java/com/bibum_server/domain/restaurant/repository/RestaurantRepository.java @@ -6,7 +6,10 @@ import java.util.List; public interface RestaurantRepository extends JpaRepository { + List findAllByRoomId(Long id); + List findByRoomIdAndIdIn(Long roomId, List restaurantIds); Restaurant findByRoomIdAndId(Long roomId, Long restaurantId); + void deleteAllByRoomId(Long roomId); } diff --git a/src/main/java/com/bibum_server/domain/util/WebClientUtil.java b/src/main/java/com/bibum_server/domain/util/WebClientUtil.java index e83ffec..337482c 100644 --- a/src/main/java/com/bibum_server/domain/util/WebClientUtil.java +++ b/src/main/java/com/bibum_server/domain/util/WebClientUtil.java @@ -96,7 +96,7 @@ public List reSuggestRestaurant(ReSuggestReq reS .getDocuments(); Collections.shuffle(restaurants); - return restaurants.subList(0, 1); + return restaurants.subList(0, 5); } public List reSuggestOneRestaurant(ReSuggestReq reSuggestReq){ @@ -119,7 +119,7 @@ public List reSuggestOneRestaurant(ReSuggestReq .getDocuments(); Collections.shuffle(restaurants); - return restaurants.subList(0, 5); + return restaurants.subList(0, 1); } From bd7e3a2f559d5848fa321459ebb2231681ee58d5 Mon Sep 17 00:00:00 2001 From: hyunw9 Date: Sat, 23 Sep 2023 16:53:31 +0900 Subject: [PATCH 3/4] docs: html --- src/main/resources/static/docs/index.html | 424 +++++++++++++--------- src/main/resources/static/docs/room.html | 139 ++++++- 2 files changed, 387 insertions(+), 176 deletions(-) diff --git a/src/main/resources/static/docs/index.html b/src/main/resources/static/docs/index.html index 0e4dd57..a2a23b4 100644 --- a/src/main/resources/static/docs/index.html +++ b/src/main/resources/static/docs/index.html @@ -447,8 +447,10 @@

오늘의 메뉴 API Docs

Table of Contents
@@ -659,8 +661,133 @@

Response body

"address" : "testAddress", "roomId" : 1, "rank" : 0 - }, { - "id" : 6, + } ] +} + + + + + + + +
+

방 조회 API

+
+
+

생성된 방의 정보를 가져옵니다.

+
+
+
+
+

<요청>

+
+
+
+
"http:localhost:8080/{roomId} -GET "
+
+
+
+

<응답>

+
+ +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

id

방 고유 ID

x

경도 값

y

위도 값

total

총 득표 수

restaurantResList

음식점 리스트

id

음식점 고유 ID

title

음식점 이름

category

음식점 유형

count

득표 수

link

음식점 URL

distance

현 위치로 부터 거리(m)

address

음식점 주소

roomId

속한 방 ID

rank

투표 순위

+
+

HTTP request

+
+
+
GET /1 HTTP/1.1
+Host: localhost:8080
+
+
+
+
+

Response body

+
+
+
{
+  "id" : 1,
+  "x" : "1",
+  "y" : "2",
+  "total" : 0,
+  "restaurantResList" : [ {
+    "id" : 1,
     "title" : "test",
     "category" : "testCat",
     "count" : 0,
@@ -670,7 +797,7 @@ 

Response body

"roomId" : 1, "rank" : 0 }, { - "id" : 7, + "id" : 2, "title" : "test", "category" : "testCat", "count" : 0, @@ -680,7 +807,7 @@

Response body

"roomId" : 1, "rank" : 0 }, { - "id" : 8, + "id" : 3, "title" : "test", "category" : "testCat", "count" : 0, @@ -690,7 +817,7 @@

Response body

"roomId" : 1, "rank" : 0 }, { - "id" : 9, + "id" : 4, "title" : "test", "category" : "testCat", "count" : 0, @@ -700,7 +827,7 @@

Response body

"roomId" : 1, "rank" : 0 }, { - "id" : 10, + "id" : 5, "title" : "test", "category" : "testCat", "count" : 0, @@ -722,7 +849,7 @@

Response body

투표 API

-

Path Variable 로 전송합니다.
+

Path Variable 으로 방 ID를, Json 형식으로 투표하고자 하는 음식점들을 전달합니다.
해당 ID의 음식점 득표 수를 증가시킵니다.

@@ -732,7 +859,7 @@

투표 API

-
/{roomId}/vote/{restaurantId}
+
/{roomId}/vote
@@ -786,9 +913,14 @@

투표 API

HTTP request

-
POST /1/vote/2 HTTP/1.1
+
POST /1/vote HTTP/1.1
+Content-Type: application/json;charset=UTF-8
+Content-Length: 35
 Host: localhost:8080
-Content-Type: application/x-www-form-urlencoded
+ +{ + "restaurantIdList" : [ 1, 2 ] +}
@@ -797,15 +929,27 @@

Response body

{
-  "id" : 1,
-  "title" : "Test Food",
-  "category" : "TestCategory",
-  "count" : 1,
-  "link" : "www.test.com",
-  "distance" : 1,
-  "address" : "TestAddress",
-  "roomId" : 1,
-  "rank" : 0
+  "restaurantVotes" : [ {
+    "id" : 1,
+    "title" : "testRestaurant",
+    "category" : "testCategory",
+    "count" : 1,
+    "link" : "www.test.com",
+    "distance" : 999,
+    "address" : "testAddress",
+    "roomId" : 1,
+    "rank" : 0
+  }, {
+    "id" : 2,
+    "title" : "testRestaurant",
+    "category" : "testCategory",
+    "count" : 1,
+    "link" : "www.test.com",
+    "distance" : 999,
+    "address" : "testAddress",
+    "roomId" : 2,
+    "rank" : 0
+  } ]
 }
@@ -984,56 +1128,6 @@

Response body

"address" : "nextPageAddress", "roomId" : 1, "rank" : 0 - }, { - "id" : 6, - "title" : "NextPageTitle", - "category" : "nextPageCategory", - "count" : 0, - "link" : "www.nextPageLink.com", - "distance" : 99, - "address" : "nextPageAddress", - "roomId" : 1, - "rank" : 0 - }, { - "id" : 7, - "title" : "NextPageTitle", - "category" : "nextPageCategory", - "count" : 0, - "link" : "www.nextPageLink.com", - "distance" : 99, - "address" : "nextPageAddress", - "roomId" : 1, - "rank" : 0 - }, { - "id" : 8, - "title" : "NextPageTitle", - "category" : "nextPageCategory", - "count" : 0, - "link" : "www.nextPageLink.com", - "distance" : 99, - "address" : "nextPageAddress", - "roomId" : 1, - "rank" : 0 - }, { - "id" : 9, - "title" : "NextPageTitle", - "category" : "nextPageCategory", - "count" : 0, - "link" : "www.nextPageLink.com", - "distance" : 99, - "address" : "nextPageAddress", - "roomId" : 1, - "rank" : 0 - }, { - "id" : 10, - "title" : "NextPageTitle", - "category" : "nextPageCategory", - "count" : 0, - "link" : "www.nextPageLink.com", - "distance" : 99, - "address" : "nextPageAddress", - "roomId" : 1, - "rank" : 0 } ] }
@@ -1044,6 +1138,98 @@

Response body

+

재추천 API (1 개)

+
+
+
+
+

<요청>

+
+
+
+
http://localhost:8080/{roomId}/resuggest/{restaurantid}
+
+
+
+

<응답>

+
+ ++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

id

방 고유 ID

title

음식점 이름

category

카테고리

count

투표 받은 횟수

link

음식점 링크

distance

음식점 거리(m)

address

음식점 주소

roomId

음식점이 속한 방

rank

음식점 순위

+
+

HTTP request

+
+
+
POST /1/resuggest/1 HTTP/1.1
+Host: localhost:8080
+Content-Type: application/x-www-form-urlencoded
+
+
+
+
+

Response body

+
+
+
{
+  "id" : 1,
+  "title" : "ReSuggestedRestaurant",
+  "category" : "TestCategory",
+  "count" : 0,
+  "link" : "www.ResuggestURL.com",
+  "distance" : 123,
+  "address" : "testAddress",
+  "roomId" : 1,
+  "rank" : 0
+}
+
+
+
+
+
+
+
+

결과 확인 API

@@ -1201,56 +1387,6 @@

Response body

"address" : "testAddress", "roomId" : 1, "rank" : 5 - }, { - "id" : 6, - "title" : "test", - "category" : "testCat", - "count" : 0, - "link" : "www.test.com", - "distance" : 1, - "address" : "testAddress", - "roomId" : 1, - "rank" : 6 - }, { - "id" : 7, - "title" : "test", - "category" : "testCat", - "count" : 0, - "link" : "www.test.com", - "distance" : 1, - "address" : "testAddress", - "roomId" : 1, - "rank" : 7 - }, { - "id" : 8, - "title" : "test", - "category" : "testCat", - "count" : 0, - "link" : "www.test.com", - "distance" : 1, - "address" : "testAddress", - "roomId" : 1, - "rank" : 8 - }, { - "id" : 9, - "title" : "test", - "category" : "testCat", - "count" : 0, - "link" : "www.test.com", - "distance" : 1, - "address" : "testAddress", - "roomId" : 1, - "rank" : 9 - }, { - "id" : 10, - "title" : "test", - "category" : "testCat", - "count" : 0, - "link" : "www.test.com", - "distance" : 1, - "address" : "testAddress", - "roomId" : 1, - "rank" : 10 } ] }
@@ -1430,56 +1566,6 @@

Response body

"address" : "testAddress", "roomId" : 1, "rank" : 0 - }, { - "id" : 6, - "title" : "test", - "category" : "testCat", - "count" : 0, - "link" : "www.test.com", - "distance" : 1, - "address" : "testAddress", - "roomId" : 1, - "rank" : 0 - }, { - "id" : 7, - "title" : "test", - "category" : "testCat", - "count" : 0, - "link" : "www.test.com", - "distance" : 1, - "address" : "testAddress", - "roomId" : 1, - "rank" : 0 - }, { - "id" : 8, - "title" : "test", - "category" : "testCat", - "count" : 0, - "link" : "www.test.com", - "distance" : 1, - "address" : "testAddress", - "roomId" : 1, - "rank" : 0 - }, { - "id" : 9, - "title" : "test", - "category" : "testCat", - "count" : 0, - "link" : "www.test.com", - "distance" : 1, - "address" : "testAddress", - "roomId" : 1, - "rank" : 0 - }, { - "id" : 10, - "title" : "test", - "category" : "testCat", - "count" : 0, - "link" : "www.test.com", - "distance" : 1, - "address" : "testAddress", - "roomId" : 1, - "rank" : 0 } ] }
diff --git a/src/main/resources/static/docs/room.html b/src/main/resources/static/docs/room.html index 3988503..f425310 100644 --- a/src/main/resources/static/docs/room.html +++ b/src/main/resources/static/docs/room.html @@ -624,8 +624,133 @@

Response body

"address" : "testAddress", "roomId" : 1, "rank" : 0 - }, { - "id" : 6, + } ] +} +
+
+
+
+
+ + +
+

방 조회 API

+
+
+

생성된 방의 정보를 가져옵니다.

+
+
+
+
+

<요청>

+
+
+
+
"http:localhost:8080/{roomId} -GET "
+
+
+
+

<응답>

+
+ +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

id

방 고유 ID

x

경도 값

y

위도 값

total

총 득표 수

restaurantResList

음식점 리스트

id

음식점 고유 ID

title

음식점 이름

category

음식점 유형

count

득표 수

link

음식점 URL

distance

현 위치로 부터 거리(m)

address

음식점 주소

roomId

속한 방 ID

rank

투표 순위

+
+

HTTP request

+
+
+
GET /1 HTTP/1.1
+Host: localhost:8080
+
+
+
+
+

Response body

+
+
+
{
+  "id" : 1,
+  "x" : "1",
+  "y" : "2",
+  "total" : 0,
+  "restaurantResList" : [ {
+    "id" : 1,
     "title" : "test",
     "category" : "testCat",
     "count" : 0,
@@ -635,7 +760,7 @@ 

Response body

"roomId" : 1, "rank" : 0 }, { - "id" : 7, + "id" : 2, "title" : "test", "category" : "testCat", "count" : 0, @@ -645,7 +770,7 @@

Response body

"roomId" : 1, "rank" : 0 }, { - "id" : 8, + "id" : 3, "title" : "test", "category" : "testCat", "count" : 0, @@ -655,7 +780,7 @@

Response body

"roomId" : 1, "rank" : 0 }, { - "id" : 9, + "id" : 4, "title" : "test", "category" : "testCat", "count" : 0, @@ -665,7 +790,7 @@

Response body

"roomId" : 1, "rank" : 0 }, { - "id" : 10, + "id" : 5, "title" : "test", "category" : "testCat", "count" : 0, @@ -687,7 +812,7 @@

Response body

From acbffc6b4a22044c16efd2a3203cea4fc7dfd0f8 Mon Sep 17 00:00:00 2001 From: hyunw9 Date: Sat, 23 Sep 2023 16:53:40 +0900 Subject: [PATCH 4/4] test: testfile --- .../com/bibum_server/domain/TestUtil.java | 8 ++- .../presentation/RoomControllerTest.java | 50 ++++++++++++++--- .../presentation/VoteRestaurantTest.java | 53 +++++++++++++------ 3 files changed, 86 insertions(+), 25 deletions(-) diff --git a/src/test/java/com/bibum_server/domain/TestUtil.java b/src/test/java/com/bibum_server/domain/TestUtil.java index 1da34ef..e474da0 100644 --- a/src/test/java/com/bibum_server/domain/TestUtil.java +++ b/src/test/java/com/bibum_server/domain/TestUtil.java @@ -15,6 +15,10 @@ import java.util.stream.LongStream; public class TestUtil { + + public static final long LEAST_ID = 1L; + public static final long MAX_ID = 6L; + public static Room CreateTestRoom() { return Room.builder() .id(1L) @@ -25,7 +29,7 @@ public static Room CreateTestRoom() { .build(); } public static List CreateTestRestaurantList(Room room){ - return LongStream.range(1L, 11L) + return LongStream.range(1L, 6L) .mapToObj((i) -> Restaurant.builder() .room(room) .id(i) @@ -39,7 +43,7 @@ public static List CreateTestRestaurantList(Room room){ ).toList(); } public static List CreatereSuggestedRestaurantList(Room room){ - return LongStream.range(1L, 11L) + return LongStream.range(LEAST_ID, MAX_ID) .mapToObj((i) -> Restaurant.builder() .room(room) .id(i) diff --git a/src/test/java/com/bibum_server/domain/presentation/RoomControllerTest.java b/src/test/java/com/bibum_server/domain/presentation/RoomControllerTest.java index a5d7e42..1d7f03c 100644 --- a/src/test/java/com/bibum_server/domain/presentation/RoomControllerTest.java +++ b/src/test/java/com/bibum_server/domain/presentation/RoomControllerTest.java @@ -36,14 +36,12 @@ @WebMvcTest(TodayMenuController.class) class RoomControllerTest extends AbstractRestDocsTests { + ObjectMapper mapper = new ObjectMapper(); @Autowired private MockMvc mockMvc; - @MockBean private RoomService roomService; - ObjectMapper mapper = new ObjectMapper(); - @DisplayName("Create Room.") @Test void createRoom() throws Exception { @@ -57,7 +55,7 @@ void createRoom() throws Exception { Room room = TestUtil.CreateTestRoom(); List restaurantList = TestUtil.CreateTestRestaurantList(room); - room.addRestaurant(restaurantList); + room.addRestaurants(restaurantList); List restaurantResList = restaurantList.stream().map(RestaurantRes::fromEntity).toList(); RoomRes mockResponse = RoomRes.builder() @@ -83,7 +81,7 @@ void ReSuggest() throws Exception { Room room = TestUtil.CreateTestRoom(); List restaurantList = TestUtil.CreatereSuggestedRestaurantList(room); List restaurantResList = restaurantList.stream().map(RestaurantRes::fromEntity).toList(); - given(roomService.ReSuggestRestaurant(roomId)).willReturn(RoomRes.builder().id(room.getId()) + given(roomService.ReSuggestRestaurants(roomId)).willReturn(RoomRes.builder().id(room.getId()) .total(room.getTotal()) .x(room.getX()) .y(room.getY()) @@ -94,13 +92,14 @@ void ReSuggest() throws Exception { .andExpect(status().isOk()) .andDo(restDocs.document()); } + @Test void retry() throws Exception { long roomId = 1L; Room room = TestUtil.CreateTestRoom(); List restaurantList = TestUtil.CreateTestRestaurantList(room); - room.addRestaurant(restaurantList); + room.addRestaurants(restaurantList); List restaurantResList = restaurantList.stream().map(RestaurantRes::fromEntity).toList(); RoomRes mockResponse = RoomRes.builder() @@ -118,6 +117,45 @@ void retry() throws Exception { .andDo(restDocs.document()); } + @Test + void getRoomInfo() throws Exception { + long roomId = 1L; + Room room = TestUtil.CreateTestRoom(); + List restaurantList = TestUtil.CreateTestRestaurantList(room); + List restaurantResList = restaurantList.stream().map(RestaurantRes::fromEntity).toList(); + RoomRes mockResponse = RoomRes.builder() + .id(room.getId()) + .x(room.getX()) + .y(room.getY()) + .total(room.getTotal()) + .restaurantResList(restaurantResList) + .build(); + given(roomService.getRoomInfo(any(Long.class))).willReturn(mockResponse); + this.mockMvc.perform(RestDocumentationRequestBuilders.get("/{roomId}", roomId)) + .andExpect(status().isOk()) + .andDo(restDocs.document()); + } + @Test + void ReSuggestOneRestaurant() throws Exception{ + long roomId = 1L; + long restaurantId = 1L; + Room room = TestUtil.CreateTestRoom(); + Restaurant restaurant = Restaurant.builder() + .room(room) + .id(1L) + .title("ReSuggestedRestaurant") + .link("www.ResuggestURL.com") + .distance(123L) + .count(0L) + .category("TestCategory") + .address("testAddress") + .build(); + RestaurantRes response = RestaurantRes.fromEntity(restaurant); + given(roomService.reSuggestOneRestaurant(any(Long.class),any(Long.class))).willReturn(response); + this.mockMvc.perform(RestDocumentationRequestBuilders.post("/{roomId}/resuggest/{restaurantId}",roomId,restaurantId)) + .andExpect(status().isOk()) + .andDo(restDocs.document()); + } } diff --git a/src/test/java/com/bibum_server/domain/presentation/VoteRestaurantTest.java b/src/test/java/com/bibum_server/domain/presentation/VoteRestaurantTest.java index afb5d2c..edca72f 100644 --- a/src/test/java/com/bibum_server/domain/presentation/VoteRestaurantTest.java +++ b/src/test/java/com/bibum_server/domain/presentation/VoteRestaurantTest.java @@ -3,11 +3,14 @@ import com.bibum_server.domain.AbstractRestDocsTests; import com.bibum_server.domain.TestUtil; import com.bibum_server.domain.application.RoomService; +import com.bibum_server.domain.dto.request.VoteReq; import com.bibum_server.domain.dto.response.MostPopularRestaurantRes; import com.bibum_server.domain.dto.response.RestaurantRes; +import com.bibum_server.domain.dto.response.VoteRes; import com.bibum_server.domain.restaurant.entity.Restaurant; import com.bibum_server.domain.restaurant.repository.RestaurantRepository; import com.bibum_server.domain.room.entity.Room; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -15,10 +18,14 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders; import org.springframework.test.web.servlet.MockMvc; +import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.LongStream; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; @@ -37,28 +44,43 @@ public class VoteRestaurantTest extends AbstractRestDocsTests { @MockBean private RestaurantRepository restaurantRepository; + ObjectMapper mapper = new ObjectMapper(); + + @DisplayName("Vote Restaurant") @Test void voteRestaurant() throws Exception{ //given long roomId = 1L; - long restaurantId = 2L; - RestaurantRes restaurantRes= RestaurantRes.builder() - .id(1L) - .title("Test Food") - .category("TestCategory") - .link("www.test.com") - .count(1L) - .distance(1L) - .rank(0L) - .roomId(1L) - .address("TestAddress") + List restaurantId = new ArrayList<>(); + restaurantId.add(1L); + restaurantId.add(2L); + VoteReq voteReq = VoteReq.builder() + .restaurantIdList(restaurantId) .build(); - given(roomService.voteRestaurant(any(),any())).willReturn(restaurantRes); + String VoteRequest = mapper.writeValueAsString(voteReq); + + List voteResList = LongStream.range(1L,3L).mapToObj(i->{ + return VoteRes.RestaurantVote.builder() + .roomId(i) + .id(i) + .rank(0L) + .address("testAddress") + .distance(999L) + .link("www.test.com") + .count(1L) + .category("testCategory") + .title("testRestaurant") + .build(); + }).toList(); + VoteRes voteRes = VoteRes.builder().restaurantVotes(voteResList).build(); + + given(roomService.voteRestaurant(any(),any())).willReturn(voteRes); - this.mockMvc.perform(RestDocumentationRequestBuilders.post("/{roomId}/vote/{restaurantId}", - roomId,restaurantId)) + this.mockMvc.perform(RestDocumentationRequestBuilders.post("/{roomId}/vote", + roomId).contentType(MediaType.APPLICATION_JSON) + .content(VoteRequest)) .andExpect(status().isOk()) .andDo(restDocs.document()); @@ -74,9 +96,6 @@ void checkBestRestaurant() throws Exception { MostPopularRestaurantRes mostPopularRestaurantRes = TestUtil.CreateTestBestRestaurantList(room,restaurantList); given(restaurantRepository.findAllByRoomId(roomId)).willReturn(restaurantList); given(roomService.checkBestRestaurant(roomId)).willReturn(mostPopularRestaurantRes); - - - //then this.mockMvc.perform(RestDocumentationRequestBuilders.get("/{roomId}/result",roomId))