Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 블로그 이름 중복 확인 기능 #198

Merged
merged 3 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/com/mallang/auth/config/AuthConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private AuthInterceptor setUpAuthInterceptor() {
.uriPatterns(Set.of(
"/members/*",
"/blogs",
"/blogs/duplicate",
"/posts/**",
"/categories",
"/comments",
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/mallang/blog/presentation/BlogController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import com.mallang.blog.presentation.request.OpenBlogRequest;
import com.mallang.blog.query.BlogQueryService;
import com.mallang.blog.query.response.BlogResponse;
import com.mallang.blog.query.response.CheckDuplicateResponse;
import java.net.URI;
import lombok.RequiredArgsConstructor;
import org.springframework.data.repository.query.Param;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -45,4 +47,12 @@ public ResponseEntity<BlogResponse> findMyBlog(
) {
return ResponseEntity.ok(blogQueryService.findByOwnerId(memberId));
}

@GetMapping("/duplicate")
public ResponseEntity<CheckDuplicateResponse> checkDuplicate(
@Param("blogName") String blogName
) {
boolean duplicated = blogQueryService.checkDuplicatedBlogName(blogName);
return ResponseEntity.ok(new CheckDuplicateResponse(duplicated));
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/mallang/blog/query/BlogQueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ public BlogResponse findByOwnerId(Long memberId) {
Member member = memberQueryRepository.getById(memberId);
return BlogResponse.from(blogQueryRepository.getByOwner(member));
}

public boolean checkDuplicatedBlogName(String blogName) {
return blogQueryRepository.existsByName(blogName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ default Blog getByOwner(Member owner) {
}

Optional<Blog> findByOwner(Member owner);

@Query("SELECT COUNT(b) > 0 FROM Blog b WHERE b.name.value = :blogName")
boolean existsByName(@Param("blogName") String blogName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.mallang.blog.query.response;

public record CheckDuplicateResponse(
boolean duplicated
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class MemberAcceptanceSteps {
return given(세션)
.get("/members/logout")
.then()
.log().all()
.extract();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ void setUp() {
}
}


@Nested
class 로그아웃_API {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public class BlogAcceptanceSteps {
.extract();
}

public static ExtractableResponse<Response> 블로그_이름_중복_확인_요청(String 이름) {
return given()
.queryParam("blogName", 이름)
.get("/blogs/duplicate")
.then()
.extract();
}

public static ExtractableResponse<Response> 블로그_정보_조회_요청(String 블로그_이름) {
return given()
.queryParam("blogName", 블로그_이름)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import static com.mallang.acceptance.auth.AuthAcceptanceSteps.회원가입과_로그인_후_세션_ID_반환;
import static com.mallang.acceptance.blog.BlogAcceptanceSteps.내_블로그_정보_조회_요청;
import static com.mallang.acceptance.blog.BlogAcceptanceSteps.블로그_개설_요청;
import static com.mallang.acceptance.blog.BlogAcceptanceSteps.블로그_이름_중복_확인_요청;
import static com.mallang.acceptance.blog.BlogAcceptanceSteps.블로그_정보_조회_요청;
import static org.assertj.core.api.Assertions.assertThat;

import com.mallang.acceptance.AcceptanceTest;
import com.mallang.blog.query.response.BlogResponse;
import com.mallang.blog.query.response.BlogResponse.OwnerResponse;
import com.mallang.blog.query.response.CheckDuplicateResponse;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
Expand All @@ -29,7 +31,35 @@
class BlogAcceptanceTest extends AcceptanceTest {

@Nested
class 블로그_조회_API {
class 블로그_이름_중복_체크_API {

@Test
void 주어진_이름이_이미_사용중이면_중복됨() {
// given
var 말랑_세션_ID = 회원가입과_로그인_후_세션_ID_반환("말랑");
블로그_개설_요청(말랑_세션_ID, "mallang-blog");

// when
var 응답 = 블로그_이름_중복_확인_요청("mallang-blog");

// then
CheckDuplicateResponse response = 응답.as(CheckDuplicateResponse.class);
assertThat(response.duplicated()).isTrue();
}

@Test
void 주어진_이름이_사용중이지_않으면_중복되지_않음() {
// when
var 응답 = 블로그_이름_중복_확인_요청("mallang-blog");

// then
CheckDuplicateResponse response = 응답.as(CheckDuplicateResponse.class);
assertThat(response.duplicated()).isFalse();
}
}

@Nested
class 블로그_개설_API {

@Test
void 블로그를_개설한다() {
Expand Down