From ce59ce1bb9b3be663b1184deaa99d9c59f743a53 Mon Sep 17 00:00:00 2001
From: seokjin8678 <seokjin8678@gmail.com>
Date: Mon, 8 Apr 2024 22:50:46 +0900
Subject: [PATCH] =?UTF-8?q?feat:=20=EA=B4=80=EB=A6=AC=EC=9E=90=EC=9A=A9=20?=
 =?UTF-8?q?=EC=86=8C=EC=85=9C=EB=AF=B8=EB=94=94=EC=96=B4=20=EC=A1=B0?=
 =?UTF-8?q?=ED=9A=8C=20API=20=EC=B6=94=EA=B0=80?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- findByOwnerIdAndOwnerType: 목록 조회
- findById: 단건 조회
---
 .../v1/AdminSocialMediaV1Controller.java      |  24 ++++
 .../v1/AdminSocialMediaV1ControllerTest.java  | 107 ++++++++++++++++++
 2 files changed, 131 insertions(+)

diff --git a/backend/src/main/java/com/festago/admin/presentation/v1/AdminSocialMediaV1Controller.java b/backend/src/main/java/com/festago/admin/presentation/v1/AdminSocialMediaV1Controller.java
index fb985e0c1..8b4d974d6 100644
--- a/backend/src/main/java/com/festago/admin/presentation/v1/AdminSocialMediaV1Controller.java
+++ b/backend/src/main/java/com/festago/admin/presentation/v1/AdminSocialMediaV1Controller.java
@@ -1,18 +1,24 @@
 package com.festago.admin.presentation.v1;
 
+import com.festago.admin.application.AdminSocialMediaV1QueryService;
+import com.festago.admin.dto.socialmedia.AdminSocialMediaV1Response;
 import com.festago.admin.dto.socialmedia.SocialMediaCreateV1Request;
 import com.festago.admin.dto.socialmedia.SocialMediaUpdateV1Request;
 import com.festago.socialmedia.application.SocialMediaCommandService;
+import com.festago.socialmedia.domain.OwnerType;
 import io.swagger.v3.oas.annotations.Hidden;
 import jakarta.validation.Valid;
+import java.util.List;
 import lombok.RequiredArgsConstructor;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PatchMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 @RestController
@@ -22,6 +28,24 @@
 public class AdminSocialMediaV1Controller {
 
     private final SocialMediaCommandService socialMediaCommandService;
+    private final AdminSocialMediaV1QueryService adminSocialMediaV1QueryService;
+
+    @GetMapping
+    public ResponseEntity<List<AdminSocialMediaV1Response>> findByOwnerIdAndOwnerType(
+        @RequestParam Long ownerId,
+        @RequestParam OwnerType ownerType
+    ) {
+        return ResponseEntity.ok()
+            .body(adminSocialMediaV1QueryService.findByOwnerIdAndOwnerType(ownerId, ownerType));
+    }
+
+    @GetMapping("/{socialMediaId}")
+    public ResponseEntity<AdminSocialMediaV1Response> findById(
+        @PathVariable Long socialMediaId
+    ) {
+        return ResponseEntity.ok()
+            .body(adminSocialMediaV1QueryService.findById(socialMediaId));
+    }
 
     @PostMapping
     public ResponseEntity<Void> createSocialMedia(
diff --git a/backend/src/test/java/com/festago/admin/presentation/v1/AdminSocialMediaV1ControllerTest.java b/backend/src/test/java/com/festago/admin/presentation/v1/AdminSocialMediaV1ControllerTest.java
index d9519174b..33dd2310b 100644
--- a/backend/src/test/java/com/festago/admin/presentation/v1/AdminSocialMediaV1ControllerTest.java
+++ b/backend/src/test/java/com/festago/admin/presentation/v1/AdminSocialMediaV1ControllerTest.java
@@ -1,13 +1,17 @@
 package com.festago.admin.presentation.v1;
 
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
 import static org.mockito.BDDMockito.given;
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.festago.admin.application.AdminSocialMediaV1QueryService;
+import com.festago.admin.dto.socialmedia.AdminSocialMediaV1Response;
 import com.festago.admin.dto.socialmedia.SocialMediaCreateV1Request;
 import com.festago.admin.dto.socialmedia.SocialMediaUpdateV1Request;
 import com.festago.auth.domain.Role;
@@ -18,6 +22,7 @@
 import com.festago.support.CustomWebMvcTest;
 import com.festago.support.WithMockAuth;
 import jakarta.servlet.http.Cookie;
+import java.util.List;
 import org.junit.jupiter.api.DisplayName;
 import org.junit.jupiter.api.DisplayNameGeneration;
 import org.junit.jupiter.api.DisplayNameGenerator;
@@ -43,6 +48,108 @@ class AdminSocialMediaV1ControllerTest {
     @Autowired
     SocialMediaCommandService socialMediaCommandService;
 
+    @Autowired
+    AdminSocialMediaV1QueryService adminSocialMediaV1QueryService;
+
+    @Nested
+    class 소셜미디어_단건_조회 {
+
+        final String uri = "/admin/api/v1/socialmedias/{socialMediaId}";
+        Long socialMediaId = 1L;
+
+        @Nested
+        @DisplayName("GET " + uri)
+        class 올바른_주소로 {
+
+            @Test
+            @WithMockAuth(role = Role.ADMIN)
+            void 요청을_보내면_200_응답이_반환된다() throws Exception {
+                // given
+                given(adminSocialMediaV1QueryService.findById(anyLong()))
+                    .willReturn(new AdminSocialMediaV1Response(
+                        1L,
+                        SocialMediaType.INSTAGRAM,
+                        "테코대학교 총학생회 인스타그램",
+                        "https://image.com/logo.png",
+                        "htps://instagram.com/tecodaehak"
+                    ));
+
+                // when & then
+                mockMvc.perform(get(uri, socialMediaId)
+                        .cookie(TOKEN_COOKIE)
+                        .contentType(MediaType.APPLICATION_JSON))
+                    .andExpect(status().isOk());
+            }
+
+            @Test
+            void 토큰_없이_보내면_401_응답이_반환된다() throws Exception {
+                // when & then
+                mockMvc.perform(get(uri, socialMediaId))
+                    .andExpect(status().isUnauthorized());
+            }
+
+            @Test
+            @WithMockAuth(role = Role.MEMBER)
+            void 토큰의_권한이_Admin이_아니면_404_응답이_반환된다() throws Exception {
+                // when & then
+                mockMvc.perform(get(uri, socialMediaId)
+                        .cookie(TOKEN_COOKIE))
+                    .andExpect(status().isNotFound());
+            }
+        }
+    }
+
+    @Nested
+    class 소셜미디어_목록_조회 {
+
+        final String uri = "/admin/api/v1/socialmedias";
+
+        @Nested
+        @DisplayName("GET " + uri)
+        class 올바른_주소로 {
+
+            @Test
+            @WithMockAuth(role = Role.ADMIN)
+            void 요청을_보내면_200_응답이_반환된다() throws Exception {
+                // given
+                given(adminSocialMediaV1QueryService.findByOwnerIdAndOwnerType(anyLong(), any(OwnerType.class)))
+                    .willReturn(List.of(
+                        new AdminSocialMediaV1Response(
+                            1L,
+                            SocialMediaType.INSTAGRAM,
+                            "테코대학교 총학생회 인스타그램",
+                            "https://image.com/logo.png",
+                            "htps://instagram.com/tecodaehak"
+                        )
+                    ));
+
+                // when & then
+                mockMvc.perform(get(uri)
+                        .param("ownerId", "1")
+                        .param("ownerType", "SCHOOL")
+                        .cookie(TOKEN_COOKIE)
+                        .contentType(MediaType.APPLICATION_JSON))
+                    .andExpect(status().isOk());
+            }
+
+            @Test
+            void 토큰_없이_보내면_401_응답이_반환된다() throws Exception {
+                // when & then
+                mockMvc.perform(get(uri))
+                    .andExpect(status().isUnauthorized());
+            }
+
+            @Test
+            @WithMockAuth(role = Role.MEMBER)
+            void 토큰의_권한이_Admin이_아니면_404_응답이_반환된다() throws Exception {
+                // when & then
+                mockMvc.perform(get(uri)
+                        .cookie(TOKEN_COOKIE))
+                    .andExpect(status().isNotFound());
+            }
+        }
+    }
+
     @Nested
     class 소셜미디어_생성 {